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