blob: 275b85beb8ec6575144e878c5acb5b979734545e [file] [log] [blame]
Thejaswi NK00bc8c92015-10-14 19:37: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 NK00bc8c92015-10-14 19:37: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 link opaque attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public final class BgpLinkAttrOpaqLnkAttrib implements BgpValueType {
Thejaswi NK00bc8c92015-10-14 19:37:14 +053034
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrOpaqLnkAttrib.class);
37
38 public static final int ATTRNODE_OPAQUELNKATTRIB = 1097;
39
40 /* Opaque Node Attribute */
41 private final byte[] opaqueLinkAttribute;
42
43 /**
44 * Constructor to initialize the data.
45 *
46 * @param opaqueLinkAttribute opaque link attribute
47 */
48 private BgpLinkAttrOpaqLnkAttrib(byte[] opaqueLinkAttribute) {
49 this.opaqueLinkAttribute = Arrays.copyOf(opaqueLinkAttribute,
50 opaqueLinkAttribute.length);
51 }
52
53 /**
54 * Returns object of this class with specified values.
55 *
56 * @param opaqueLinkAttribute opaque link attribute
57 * @return object of BgpLinkAttrOpaqLnkAttrib
58 */
59 public static BgpLinkAttrOpaqLnkAttrib of(final byte[] opaqueLinkAttribute) {
60 return new BgpLinkAttrOpaqLnkAttrib(opaqueLinkAttribute);
61 }
62
63 /**
64 * Reads the BGP link attributes Opaque link attribute.
65 *
66 * @param cb Channel buffer
67 * @return object of type BgpLinkAttrOpaqLnkAttrib
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 * @throws BgpParseException while parsing BgpLinkAttrOpaqLnkAttrib
Thejaswi NK00bc8c92015-10-14 19:37:14 +053069 */
70 public static BgpLinkAttrOpaqLnkAttrib read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 throws BgpParseException {
Thejaswi NK00bc8c92015-10-14 19:37:14 +053072
73 byte[] opaqueLinkAttribute;
74
75 short lsAttrLength = cb.readShort();
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 NK00bc8c92015-10-14 19:37:14 +053080 lsAttrLength);
81 }
82
83 opaqueLinkAttribute = new byte[lsAttrLength];
84 cb.readBytes(opaqueLinkAttribute);
85
86 return BgpLinkAttrOpaqLnkAttrib.of(opaqueLinkAttribute);
87 }
88
89 /**
90 * Returns the Opaque link attribute.
91 *
92 * @return byte array of opaque link attribute.
93 */
94 public byte[] attrOpaqueLnk() {
95 return opaqueLinkAttribute;
96 }
97
98 @Override
99 public short getType() {
100 return ATTRNODE_OPAQUELNKATTRIB;
101 }
102
103 @Override
104 public int hashCode() {
105 return Arrays.hashCode(opaqueLinkAttribute);
106 }
107
108 @Override
109 public boolean equals(Object obj) {
110 if (this == obj) {
111 return true;
112 }
113
114 if (obj instanceof BgpLinkAttrOpaqLnkAttrib) {
115 BgpLinkAttrOpaqLnkAttrib other = (BgpLinkAttrOpaqLnkAttrib) obj;
116 return Arrays
117 .equals(opaqueLinkAttribute, other.opaqueLinkAttribute);
118 }
119 return false;
120 }
121
122 @Override
123 public int write(ChannelBuffer cb) {
124 // TODO This will be implemented in the next version
125 return 0;
126 }
127
128 @Override
129 public String toString() {
130 return MoreObjects.toStringHelper(getClass()).omitNullValues()
131 .add("opaqueLinkAttribute", opaqueLinkAttribute).toString();
132 }
Priyanka B02040732015-11-29 11:30:29 +0530133
134 @Override
135 public int compareTo(Object o) {
136 // TODO Auto-generated method stub
137 return 0;
138 }
Thejaswi NK00bc8c92015-10-14 19:37:14 +0530139}