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