blob: a075d74fdf81f590e8e7078cc481dec8f158957a [file] [log] [blame]
Thejaswi NKe31bb102015-10-15 11:21:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thejaswi NKe31bb102015-10-15 11:21:12 +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
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.ArrayList;
21import java.util.List;
22import java.util.Objects;
23
24import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgpio.exceptions.BgpParseException;
26import org.onosproject.bgpio.types.BgpErrorType;
27import org.onosproject.bgpio.types.BgpValueType;
Thejaswi NKe31bb102015-10-15 11:21:12 +053028import org.onosproject.bgpio.util.Validation;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import com.google.common.base.MoreObjects;
33
34/**
35 * Implements BGP prefix route Extended tag attribute.
36 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053037public class BgpPrefixAttrExtRouteTag implements BgpValueType {
Thejaswi NKe31bb102015-10-15 11:21:12 +053038
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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 * @throws BgpParseException while parsing BgpPrefixAttrExtRouteTag
Thejaswi NKe31bb102015-10-15 11:21:12 +053073 */
74 public static BgpPrefixAttrExtRouteTag read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 throws BgpParseException {
Thejaswi NKe31bb102015-10-15 11:21:12 +053076 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) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
84 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi NKe31bb102015-10-15 11:21:12 +053085 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 }
Priyanka B02040732015-11-29 11:30:29 +0530139
140 @Override
141 public int compareTo(Object o) {
142 // TODO Auto-generated method stub
143 return 0;
144 }
Thejaswi NKe31bb102015-10-15 11:21:12 +0530145}