blob: 0581b701ceb1301fb258194806853d97b231da81 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +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 */
16
Jonathan Hart317f4762015-11-09 16:05:36 -080017package org.onosproject.bgpio.protocol.linkstate;
Priyanka Bb2988fa2015-10-09 12:45:36 +053018
19import java.util.Iterator;
20import java.util.LinkedList;
21import java.util.Objects;
22
23import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Bb2988fa2015-10-09 12:45:36 +053025import org.onosproject.bgpio.types.AreaIDTlv;
26import org.onosproject.bgpio.types.AutonomousSystemTlv;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053027import org.onosproject.bgpio.types.BgpErrorType;
28import org.onosproject.bgpio.types.BgpLSIdentifierTlv;
29import org.onosproject.bgpio.types.BgpValueType;
Priyanka Bb2988fa2015-10-09 12:45:36 +053030import org.onosproject.bgpio.types.IsIsNonPseudonode;
31import org.onosproject.bgpio.types.IsIsPseudonode;
32import org.onosproject.bgpio.types.OSPFNonPseudonode;
33import org.onosproject.bgpio.types.OSPFPseudonode;
34import org.onosproject.bgpio.util.UnSupportedAttribute;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import com.google.common.base.MoreObjects;
39
40/**
41 * Provides Local and Remote NodeDescriptors which contains Node Descriptor Sub-TLVs.
42 */
43public class NodeDescriptors {
44
45 /*
46 *Reference :draft-ietf-idr-ls-distribution-11
47 0 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 | Type | Length |
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 | |
53 // Node Descriptor Sub-TLVs (variable) //
54 | |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56
57 Figure : Local or Remote Node Descriptors TLV format
58 */
59
60 protected static final Logger log = LoggerFactory.getLogger(NodeDescriptors.class);
61
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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 private LinkedList<BgpValueType> subTlvs;
Priyanka Bb2988fa2015-10-09 12:45:36 +053075 private short deslength;
76 private short desType;
77
78 /**
79 * Resets parameters.
80 */
81 public NodeDescriptors() {
82 this.subTlvs = null;
83 this.deslength = 0;
84 this.desType = 0;
85 }
86
87 /**
88 * Constructor to initialize parameters.
89 *
90 * @param subTlvs list of subTlvs
91 * @param deslength Descriptors length
92 * @param desType local node descriptor or remote node descriptor type
93 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 public NodeDescriptors(LinkedList<BgpValueType> subTlvs, short deslength, short desType) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053095 this.subTlvs = subTlvs;
96 this.deslength = deslength;
97 this.desType = desType;
98 }
99
100 /**
101 * Returns list of subTlvs.
102 *
103 * @return subTlvs list of subTlvs
104 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530105 public LinkedList<BgpValueType> getSubTlvs() {
Priyanka Bb2988fa2015-10-09 12:45:36 +0530106 return subTlvs;
107 }
108
109 @Override
110 public int hashCode() {
111 return Objects.hash(subTlvs.hashCode());
112 }
113
114 @Override
115 public boolean equals(Object obj) {
116 if (this == obj) {
117 return true;
118 }
119
120 if (obj instanceof NodeDescriptors) {
121 int countObjSubTlv = 0;
122 int countOtherSubTlv = 0;
123 boolean isCommonSubTlv = true;
124 NodeDescriptors other = (NodeDescriptors) obj;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530125 Iterator<BgpValueType> objListIterator = other.subTlvs.iterator();
Priyanka Bb2988fa2015-10-09 12:45:36 +0530126 countOtherSubTlv = other.subTlvs.size();
127 countObjSubTlv = subTlvs.size();
128 if (countObjSubTlv != countOtherSubTlv) {
129 return false;
130 } else {
131 while (objListIterator.hasNext() && isCommonSubTlv) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530132 BgpValueType subTlv = objListIterator.next();
Priyanka Bb2988fa2015-10-09 12:45:36 +0530133 isCommonSubTlv = Objects.equals(subTlvs.contains(subTlv), other.subTlvs.contains(subTlv));
134 }
135 return isCommonSubTlv;
136 }
137 }
138 return false;
139 }
140
141 /**
142 * Reads node descriptors Sub-TLVs.
143 *
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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530149 * @throws BgpParseException while parsing node descriptors
Priyanka Bb2988fa2015-10-09 12:45:36 +0530150 */
151 public static NodeDescriptors read(ChannelBuffer cb, short desLength, short desType, byte protocolId)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530152 throws BgpParseException {
153 LinkedList<BgpValueType> subTlvs;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530154 subTlvs = new LinkedList<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530155 BgpValueType tlv = null;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530156
157 while (cb.readableBytes() > 0) {
158 ChannelBuffer tempBuf = cb;
159 short type = cb.readShort();
160 short length = cb.readShort();
161 if (cb.readableBytes() < length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530162 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
Priyanka Bb2988fa2015-10-09 12:45:36 +0530163 tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
164 }
165 ChannelBuffer tempCb = cb.readBytes(length);
166 switch (type) {
167 case AutonomousSystemTlv.TYPE:
168 tlv = AutonomousSystemTlv.read(tempCb);
169 break;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530170 case BgpLSIdentifierTlv.TYPE:
171 tlv = BgpLSIdentifierTlv.read(tempCb);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530172 break;
173 case AreaIDTlv.TYPE:
174 tlv = AreaIDTlv.read(tempCb);
175 break;
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);
182 }
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);
188 }
189 }
190 break;
191 default:
192 UnSupportedAttribute.skipBytes(tempCb, length);
193 }
194 subTlvs.add(tlv);
195 }
196 return new NodeDescriptors(subTlvs, desLength, desType);
197 }
198
199 /**
200 * Returns node descriptors length.
201 *
202 * @return node descriptors length
203 */
204 public short getLength() {
205 return this.deslength;
206 }
207
208 /**
209 * Returns node descriptors type.
210 *
211 * @return node descriptors type
212 */
213 public short getType() {
214 return this.desType;
215 }
216
217 @Override
218 public String toString() {
219 return MoreObjects.toStringHelper(getClass())
220 .add("desType", desType)
221 .add("deslength", deslength)
222 .add("subTlvs", subTlvs)
223 .toString();
224 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800225}