blob: 3894c0039968392814509e16ca8bfe45a89240a0 [file] [log] [blame]
Thejaswi N K80252d22015-10-14 23:34:23 +05301/*
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 */
16package org.onosproject.bgpio.types.attr;
17
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053018import java.util.ArrayList;
19import java.util.List;
Thejaswi N K80252d22015-10-14 23:34:23 +053020import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.types.BgpErrorType;
25import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N K80252d22015-10-14 23:34:23 +053026import org.onosproject.bgpio.util.Validation;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * Implements BGP prefix route tag attribute.
34 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035public class BgpPrefixAttrRouteTag implements BgpValueType {
Thejaswi N K80252d22015-10-14 23:34:23 +053036
37 protected static final Logger log = LoggerFactory
38 .getLogger(BgpPrefixAttrRouteTag.class);
39
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053040 public static final short ATTR_PREFIX_ROUTETAG = 1153;
Thejaswi N K80252d22015-10-14 23:34:23 +053041
42 /* Prefix Route Tag */
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053043 private List<Integer> pfxRouteTag = new ArrayList<Integer>();
Thejaswi N K80252d22015-10-14 23:34:23 +053044
45 /**
46 * Constructor to initialize the values.
47 *
48 * @param pfxRouteTag prefix route tag
49 */
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053050 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);
Thejaswi N K80252d22015-10-14 23:34:23 +053062 }
63
64 /**
65 * Reads the Route Tag.
66 *
67 * @param cb ChannelBuffer
68 * @return object of BgpPrefixAttrRouteTag
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 * @throws BgpParseException while parsing BgpPrefixAttrRouteTag
Thejaswi N K80252d22015-10-14 23:34:23 +053070 */
71 public static BgpPrefixAttrRouteTag read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 throws BgpParseException {
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053073 int tmp;
74 ArrayList<Integer> pfxRouteTag = new ArrayList<Integer>();
Thejaswi N K80252d22015-10-14 23:34:23 +053075
76 short lsAttrLength = cb.readShort();
77 int len = lsAttrLength / Integer.SIZE;
78
79 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
81 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K80252d22015-10-14 23:34:23 +053082 lsAttrLength);
83 }
84
Thejaswi N K80252d22015-10-14 23:34:23 +053085 for (int i = 0; i < len; i++) {
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053086 tmp = cb.readInt();
87 pfxRouteTag.add(new Integer(tmp));
Thejaswi N K80252d22015-10-14 23:34:23 +053088 }
89
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053090 return BgpPrefixAttrRouteTag.of(pfxRouteTag);
Thejaswi N K80252d22015-10-14 23:34:23 +053091 }
92
93 /**
94 * Returns the prefix route tag.
95 *
96 * @return route tag
97 */
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053098 public List<Integer> getPfxRouteTag() {
Thejaswi N K80252d22015-10-14 23:34:23 +053099 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 }
Priyanka B02040732015-11-29 11:30:29 +0530136
137 @Override
138 public int compareTo(Object o) {
139 // TODO Auto-generated method stub
140 return 0;
141 }
Thejaswi N K80252d22015-10-14 23:34:23 +0530142}