a2d7c57e97081c6fc86d13318034acdd87a8365f
[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 static com.google.common.base.Preconditions.checkNotNull;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Objects;
23
24 import org.jboss.netty.buffer.ChannelBuffer;
25 import org.onosproject.bgpio.exceptions.BgpParseException;
26 import org.onosproject.bgpio.types.BgpErrorType;
27 import org.onosproject.bgpio.types.BgpValueType;
28 import org.onosproject.bgpio.util.Validation;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.MoreObjects;
33
34 /**
35  * Implements BGP prefix route Extended tag attribute.
36  */
37 public class BgpPrefixAttrExtRouteTag implements BgpValueType {
38
39     protected static final Logger log = LoggerFactory
40             .getLogger(BgpPrefixAttrExtRouteTag.class);
41
42     public static final int ATTR_PREFIX_EXTROUTETAG = 1154;
43     public static final int ATTR_PREFIX_EXT_LEN = 8;
44
45     /* Prefix Route Tag */
46     private List<Long> pfxExtRouteTag = new ArrayList<Long>();
47
48     /**
49      * Constructor to initialize the values.
50      *
51      * @param pfxExtRouteTag Extended route tag
52      */
53     public BgpPrefixAttrExtRouteTag(List<Long> pfxExtRouteTag) {
54         this.pfxExtRouteTag = checkNotNull(pfxExtRouteTag);
55     }
56
57     /**
58      * Returns object of this class with specified values.
59      *
60      * @param pfxExtRouteTag Prefix Metric
61      * @return object of BgpPrefixAttrMetric
62      */
63     public static BgpPrefixAttrExtRouteTag of(ArrayList<Long> pfxExtRouteTag) {
64         return new BgpPrefixAttrExtRouteTag(pfxExtRouteTag);
65     }
66
67     /**
68      * Reads the Extended Tag.
69      *
70      * @param cb ChannelBuffer
71      * @return object of BgpPrefixAttrExtRouteTag
72      * @throws BgpParseException while parsing BgpPrefixAttrExtRouteTag
73      */
74     public static BgpPrefixAttrExtRouteTag read(ChannelBuffer cb)
75             throws BgpParseException {
76         ArrayList<Long> pfxExtRouteTag = new ArrayList<Long>();
77         long temp;
78
79         short lsAttrLength = cb.readShort();
80         int len = lsAttrLength / ATTR_PREFIX_EXT_LEN;
81
82         if (cb.readableBytes() < lsAttrLength) {
83             Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
84                                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
85                                    lsAttrLength);
86         }
87
88         for (int i = 0; i < len; i++) {
89             temp = cb.readLong();
90             pfxExtRouteTag.add(new Long(temp));
91         }
92
93         return new BgpPrefixAttrExtRouteTag(pfxExtRouteTag);
94     }
95
96     /**
97      * Returns Extended route tag.
98      *
99      * @return route tag
100      */
101     public List<Long> pfxExtRouteTag() {
102         return pfxExtRouteTag;
103     }
104
105     @Override
106     public short getType() {
107         return ATTR_PREFIX_EXTROUTETAG;
108     }
109
110     @Override
111     public int hashCode() {
112         return Objects.hash(pfxExtRouteTag);
113     }
114
115     @Override
116     public boolean equals(Object obj) {
117         if (this == obj) {
118             return true;
119         }
120
121         if (obj instanceof BgpPrefixAttrExtRouteTag) {
122             BgpPrefixAttrExtRouteTag other = (BgpPrefixAttrExtRouteTag) obj;
123             return Objects.equals(pfxExtRouteTag, other.pfxExtRouteTag);
124         }
125         return false;
126     }
127
128     @Override
129     public int write(ChannelBuffer cb) {
130         // TODO This will be implemented in the next version
131         return 0;
132     }
133
134     @Override
135     public String toString() {
136         return MoreObjects.toStringHelper(getClass()).omitNullValues()
137                 .add("pfxExtRouteTag", pfxExtRouteTag).toString();
138     }
139
140     @Override
141     public int compareTo(Object o) {
142         // TODO Auto-generated method stub
143         return 0;
144     }
145 }