2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.types.attr;
18 import java.util.Arrays;
19 import java.util.Objects;
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.onosproject.bgpio.exceptions.BGPParseException;
23 import org.onosproject.bgpio.types.BGPErrorType;
24 import org.onosproject.bgpio.types.BGPValueType;
25 import org.onosproject.bgpio.util.Validation;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.common.base.MoreObjects;
32 * Implements BGP prefix route tag attribute.
34 public class BgpPrefixAttrRouteTag implements BGPValueType {
36 protected static final Logger log = LoggerFactory
37 .getLogger(BgpPrefixAttrRouteTag.class);
39 public static final int ATTR_PREFIX_ROUTETAG = 1153;
41 /* Prefix Route Tag */
42 private int[] pfxRouteTag;
45 * Constructor to initialize the values.
47 * @param pfxRouteTag prefix route tag
49 BgpPrefixAttrRouteTag(int[] pfxRouteTag) {
50 this.pfxRouteTag = Arrays.copyOf(pfxRouteTag, pfxRouteTag.length);
54 * Reads the Route Tag.
56 * @param cb ChannelBuffer
57 * @return object of BgpPrefixAttrRouteTag
58 * @throws BGPParseException while parsing BgpPrefixAttrRouteTag
60 public static BgpPrefixAttrRouteTag read(ChannelBuffer cb)
61 throws BGPParseException {
64 short lsAttrLength = cb.readShort();
65 int len = lsAttrLength / Integer.SIZE;
67 if (cb.readableBytes() < lsAttrLength) {
68 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
69 BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
73 pfxRouteTag = new int[lsAttrLength];
75 for (int i = 0; i < len; i++) {
76 pfxRouteTag[i] = cb.readInt();
79 return new BgpPrefixAttrRouteTag(pfxRouteTag);
83 * Returns the prefix route tag.
87 int[] getPfxRouteTag() {
92 public short getType() {
93 return ATTR_PREFIX_ROUTETAG;
97 public int hashCode() {
98 return Objects.hash(pfxRouteTag);
102 public boolean equals(Object obj) {
107 if (obj instanceof BgpPrefixAttrRouteTag) {
108 BgpPrefixAttrRouteTag other = (BgpPrefixAttrRouteTag) obj;
109 return Objects.equals(pfxRouteTag, other.pfxRouteTag);
115 public int write(ChannelBuffer cb) {
116 // TODO This will be implemented in the next version
121 public String toString() {
122 return MoreObjects.toStringHelper(getClass()).omitNullValues()
123 .add("pfxRouteTag", pfxRouteTag).toString();