Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [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 | package org.onosproject.pcepio.types; |
| 17 | |
| 18 | import java.util.Iterator; |
| 19 | import java.util.LinkedList; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 20 | import java.util.List; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 21 | import java.util.ListIterator; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import org.jboss.netty.buffer.ChannelBuffer; |
| 25 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 26 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.google.common.base.MoreObjects; |
| 31 | |
| 32 | /** |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 33 | * Provides LinkAttributesTlv. |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 34 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 35 | public class LinkAttributesTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 38 | * Reference :draft-dhodylee-pce-pcep-ls-01, section 9.2.8.2. |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 39 | * 0 1 2 3 |
| 40 | 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 |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | Type=[TBD27] | Length | |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | | | |
| 45 | // Link Attributes Sub-TLVs (variable) // |
| 46 | | | |
| 47 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | */ |
| 49 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 50 | private static final Logger log = LoggerFactory.getLogger(LinkAttributesTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 51 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 52 | public static final short TYPE = (short) 65286; |
Ray Milkey | e4afdb5 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 53 | short hLength; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 54 | |
| 55 | public static final int TLV_HEADER_LENGTH = 4; |
| 56 | |
| 57 | // LinkDescriptors Sub-TLVs (variable) |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 58 | private List<PcepValueType> llLinkAttributesSubTLVs; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Constructor to initialize Link Attributes Sub TLVs. |
| 62 | * |
| 63 | * @param llLinkAttributesSubTLVs linked list of PcepValueType |
| 64 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 65 | public LinkAttributesTlv(List<PcepValueType> llLinkAttributesSubTLVs) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 66 | this.llLinkAttributesSubTLVs = llLinkAttributesSubTLVs; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns object of TE Link Attributes TLV. |
| 71 | * |
| 72 | * @param llLinkAttributesSubTLVs linked list of Link Attribute of Sub TLV |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 73 | * @return object of LinkAttributesTlv |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 74 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 75 | public static LinkAttributesTlv of(final List<PcepValueType> llLinkAttributesSubTLVs) { |
| 76 | return new LinkAttributesTlv(llLinkAttributesSubTLVs); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns linked list of Link Attribute of Sub TLV. |
| 81 | * |
| 82 | * @return llLinkAttributesSubTLVs linked list of Link Attribute of Sub TLV |
| 83 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 84 | public List<PcepValueType> getllLinkAttributesSubTLVs() { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 85 | return llLinkAttributesSubTLVs; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public PcepVersion getVersion() { |
| 90 | return PcepVersion.PCEP_1; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public short getType() { |
| 95 | return TYPE; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public short getLength() { |
| 100 | return hLength; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hash(llLinkAttributesSubTLVs.hashCode()); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean equals(Object obj) { |
| 110 | if (this == obj) { |
| 111 | return true; |
| 112 | } |
| 113 | /* |
| 114 | * Here we have a list of Tlv so to compare each sub tlv between the object |
| 115 | * we have to take a list iterator so one by one we can get each sub tlv object |
| 116 | * and can compare them. |
| 117 | * it may be possible that the size of 2 lists is not equal so we have to first check |
| 118 | * the size, if both are same then we should check for the subtlv objects otherwise |
| 119 | * we should return false. |
| 120 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 121 | if (obj instanceof LinkAttributesTlv) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 122 | int countObjSubTlv = 0; |
| 123 | int countOtherSubTlv = 0; |
| 124 | boolean isCommonSubTlv = true; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 125 | LinkAttributesTlv other = (LinkAttributesTlv) obj; |
| 126 | Iterator<PcepValueType> objListIterator = ((LinkAttributesTlv) obj).llLinkAttributesSubTLVs.iterator(); |
| 127 | countObjSubTlv = ((LinkAttributesTlv) obj).llLinkAttributesSubTLVs.size(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 128 | countOtherSubTlv = other.llLinkAttributesSubTLVs.size(); |
| 129 | if (countObjSubTlv != countOtherSubTlv) { |
| 130 | return false; |
| 131 | } else { |
| 132 | while (objListIterator.hasNext() && isCommonSubTlv) { |
| 133 | PcepValueType subTlv = objListIterator.next(); |
| 134 | isCommonSubTlv = Objects.equals(llLinkAttributesSubTLVs.contains(subTlv), |
| 135 | other.llLinkAttributesSubTLVs.contains(subTlv)); |
| 136 | } |
| 137 | return isCommonSubTlv; |
| 138 | } |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public int write(ChannelBuffer c) { |
| 145 | int tlvStartIndex = c.writerIndex(); |
| 146 | c.writeShort(TYPE); |
| 147 | int tlvLenIndex = c.writerIndex(); |
| 148 | hLength = 0; |
| 149 | c.writeShort(hLength); |
| 150 | |
| 151 | ListIterator<PcepValueType> listIterator = llLinkAttributesSubTLVs.listIterator(); |
| 152 | |
| 153 | while (listIterator.hasNext()) { |
| 154 | PcepValueType tlv = listIterator.next(); |
| 155 | |
Sho SHIMIZU | de09fa0 | 2015-09-03 09:39:52 -0700 | [diff] [blame] | 156 | if (tlv == null) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 157 | log.debug("TLV is null from subTlv list"); |
| 158 | continue; |
| 159 | } |
| 160 | tlv.write(c); |
| 161 | |
| 162 | // need to take care of padding |
| 163 | int pad = tlv.getLength() % 4; |
| 164 | |
| 165 | if (0 != pad) { |
| 166 | pad = 4 - pad; |
| 167 | for (int i = 0; i < pad; ++i) { |
| 168 | c.writeByte((byte) 0); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | hLength = (short) (c.writerIndex() - tlvStartIndex); |
| 174 | c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH)); |
| 175 | |
| 176 | return c.writerIndex() - tlvStartIndex; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Reads channel buffer and returns object of TE Link Attributes TLV. |
| 181 | * |
| 182 | * @param c input channel buffer |
| 183 | * @param hLength length |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 184 | * @return object of LinkAttributesTlv |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 185 | * @throws PcepParseException if mandatory fields are missing |
| 186 | */ |
| 187 | public static PcepValueType read(ChannelBuffer c, short hLength) throws PcepParseException { |
| 188 | |
| 189 | // Node Descriptor Sub-TLVs (variable) |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 190 | List<PcepValueType> llLinkAttributesSubTLVs = new LinkedList<>(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 191 | |
| 192 | ChannelBuffer tempCb = c.readBytes(hLength); |
| 193 | |
| 194 | while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) { |
| 195 | |
| 196 | PcepValueType tlv; |
| 197 | short hType = tempCb.readShort(); |
| 198 | int iValue = 0; |
| 199 | short length = tempCb.readShort(); |
| 200 | switch (hType) { |
| 201 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 202 | case IPv4RouterIdOfLocalNodeSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 203 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 204 | tlv = new IPv4RouterIdOfLocalNodeSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 205 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 206 | case IPv6RouterIdofLocalNodeSubTlv.TYPE: |
| 207 | byte[] ipv6LValue = new byte[IPv6RouterIdofLocalNodeSubTlv.VALUE_LENGTH]; |
| 208 | tempCb.readBytes(ipv6LValue, 0, IPv6RouterIdofLocalNodeSubTlv.VALUE_LENGTH); |
| 209 | tlv = new IPv6RouterIdofLocalNodeSubTlv(ipv6LValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 210 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 211 | case IPv4RouterIdOfRemoteNodeSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 212 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 213 | tlv = new IPv4RouterIdOfRemoteNodeSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 214 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 215 | case IPv6RouterIdofRemoteNodeSubTlv.TYPE: |
| 216 | byte[] ipv6RValue = new byte[IPv6RouterIdofRemoteNodeSubTlv.VALUE_LENGTH]; |
| 217 | tempCb.readBytes(ipv6RValue, 0, IPv6RouterIdofRemoteNodeSubTlv.VALUE_LENGTH); |
| 218 | tlv = new IPv6RouterIdofRemoteNodeSubTlv(ipv6RValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 219 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 220 | case LinkLocalRemoteIdentifiersSubTlv.TYPE: |
| 221 | tlv = LinkLocalRemoteIdentifiersSubTlv.read(tempCb); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 222 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 223 | case AdministrativeGroupSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 224 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 225 | tlv = new AdministrativeGroupSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 226 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 227 | case MaximumLinkBandwidthSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 228 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 229 | tlv = new MaximumLinkBandwidthSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 230 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 231 | case MaximumReservableLinkBandwidthSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 232 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 233 | tlv = new MaximumReservableLinkBandwidthSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 234 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 235 | case UnreservedBandwidthSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 236 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 237 | tlv = new UnreservedBandwidthSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 238 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 239 | case TEDefaultMetricSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 240 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 241 | tlv = new TEDefaultMetricSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 242 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 243 | case LinkProtectionTypeSubTlv.TYPE: |
| 244 | tlv = LinkProtectionTypeSubTlv.read(tempCb); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 245 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 246 | case MplsProtocolMaskSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 247 | byte cValue = tempCb.readByte(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 248 | tlv = new MplsProtocolMaskSubTlv(cValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 249 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 250 | case IgpMetricSubTlv.TYPE: |
| 251 | tlv = IgpMetricSubTlv.read(tempCb, length); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 252 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 253 | case SharedRiskLinkGroupSubTlv.TYPE: |
| 254 | tlv = SharedRiskLinkGroupSubTlv.read(tempCb, length); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 255 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 256 | case OpaqueLinkAttributeSubTlv.TYPE: |
| 257 | tlv = OpaqueLinkAttributeSubTlv.read(tempCb, length); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 258 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 259 | case LinkNameAttributeSubTlv.TYPE: |
| 260 | tlv = LinkNameAttributeSubTlv.read(tempCb, length); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 261 | break; |
| 262 | default: |
| 263 | throw new PcepParseException("Unsupported Sub TLV type :" + hType); |
| 264 | } |
| 265 | |
| 266 | // Check for the padding |
| 267 | int pad = length % 4; |
| 268 | if (0 < pad) { |
| 269 | pad = 4 - pad; |
| 270 | if (pad <= tempCb.readableBytes()) { |
| 271 | tempCb.skipBytes(pad); |
| 272 | } |
| 273 | } |
| 274 | llLinkAttributesSubTLVs.add(tlv); |
| 275 | } |
| 276 | |
| 277 | if (0 < tempCb.readableBytes()) { |
| 278 | |
| 279 | throw new PcepParseException("Sub Tlv parsing error. Extra bytes received."); |
| 280 | } |
| 281 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 282 | return new LinkAttributesTlv(llLinkAttributesSubTLVs); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 287 | return MoreObjects.toStringHelper(getClass()) |
| 288 | .add("Type", TYPE) |
| 289 | .add("Length", hLength) |
| 290 | .add("LinkAttributesSubTLVs", llLinkAttributesSubTLVs) |
| 291 | .toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 292 | } |
| 293 | } |