blob: 6f7a74bbcecc6d161640a221552b65f4d0c9ef00 [file] [log] [blame]
Thejaswi N K34c18432015-10-15 15:33:14 +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
18import java.util.Arrays;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
23import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N K34c18432015-10-15 15:33:14 +053024import org.onosproject.bgpio.util.Validation;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implements BGP prefix opaque data attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public final class BgpPrefixAttrOpaqueData implements BgpValueType {
Thejaswi N K34c18432015-10-15 15:33:14 +053034
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpPrefixAttrOpaqueData.class);
37
38 public static final int ATTR_PREFIX_OPAQUEDATA = 1157;
39
40 /* Opaque Node Attribute */
41 private final byte[] opaquePrefixAttribute;
42
43 /**
44 * Constructor to initialize the values.
45 *
46 * @param opaquePrefixAttribute opaque prefix data
47 */
48 public BgpPrefixAttrOpaqueData(byte[] opaquePrefixAttribute) {
49 this.opaquePrefixAttribute = Arrays
50 .copyOf(opaquePrefixAttribute, opaquePrefixAttribute.length);
51 }
52
53 /**
54 * Returns object of this class with specified values.
55 *
56 * @param opaquePrefixAttribute opaque prefix data
57 * @return object of BgpPrefixAttrOpaqueData
58 */
59 public static BgpPrefixAttrOpaqueData of(final byte[] opaquePrefixAttribute) {
60 return new BgpPrefixAttrOpaqueData(opaquePrefixAttribute);
61 }
62
63 /**
64 * Reads the Opaque Prefix Attribute.
65 *
66 * @param cb ChannelBuffer
67 * @return object of BgpPrefixAttrOpaqueData
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 * @throws BgpParseException while parsing BgpPrefixAttrOpaqueData
Thejaswi N K34c18432015-10-15 15:33:14 +053069 */
70 public static BgpPrefixAttrOpaqueData read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 throws BgpParseException {
Thejaswi N K34c18432015-10-15 15:33:14 +053072 byte[] opaquePrefixAttribute;
73
74 short lsAttrLength = cb.readShort();
75 opaquePrefixAttribute = new byte[lsAttrLength];
76
77 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
79 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K34c18432015-10-15 15:33:14 +053080 lsAttrLength);
81 }
82
83 cb.readBytes(opaquePrefixAttribute);
84
85 return BgpPrefixAttrOpaqueData.of(opaquePrefixAttribute);
86 }
87
88 /**
89 * Returns the Opaque prefix attribute name.
90 *
91 * @return opaque prefix name
92 */
93 public byte[] getOpaquePrefixAttribute() {
94 return opaquePrefixAttribute;
95 }
96
97 @Override
98 public short getType() {
99 return ATTR_PREFIX_OPAQUEDATA;
100 }
101
102 @Override
103 public int hashCode() {
104 return Arrays.hashCode(opaquePrefixAttribute);
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112
113 if (obj instanceof BgpPrefixAttrOpaqueData) {
114 BgpPrefixAttrOpaqueData other = (BgpPrefixAttrOpaqueData) obj;
115 return Arrays.equals(opaquePrefixAttribute,
116 other.opaquePrefixAttribute);
117 }
118 return false;
119 }
120
121 @Override
122 public int write(ChannelBuffer cb) {
123 // TODO This will be implemented in the next version
124 return 0;
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(getClass()).omitNullValues()
130 .add("opaquePrefixAttribute", getOpaquePrefixAttribute())
131 .toString();
132 }
133
Priyanka B02040732015-11-29 11:30:29 +0530134 @Override
135 public int compareTo(Object o) {
136 // TODO Auto-generated method stub
137 return 0;
138 }
Thejaswi N K34c18432015-10-15 15:33:14 +0530139}