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.
17 package org.onosproject.bgpio.protocol.link_state;
19 import java.util.Iterator;
20 import java.util.LinkedList;
21 import java.util.Objects;
23 import org.jboss.netty.buffer.ChannelBuffer;
24 import org.onosproject.bgpio.exceptions.BGPParseException;
25 import org.onosproject.bgpio.types.AreaIDTlv;
26 import org.onosproject.bgpio.types.AutonomousSystemTlv;
27 import org.onosproject.bgpio.types.BGPErrorType;
28 import org.onosproject.bgpio.types.BGPLSIdentifierTlv;
29 import org.onosproject.bgpio.types.BGPValueType;
30 import org.onosproject.bgpio.types.IsIsNonPseudonode;
31 import org.onosproject.bgpio.types.IsIsPseudonode;
32 import org.onosproject.bgpio.types.OSPFNonPseudonode;
33 import org.onosproject.bgpio.types.OSPFPseudonode;
34 import org.onosproject.bgpio.util.UnSupportedAttribute;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.MoreObjects;
41 * Provides Local and Remote NodeDescriptors which contains Node Descriptor Sub-TLVs.
43 public class NodeDescriptors {
46 *Reference :draft-ietf-idr-ls-distribution-11
48 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 // Node Descriptor Sub-TLVs (variable) //
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 Figure : Local or Remote Node Descriptors TLV format
60 protected static final Logger log = LoggerFactory.getLogger(NodeDescriptors.class);
62 public static final short LOCAL_NODE_DES_TYPE = 256;
63 public static final short REMOTE_NODE_DES_TYPE = 257;
64 public static final short IGP_ROUTERID_TYPE = 515;
65 public static final short IS_IS_LEVEL_1_PROTOCOL_ID = 1;
66 public static final short IS_IS_LEVEL_2_PROTOCOL_ID = 2;
67 public static final short OSPF_V2_PROTOCOL_ID = 3;
68 public static final short OSPF_V3_PROTOCOL_ID = 6;
69 public static final int TYPE_AND_LEN = 4;
70 public static final int ISISNONPSEUDONODE_LEN = 6;
71 public static final int ISISPSEUDONODE_LEN = 7;
72 public static final int OSPFNONPSEUDONODE_LEN = 4;
73 public static final int OSPFPSEUDONODE_LEN = 8;
74 private LinkedList<BGPValueType> subTlvs;
75 private short deslength;
76 private short desType;
81 public NodeDescriptors() {
88 * Constructor to initialize parameters.
90 * @param subTlvs list of subTlvs
91 * @param deslength Descriptors length
92 * @param desType local node descriptor or remote node descriptor type
94 public NodeDescriptors(LinkedList<BGPValueType> subTlvs, short deslength, short desType) {
95 this.subTlvs = subTlvs;
96 this.deslength = deslength;
97 this.desType = desType;
101 * Returns list of subTlvs.
103 * @return subTlvs list of subTlvs
105 public LinkedList<BGPValueType> getSubTlvs() {
110 public int hashCode() {
111 return Objects.hash(subTlvs.hashCode());
115 public boolean equals(Object obj) {
120 if (obj instanceof NodeDescriptors) {
121 int countObjSubTlv = 0;
122 int countOtherSubTlv = 0;
123 boolean isCommonSubTlv = true;
124 NodeDescriptors other = (NodeDescriptors) obj;
125 Iterator<BGPValueType> objListIterator = other.subTlvs.iterator();
126 countOtherSubTlv = other.subTlvs.size();
127 countObjSubTlv = subTlvs.size();
128 if (countObjSubTlv != countOtherSubTlv) {
131 while (objListIterator.hasNext() && isCommonSubTlv) {
132 BGPValueType subTlv = objListIterator.next();
133 isCommonSubTlv = Objects.equals(subTlvs.contains(subTlv), other.subTlvs.contains(subTlv));
135 return isCommonSubTlv;
142 * Reads node descriptors Sub-TLVs.
144 * @param cb ChannelBuffer
145 * @param desLength node descriptor length
146 * @param desType local node descriptor or remote node descriptor type
147 * @param protocolId protocol ID
148 * @return object of NodeDescriptors
149 * @throws BGPParseException while parsing node descriptors
151 public static NodeDescriptors read(ChannelBuffer cb, short desLength, short desType, byte protocolId)
152 throws BGPParseException {
153 LinkedList<BGPValueType> subTlvs;
154 subTlvs = new LinkedList<>();
155 BGPValueType tlv = null;
157 while (cb.readableBytes() > 0) {
158 ChannelBuffer tempBuf = cb;
159 short type = cb.readShort();
160 short length = cb.readShort();
161 if (cb.readableBytes() < length) {
162 throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR,
163 tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
165 ChannelBuffer tempCb = cb.readBytes(length);
167 case AutonomousSystemTlv.TYPE:
168 tlv = AutonomousSystemTlv.read(tempCb);
170 case BGPLSIdentifierTlv.TYPE:
171 tlv = BGPLSIdentifierTlv.read(tempCb);
174 tlv = AreaIDTlv.read(tempCb);
176 case IGP_ROUTERID_TYPE:
177 if (protocolId == IS_IS_LEVEL_1_PROTOCOL_ID || protocolId == IS_IS_LEVEL_2_PROTOCOL_ID) {
178 if (length == ISISNONPSEUDONODE_LEN) {
179 tlv = IsIsNonPseudonode.read(tempCb);
180 } else if (length == ISISPSEUDONODE_LEN) {
181 tlv = IsIsPseudonode.read(tempCb);
183 } else if (protocolId == OSPF_V2_PROTOCOL_ID || protocolId == OSPF_V3_PROTOCOL_ID) {
184 if (length == OSPFNONPSEUDONODE_LEN) {
185 tlv = OSPFNonPseudonode.read(tempCb);
186 } else if (length == OSPFPSEUDONODE_LEN) {
187 tlv = OSPFPseudonode.read(tempCb);
192 UnSupportedAttribute.skipBytes(tempCb, length);
196 return new NodeDescriptors(subTlvs, desLength, desType);
200 * Returns node descriptors length.
202 * @return node descriptors length
204 public short getLength() {
205 return this.deslength;
209 * Returns node descriptors type.
211 * @return node descriptors type
213 public short getType() {
218 public String toString() {
219 return MoreObjects.toStringHelper(getClass())
220 .add("desType", desType)
221 .add("deslength", deslength)
222 .add("subTlvs", subTlvs)