blob: bb0c3bec8afa84ee3f2693a2ccd00bffdf02f937 [file] [log] [blame]
Thejaswi N K80252d22015-10-14 23:34:23 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thejaswi N K80252d22015-10-14 23:34:23 +05303 *
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;
Priyanka B0ab34b92015-12-03 21:13:52 +053041 public static final short SIZE = 4;
Thejaswi N K80252d22015-10-14 23:34:23 +053042
43 /* Prefix Route Tag */
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053044 private List<Integer> pfxRouteTag = new ArrayList<Integer>();
Thejaswi N K80252d22015-10-14 23:34:23 +053045
46 /**
47 * Constructor to initialize the values.
48 *
49 * @param pfxRouteTag prefix route tag
50 */
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053051 public BgpPrefixAttrRouteTag(List<Integer> pfxRouteTag) {
52 this.pfxRouteTag = pfxRouteTag;
53 }
54
55 /**
56 * Returns object of this class with specified values.
57 *
58 * @param pfxRouteTag Prefix Metric
59 * @return object of BgpPrefixAttrRouteTag
60 */
61 public static BgpPrefixAttrRouteTag of(ArrayList<Integer> pfxRouteTag) {
62 return new BgpPrefixAttrRouteTag(pfxRouteTag);
Thejaswi N K80252d22015-10-14 23:34:23 +053063 }
64
65 /**
66 * Reads the Route Tag.
67 *
68 * @param cb ChannelBuffer
69 * @return object of BgpPrefixAttrRouteTag
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053070 * @throws BgpParseException while parsing BgpPrefixAttrRouteTag
Thejaswi N K80252d22015-10-14 23:34:23 +053071 */
72 public static BgpPrefixAttrRouteTag read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 throws BgpParseException {
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053074 int tmp;
75 ArrayList<Integer> pfxRouteTag = new ArrayList<Integer>();
Thejaswi N K80252d22015-10-14 23:34:23 +053076
77 short lsAttrLength = cb.readShort();
Priyanka B0ab34b92015-12-03 21:13:52 +053078 int len = lsAttrLength / SIZE;
Thejaswi N K80252d22015-10-14 23:34:23 +053079
80 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053081 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
82 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K80252d22015-10-14 23:34:23 +053083 lsAttrLength);
84 }
85
Thejaswi N K80252d22015-10-14 23:34:23 +053086 for (int i = 0; i < len; i++) {
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053087 tmp = cb.readInt();
88 pfxRouteTag.add(new Integer(tmp));
Thejaswi N K80252d22015-10-14 23:34:23 +053089 }
90
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053091 return BgpPrefixAttrRouteTag.of(pfxRouteTag);
Thejaswi N K80252d22015-10-14 23:34:23 +053092 }
93
94 /**
95 * Returns the prefix route tag.
96 *
97 * @return route tag
98 */
Thejaswi N Ke79b0ef2015-11-21 20:59:04 +053099 public List<Integer> getPfxRouteTag() {
Thejaswi N K80252d22015-10-14 23:34:23 +0530100 return pfxRouteTag;
101 }
102
103 @Override
104 public short getType() {
105 return ATTR_PREFIX_ROUTETAG;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(pfxRouteTag);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118
119 if (obj instanceof BgpPrefixAttrRouteTag) {
120 BgpPrefixAttrRouteTag other = (BgpPrefixAttrRouteTag) obj;
121 return Objects.equals(pfxRouteTag, other.pfxRouteTag);
122 }
123 return false;
124 }
125
126 @Override
127 public int write(ChannelBuffer cb) {
128 // TODO This will be implemented in the next version
129 return 0;
130 }
131
132 @Override
133 public String toString() {
134 return MoreObjects.toStringHelper(getClass()).omitNullValues()
135 .add("pfxRouteTag", pfxRouteTag).toString();
136 }
Priyanka B02040732015-11-29 11:30:29 +0530137
138 @Override
139 public int compareTo(Object o) {
140 // TODO Auto-generated method stub
141 return 0;
142 }
Thejaswi N K80252d22015-10-14 23:34:23 +0530143}