Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 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 | */ |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 16 | package org.onosproject.bgpio.protocol.linkstate; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 17 | |
| 18 | import java.util.Iterator; |
| 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 21 | import java.util.ListIterator; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
| 24 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 26 | import org.onosproject.bgpio.types.BgpErrorType; |
| 27 | import org.onosproject.bgpio.types.BgpValueType; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 28 | import org.onosproject.bgpio.types.IPv4AddressTlv; |
| 29 | import org.onosproject.bgpio.types.IPv6AddressTlv; |
| 30 | import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv; |
| 31 | import org.onosproject.bgpio.types.attr.BgpAttrNodeMultiTopologyId; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 32 | import org.onosproject.bgpio.util.Constants; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.util.UnSupportedAttribute; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import com.google.common.base.MoreObjects; |
| 38 | import com.google.common.base.Preconditions; |
| 39 | |
| 40 | /** |
| 41 | * Implementation of local node descriptors, remote node descriptors and link descriptors. |
| 42 | */ |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 43 | public class BgpLinkLSIdentifier implements Comparable<Object> { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 44 | private static final Logger log = LoggerFactory.getLogger(BgpLinkLSIdentifier.class); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 45 | public static final short IPV4_INTERFACE_ADDRESS_TYPE = 259; |
| 46 | public static final short IPV4_NEIGHBOR_ADDRESS_TYPE = 260; |
| 47 | public static final short IPV6_INTERFACE_ADDRESS_TYPE = 261; |
| 48 | public static final short IPV6_NEIGHBOR_ADDRESS_TYPE = 262; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 49 | |
| 50 | private NodeDescriptors localNodeDescriptors; |
| 51 | private NodeDescriptors remoteNodeDescriptors; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 52 | private List<BgpValueType> linkDescriptor; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Initialize fields. |
| 56 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 57 | public BgpLinkLSIdentifier() { |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 58 | this.localNodeDescriptors = null; |
| 59 | this.remoteNodeDescriptors = null; |
| 60 | this.linkDescriptor = null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Constructors to initialize parameters. |
| 65 | * |
| 66 | * @param localNodeDescriptors local node descriptors |
| 67 | * @param remoteNodeDescriptors remote node descriptors |
| 68 | * @param linkDescriptor link descriptors |
| 69 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | public BgpLinkLSIdentifier(NodeDescriptors localNodeDescriptors, NodeDescriptors remoteNodeDescriptors, |
| 71 | LinkedList<BgpValueType> linkDescriptor) { |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 72 | this.localNodeDescriptors = Preconditions.checkNotNull(localNodeDescriptors); |
| 73 | this.remoteNodeDescriptors = Preconditions.checkNotNull(remoteNodeDescriptors); |
| 74 | this.linkDescriptor = Preconditions.checkNotNull(linkDescriptor); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Reads channel buffer and parses link identifier. |
| 79 | * |
| 80 | * @param cb ChannelBuffer |
| 81 | * @param protocolId in linkstate nlri |
| 82 | * @return object of BGPLinkLSIdentifier |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 83 | * @throws BgpParseException while parsing link identifier |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 84 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 85 | public static BgpLinkLSIdentifier parseLinkIdendifier(ChannelBuffer cb, byte protocolId) throws BgpParseException { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 86 | log.debug("Parse local node descriptor"); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 87 | NodeDescriptors localNodeDescriptors = new NodeDescriptors(); |
| 88 | localNodeDescriptors = parseNodeDescriptors(cb, NodeDescriptors.LOCAL_NODE_DES_TYPE, protocolId); |
| 89 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 90 | log.debug("Parse remote node descriptor"); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 91 | NodeDescriptors remoteNodeDescriptors = new NodeDescriptors(); |
| 92 | remoteNodeDescriptors = parseNodeDescriptors(cb, NodeDescriptors.REMOTE_NODE_DES_TYPE, protocolId); |
| 93 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 94 | log.debug("Parse link descriptor"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 95 | LinkedList<BgpValueType> linkDescriptor = new LinkedList<>(); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 96 | linkDescriptor = parseLinkDescriptors(cb); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 97 | return new BgpLinkLSIdentifier(localNodeDescriptors, remoteNodeDescriptors, linkDescriptor); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Parses Local/Remote node descriptors. |
| 102 | * |
| 103 | * @param cb ChannelBuffer |
| 104 | * @param desType descriptor type |
| 105 | * @param protocolId protocol identifier |
| 106 | * @return object of NodeDescriptors |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 107 | * @throws BgpParseException while parsing Local/Remote node descriptors |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 108 | */ |
| 109 | public static NodeDescriptors parseNodeDescriptors(ChannelBuffer cb, short desType, byte protocolId) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 110 | throws BgpParseException { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 111 | log.debug("Parse node descriptors"); |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 112 | ChannelBuffer tempBuf = cb.copy(); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 113 | short type = cb.readShort(); |
| 114 | short length = cb.readShort(); |
| 115 | if (cb.readableBytes() < length) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 116 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 117 | tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN_AS_SHORT)); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 118 | } |
| 119 | NodeDescriptors nodeIdentifier = new NodeDescriptors(); |
| 120 | ChannelBuffer tempCb = cb.readBytes(length); |
| 121 | |
| 122 | if (type == desType) { |
| 123 | nodeIdentifier = NodeDescriptors.read(tempCb, length, desType, protocolId); |
| 124 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 125 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 126 | } |
| 127 | return nodeIdentifier; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Parses link descriptors. |
| 132 | * |
| 133 | * @param cb ChannelBuffer |
| 134 | * @return list of link descriptors |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 135 | * @throws BgpParseException while parsing link descriptors |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 136 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 137 | public static LinkedList<BgpValueType> parseLinkDescriptors(ChannelBuffer cb) throws BgpParseException { |
| 138 | LinkedList<BgpValueType> linkDescriptor = new LinkedList<>(); |
| 139 | BgpValueType tlv = null; |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 140 | int count = 0; |
| 141 | |
| 142 | while (cb.readableBytes() > 0) { |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 143 | ChannelBuffer tempBuf = cb.copy(); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 144 | short type = cb.readShort(); |
| 145 | short length = cb.readShort(); |
| 146 | if (cb.readableBytes() < length) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 147 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 148 | tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN_AS_SHORT)); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 149 | } |
| 150 | ChannelBuffer tempCb = cb.readBytes(length); |
| 151 | switch (type) { |
| 152 | case LinkLocalRemoteIdentifiersTlv.TYPE: |
| 153 | tlv = LinkLocalRemoteIdentifiersTlv.read(tempCb); |
| 154 | break; |
| 155 | case IPV4_INTERFACE_ADDRESS_TYPE: |
| 156 | tlv = IPv4AddressTlv.read(tempCb, IPV4_INTERFACE_ADDRESS_TYPE); |
| 157 | break; |
| 158 | case IPV4_NEIGHBOR_ADDRESS_TYPE: |
| 159 | tlv = IPv4AddressTlv.read(tempCb, IPV4_NEIGHBOR_ADDRESS_TYPE); |
| 160 | break; |
| 161 | case IPV6_INTERFACE_ADDRESS_TYPE: |
| 162 | tlv = IPv6AddressTlv.read(tempCb, IPV6_INTERFACE_ADDRESS_TYPE); |
| 163 | break; |
| 164 | case IPV6_NEIGHBOR_ADDRESS_TYPE: |
| 165 | tlv = IPv6AddressTlv.read(tempCb, IPV6_NEIGHBOR_ADDRESS_TYPE); |
| 166 | break; |
| 167 | case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY: |
| 168 | tlv = BgpAttrNodeMultiTopologyId.read(tempCb); |
vivek | 7076e41 | 2017-01-04 12:29:52 +0530 | [diff] [blame] | 169 | count++; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 170 | log.debug("MultiTopologyId TLV cannot repeat more than once"); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 171 | if (count > 1) { |
| 172 | //length + 4 implies data contains type, length and value |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 173 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 174 | BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, tempBuf.readBytes(length |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 175 | + Constants.TYPE_AND_LEN_AS_SHORT)); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 176 | } |
| 177 | break; |
| 178 | default: |
| 179 | UnSupportedAttribute.skipBytes(tempCb, length); |
| 180 | } |
| 181 | linkDescriptor.add(tlv); |
| 182 | } |
| 183 | return linkDescriptor; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns local node descriptors. |
| 188 | * |
| 189 | * @return local node descriptors |
| 190 | */ |
| 191 | public NodeDescriptors localNodeDescriptors() { |
| 192 | return this.localNodeDescriptors; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns remote node descriptors. |
| 197 | * |
| 198 | * @return remote node descriptors |
| 199 | */ |
| 200 | public NodeDescriptors remoteNodeDescriptors() { |
| 201 | return this.remoteNodeDescriptors; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns link descriptors. |
| 206 | * |
| 207 | * @return link descriptors |
| 208 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 209 | public List<BgpValueType> linkDescriptors() { |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 210 | return this.linkDescriptor; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public int hashCode() { |
| 215 | return Objects.hash(linkDescriptor, localNodeDescriptors, remoteNodeDescriptors); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public boolean equals(Object obj) { |
| 220 | if (this == obj) { |
| 221 | return true; |
| 222 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 223 | if (obj instanceof BgpLinkLSIdentifier) { |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 224 | int countObjSubTlv = 0; |
| 225 | int countOtherSubTlv = 0; |
| 226 | boolean isCommonSubTlv = true; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 227 | BgpLinkLSIdentifier other = (BgpLinkLSIdentifier) obj; |
| 228 | Iterator<BgpValueType> objListIterator = other.linkDescriptor.iterator(); |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 229 | countOtherSubTlv = other.linkDescriptor.size(); |
| 230 | countObjSubTlv = linkDescriptor.size(); |
| 231 | if (countObjSubTlv != countOtherSubTlv) { |
| 232 | return false; |
| 233 | } else { |
| 234 | while (objListIterator.hasNext() && isCommonSubTlv) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 235 | BgpValueType subTlv = objListIterator.next(); |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 236 | if (linkDescriptor.contains(subTlv) && other.linkDescriptor.contains(subTlv)) { |
| 237 | isCommonSubTlv = Objects.equals(linkDescriptor.get(linkDescriptor.indexOf(subTlv)), |
| 238 | other.linkDescriptor.get(other.linkDescriptor.indexOf(subTlv))); |
| 239 | } else { |
| 240 | isCommonSubTlv = false; |
| 241 | } |
Priyanka B | 25fad14 | 2015-10-14 12:17:13 +0530 | [diff] [blame] | 242 | } |
| 243 | return isCommonSubTlv && Objects.equals(this.localNodeDescriptors, other.localNodeDescriptors) |
| 244 | && Objects.equals(this.remoteNodeDescriptors, other.remoteNodeDescriptors); |
| 245 | } |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public String toString() { |
| 252 | return MoreObjects.toStringHelper(getClass()) |
| 253 | .add("localNodeDescriptors", localNodeDescriptors) |
| 254 | .add("remoteNodeDescriptors", remoteNodeDescriptors) |
| 255 | .add("linkDescriptor", linkDescriptor) |
| 256 | .toString(); |
| 257 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 258 | |
| 259 | @Override |
| 260 | public int compareTo(Object o) { |
| 261 | if (this.equals(o)) { |
| 262 | return 0; |
| 263 | } |
Priyanka B | 897c62a | 2015-12-13 13:37:07 +0530 | [diff] [blame] | 264 | boolean tlvFound = false; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 265 | int result = this.localNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).localNodeDescriptors); |
| 266 | if (result != 0) { |
| 267 | return result; |
| 268 | } else if (this.remoteNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).remoteNodeDescriptors) != 0) { |
| 269 | return this.remoteNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).remoteNodeDescriptors); |
| 270 | } else { |
| 271 | int countOtherSubTlv = ((BgpLinkLSIdentifier) o).linkDescriptor.size(); |
| 272 | int countObjSubTlv = linkDescriptor.size(); |
| 273 | if (countOtherSubTlv != countObjSubTlv) { |
| 274 | if (countOtherSubTlv > countObjSubTlv) { |
| 275 | return 1; |
| 276 | } else { |
| 277 | return -1; |
| 278 | } |
Priyanka B | 897c62a | 2015-12-13 13:37:07 +0530 | [diff] [blame] | 279 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 280 | ListIterator<BgpValueType> listIterator = linkDescriptor.listIterator(); |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 281 | while (listIterator.hasNext()) { |
Priyanka B | 897c62a | 2015-12-13 13:37:07 +0530 | [diff] [blame] | 282 | BgpValueType tlv1 = listIterator.next(); |
| 283 | for (BgpValueType tlv : ((BgpLinkLSIdentifier) o).linkDescriptor) { |
| 284 | if (tlv.getType() == tlv1.getType()) { |
| 285 | result = linkDescriptor.get(linkDescriptor.indexOf(tlv1)).compareTo( |
| 286 | ((BgpLinkLSIdentifier) o).linkDescriptor.get(((BgpLinkLSIdentifier) o).linkDescriptor |
| 287 | .indexOf(tlv))); |
| 288 | if (result != 0) { |
| 289 | return result; |
| 290 | } |
| 291 | tlvFound = true; |
| 292 | break; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 293 | } |
Priyanka B | 897c62a | 2015-12-13 13:37:07 +0530 | [diff] [blame] | 294 | } |
| 295 | if (!tlvFound) { |
Shashikanth VH | eacbed5 | 2015-12-02 22:37:21 +0530 | [diff] [blame] | 296 | return 1; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | return 0; |
| 301 | } |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 302 | } |