0cf02386da2fe37c68897af7d1044741063def89
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.bgpio.types.attr;
17
18 import java.util.Arrays;
19 import java.util.Objects;
20
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.onosproject.bgpio.exceptions.BGPParseException;
23 import org.onosproject.bgpio.types.BGPErrorType;
24 import org.onosproject.bgpio.types.BGPValueType;
25 import org.onosproject.bgpio.util.Validation;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.common.base.MoreObjects;
30
31 /**
32  * Implements BGP prefix route tag attribute.
33  */
34 public class BgpPrefixAttrRouteTag implements BGPValueType {
35
36     protected static final Logger log = LoggerFactory
37             .getLogger(BgpPrefixAttrRouteTag.class);
38
39     public static final int ATTR_PREFIX_ROUTETAG = 1153;
40
41     /* Prefix Route Tag */
42     private int[] pfxRouteTag;
43
44     /**
45      * Constructor to initialize the values.
46      *
47      * @param pfxRouteTag prefix route tag
48      */
49     BgpPrefixAttrRouteTag(int[] pfxRouteTag) {
50         this.pfxRouteTag = Arrays.copyOf(pfxRouteTag, pfxRouteTag.length);
51     }
52
53     /**
54      * Reads the Route Tag.
55      *
56      * @param cb ChannelBuffer
57      * @return object of BgpPrefixAttrRouteTag
58      * @throws BGPParseException while parsing BgpPrefixAttrRouteTag
59      */
60     public static BgpPrefixAttrRouteTag read(ChannelBuffer cb)
61             throws BGPParseException {
62         int[] pfxRouteTag;
63
64         short lsAttrLength = cb.readShort();
65         int len = lsAttrLength / Integer.SIZE;
66
67         if (cb.readableBytes() < lsAttrLength) {
68             Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
69                                    BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
70                                    lsAttrLength);
71         }
72
73         pfxRouteTag = new int[lsAttrLength];
74
75         for (int i = 0; i < len; i++) {
76             pfxRouteTag[i] = cb.readInt();
77         }
78
79         return new BgpPrefixAttrRouteTag(pfxRouteTag);
80     }
81
82     /**
83      * Returns the prefix route tag.
84      *
85      * @return route tag
86      */
87     int[] getPfxRouteTag() {
88         return pfxRouteTag;
89     }
90
91     @Override
92     public short getType() {
93         return ATTR_PREFIX_ROUTETAG;
94     }
95
96     @Override
97     public int hashCode() {
98         return Objects.hash(pfxRouteTag);
99     }
100
101     @Override
102     public boolean equals(Object obj) {
103         if (this == obj) {
104             return true;
105         }
106
107         if (obj instanceof BgpPrefixAttrRouteTag) {
108             BgpPrefixAttrRouteTag other = (BgpPrefixAttrRouteTag) obj;
109             return Objects.equals(pfxRouteTag, other.pfxRouteTag);
110         }
111         return false;
112     }
113
114     @Override
115     public int write(ChannelBuffer cb) {
116         // TODO This will be implemented in the next version
117         return 0;
118     }
119
120     @Override
121     public String toString() {
122         return MoreObjects.toStringHelper(getClass()).omitNullValues()
123                 .add("pfxRouteTag", pfxRouteTag).toString();
124     }
125 }