blob: f6aaea606f81c48cc5f712df574902247eb04a19 [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;
25import org.onosproject.bgpio.types.BgpErrorType;
26import org.onosproject.bgpio.types.BgpValueType;
Priyanka Bb2988fa2015-10-09 12:45:36 +053027import org.onosproject.bgpio.types.IPReachabilityInformationTlv;
28import org.onosproject.bgpio.types.OSPFRouteTypeTlv;
29import org.onosproject.bgpio.types.attr.BgpAttrNodeMultiTopologyId;
30import org.onosproject.bgpio.util.UnSupportedAttribute;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import com.google.common.base.MoreObjects;
35
36/**
37 * Provides Implementation of Local node descriptors and prefix descriptors.
38 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053039public class BgpPrefixLSIdentifier {
Priyanka Bb2988fa2015-10-09 12:45:36 +053040
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053041 protected static final Logger log = LoggerFactory.getLogger(BgpPrefixLSIdentifier.class);
Priyanka Bb2988fa2015-10-09 12:45:36 +053042 public static final int TYPE_AND_LEN = 4;
43 private NodeDescriptors localNodeDescriptors;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053044 private LinkedList<BgpValueType> prefixDescriptor;
Priyanka Bb2988fa2015-10-09 12:45:36 +053045
46 /**
47 * Resets parameters.
48 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049 public BgpPrefixLSIdentifier() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053050 this.localNodeDescriptors = null;
51 this.prefixDescriptor = null;
52 }
53
54 /**
55 * Constructor to initialize parameters.
56 *
57 * @param localNodeDescriptors Local node descriptors
58 * @param prefixDescriptor Prefix Descriptors
59 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060 public BgpPrefixLSIdentifier(NodeDescriptors localNodeDescriptors, LinkedList<BgpValueType> prefixDescriptor) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053061 this.localNodeDescriptors = localNodeDescriptors;
62 this.prefixDescriptor = prefixDescriptor;
63 }
64
65 /**
66 * Reads the channel buffer and parses Prefix Identifier.
67 *
68 * @param cb ChannelBuffer
69 * @param protocolId protocol ID
70 * @return object of this class
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 * @throws BgpParseException while parsing Prefix Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053072 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 public static BgpPrefixLSIdentifier parsePrefixIdendifier(ChannelBuffer cb, byte protocolId)
74 throws BgpParseException {
Priyanka Bb2988fa2015-10-09 12:45:36 +053075 //Parse Local Node descriptor
76 NodeDescriptors localNodeDescriptors = new NodeDescriptors();
77 localNodeDescriptors = parseLocalNodeDescriptors(cb, protocolId);
78
79 //Parse Prefix descriptor
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 LinkedList<BgpValueType> prefixDescriptor = new LinkedList<>();
Priyanka Bb2988fa2015-10-09 12:45:36 +053081 prefixDescriptor = parsePrefixDescriptors(cb);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053082 return new BgpPrefixLSIdentifier(localNodeDescriptors, prefixDescriptor);
Priyanka Bb2988fa2015-10-09 12:45:36 +053083 }
84
85 /**
86 * Parse local node descriptors.
87 *
88 * @param cb ChannelBuffer
89 * @param protocolId protocol identifier
90 * @return LocalNodeDescriptors
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053091 * @throws BgpParseException while parsing local node descriptors
Priyanka Bb2988fa2015-10-09 12:45:36 +053092 */
93 public static NodeDescriptors parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 throws BgpParseException {
Priyanka Bb2988fa2015-10-09 12:45:36 +053095 ChannelBuffer tempBuf = cb;
96 short type = cb.readShort();
97 short length = cb.readShort();
98 if (cb.readableBytes() < length) {
99 //length + 4 implies data contains type, length and value
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530100 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
Priyanka Bb2988fa2015-10-09 12:45:36 +0530101 tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
102 }
103 NodeDescriptors localNodeDescriptors = new NodeDescriptors();
104 ChannelBuffer tempCb = cb.readBytes(length);
105
106 if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) {
107 localNodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId);
108 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530109 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
110 BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530111 }
112 return localNodeDescriptors;
113 }
114
115 /**
116 * Parse list of prefix descriptors.
117 *
118 * @param cb ChannelBuffer
119 * @return list of prefix descriptors
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530120 * @throws BgpParseException while parsing list of prefix descriptors
Priyanka Bb2988fa2015-10-09 12:45:36 +0530121 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530122 public static LinkedList<BgpValueType> parsePrefixDescriptors(ChannelBuffer cb) throws BgpParseException {
123 LinkedList<BgpValueType> prefixDescriptor = new LinkedList<>();
124 BgpValueType tlv = null;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530125 boolean isIpReachInfo = false;
126 ChannelBuffer tempCb;
127 int count = 0;
128
129 while (cb.readableBytes() > 0) {
130 ChannelBuffer tempBuf = cb;
131 short type = cb.readShort();
132 short length = cb.readShort();
133 if (cb.readableBytes() < length) {
134 //length + 4 implies data contains type, length and value
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530135 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
Priyanka Bb2988fa2015-10-09 12:45:36 +0530136 tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
137 }
138 tempCb = cb.readBytes(length);
139 switch (type) {
140 case OSPFRouteTypeTlv.TYPE:
141 tlv = OSPFRouteTypeTlv.read(tempCb);
142 break;
143 case IPReachabilityInformationTlv.TYPE:
144 tlv = IPReachabilityInformationTlv.read(tempCb, length);
145 isIpReachInfo = true;
146 break;
147 case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY:
148 tlv = BgpAttrNodeMultiTopologyId.read(tempCb);
149 count = count + 1;
150 if (count > 1) {
151 //length + 4 implies data contains type, length and value
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530152 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
153 BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, tempBuf.readBytes(length + TYPE_AND_LEN));
Priyanka Bb2988fa2015-10-09 12:45:36 +0530154 }
155 break;
156 default:
157 UnSupportedAttribute.skipBytes(tempCb, length);
158 }
159 prefixDescriptor.add(tlv);
160 }
161
162 if (!isIpReachInfo) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530163 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
Priyanka Bb2988fa2015-10-09 12:45:36 +0530164 null);
165 }
166 return prefixDescriptor;
167 }
168
169 /**
170 * Returns local node descriptors.
171 *
172 * @return local node descriptors
173 */
174 public NodeDescriptors getLocalNodeDescriptors() {
175 return this.localNodeDescriptors;
176 }
177
178 /**
179 * Returns Prefix descriptors.
180 *
181 * @return Prefix descriptors
182 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530183 public LinkedList<BgpValueType> getPrefixdescriptor() {
Priyanka Bb2988fa2015-10-09 12:45:36 +0530184 return this.prefixDescriptor;
185 }
186
187 @Override
188 public int hashCode() {
189 return Objects.hash(prefixDescriptor.hashCode(), localNodeDescriptors);
190 }
191
192 @Override
193 public boolean equals(Object obj) {
194 if (this == obj) {
195 return true;
196 }
197
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530198 if (obj instanceof BgpPrefixLSIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +0530199 int countObjSubTlv = 0;
200 int countOtherSubTlv = 0;
201 boolean isCommonSubTlv = true;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530202 BgpPrefixLSIdentifier other = (BgpPrefixLSIdentifier) obj;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530203
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530204 Iterator<BgpValueType> objListIterator = other.prefixDescriptor.iterator();
Priyanka Bb2988fa2015-10-09 12:45:36 +0530205 countOtherSubTlv = other.prefixDescriptor.size();
206 countObjSubTlv = prefixDescriptor.size();
207 if (countObjSubTlv != countOtherSubTlv) {
208 return false;
209 } else {
210 while (objListIterator.hasNext() && isCommonSubTlv) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530211 BgpValueType subTlv = objListIterator.next();
Priyanka Bb2988fa2015-10-09 12:45:36 +0530212 isCommonSubTlv = Objects.equals(prefixDescriptor.contains(subTlv),
213 other.prefixDescriptor.contains(subTlv));
214 }
215 return isCommonSubTlv && Objects.equals(this.localNodeDescriptors, other.localNodeDescriptors);
216 }
217 }
218 return false;
219 }
220
221 @Override
222 public String toString() {
223 return MoreObjects.toStringHelper(getClass())
224 .add("localNodeDescriptors", localNodeDescriptors)
225 .add("prefixDescriptor", prefixDescriptor)
226 .toString();
227 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800228}