Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bgpio.protocol.link_state; |
| 18 | |
| 19 | import java.util.Iterator; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.Objects; |
| 22 | |
| 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; |
| 37 | |
| 38 | import com.google.common.base.MoreObjects; |
| 39 | |
| 40 | /** |
| 41 | * Provides Local and Remote NodeDescriptors which contains Node Descriptor Sub-TLVs. |
| 42 | */ |
| 43 | public 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; |
| 74 | private LinkedList<BGPValueType> subTlvs; |
| 75 | 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 | */ |
| 94 | public NodeDescriptors(LinkedList<BGPValueType> subTlvs, short deslength, short desType) { |
| 95 | 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 | */ |
| 105 | public LinkedList<BGPValueType> getSubTlvs() { |
| 106 | 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; |
| 125 | Iterator<BGPValueType> objListIterator = other.subTlvs.iterator(); |
| 126 | countOtherSubTlv = other.subTlvs.size(); |
| 127 | countObjSubTlv = subTlvs.size(); |
| 128 | if (countObjSubTlv != countOtherSubTlv) { |
| 129 | return false; |
| 130 | } else { |
| 131 | while (objListIterator.hasNext() && isCommonSubTlv) { |
| 132 | BGPValueType subTlv = objListIterator.next(); |
| 133 | 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 |
| 149 | * @throws BGPParseException while parsing node descriptors |
| 150 | */ |
| 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; |
| 156 | |
| 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)); |
| 164 | } |
| 165 | ChannelBuffer tempCb = cb.readBytes(length); |
| 166 | switch (type) { |
| 167 | case AutonomousSystemTlv.TYPE: |
| 168 | tlv = AutonomousSystemTlv.read(tempCb); |
| 169 | break; |
| 170 | case BGPLSIdentifierTlv.TYPE: |
| 171 | tlv = BGPLSIdentifierTlv.read(tempCb); |
| 172 | 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 | } |
| 225 | } |