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 | /** |
| 33 | * Provides Local TE Node Descriptors TLV which contains Node Descriptor Sub-TLVs. |
| 34 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 35 | public class LocalNodeDescriptorsTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 36 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 37 | /* REFERENCE :draft-dhodylee-pce-pcep-ls-01, section 9.2.2 |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 38 | * 0 1 2 3 |
| 39 | 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 |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | Type=[TBD8] | Length | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | | | |
| 44 | // Node Descriptor Sub-TLVs (variable) // |
| 45 | | | |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | Note: Length is including header here. Refer Routing Universe TLV. |
| 48 | */ |
| 49 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 50 | private static final Logger log = LoggerFactory.getLogger(LocalNodeDescriptorsTlv.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) 65282; |
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 | // Node Descriptor Sub-TLVs (variable) |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 57 | private List<PcepValueType> llNodeDescriptorSubTLVs; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Constructor to initialize llNodeDescriptorSubTLVs. |
| 61 | * |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 62 | * @param llNodeDescriptorSubTLVs List of PcepValueType |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 63 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 64 | public LocalNodeDescriptorsTlv(List<PcepValueType> llNodeDescriptorSubTLVs) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 65 | this.llNodeDescriptorSubTLVs = llNodeDescriptorSubTLVs; |
| 66 | } |
| 67 | |
| 68 | /** |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 69 | * Returns a new object of LocalNodeDescriptorsTLV. |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 70 | * |
| 71 | * @param llNodeDescriptorSubTLVs linked list of Node Descriptor Sub TLVs |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 72 | * @return object of LocalNodeDescriptorsTLV |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 73 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 74 | public static LocalNodeDescriptorsTlv of(final List<PcepValueType> llNodeDescriptorSubTLVs) { |
| 75 | return new LocalNodeDescriptorsTlv(llNodeDescriptorSubTLVs); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns Linked List of tlvs. |
| 80 | * |
| 81 | * @return llNodeDescriptorSubTLVs linked list of Node Descriptor Sub TLV |
| 82 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 83 | public List<PcepValueType> getllNodeDescriptorSubTLVs() { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 84 | return llNodeDescriptorSubTLVs; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public PcepVersion getVersion() { |
| 89 | return PcepVersion.PCEP_1; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public short getType() { |
| 94 | return TYPE; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public short getLength() { |
| 99 | return hLength; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Objects.hash(llNodeDescriptorSubTLVs.hashCode()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(Object obj) { |
| 109 | if (this == obj) { |
| 110 | return true; |
| 111 | } |
| 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 LocalNodeDescriptorsTlv) { |
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 | LocalNodeDescriptorsTlv other = (LocalNodeDescriptorsTlv) obj; |
| 126 | Iterator<PcepValueType> objListIterator = ((LocalNodeDescriptorsTlv) obj).llNodeDescriptorSubTLVs |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 127 | .iterator(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 128 | countObjSubTlv = ((LocalNodeDescriptorsTlv) obj).llNodeDescriptorSubTLVs.size(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 129 | countOtherSubTlv = other.llNodeDescriptorSubTLVs.size(); |
| 130 | if (countObjSubTlv != countOtherSubTlv) { |
| 131 | return false; |
| 132 | } else { |
| 133 | while (objListIterator.hasNext() && isCommonSubTlv) { |
| 134 | PcepValueType subTlv = objListIterator.next(); |
| 135 | isCommonSubTlv = Objects.equals(llNodeDescriptorSubTLVs.contains(subTlv), |
| 136 | other.llNodeDescriptorSubTLVs.contains(subTlv)); |
| 137 | } |
| 138 | return isCommonSubTlv; |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public int write(ChannelBuffer c) { |
| 146 | int tlvStartIndex = c.writerIndex(); |
| 147 | c.writeShort(TYPE); |
| 148 | int tlvLenIndex = c.writerIndex(); |
| 149 | hLength = 0; |
| 150 | c.writeShort(0); |
| 151 | |
| 152 | ListIterator<PcepValueType> listIterator = llNodeDescriptorSubTLVs.listIterator(); |
| 153 | |
| 154 | while (listIterator.hasNext()) { |
| 155 | PcepValueType tlv = listIterator.next(); |
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 | hLength = (short) (c.writerIndex() - tlvStartIndex); |
| 173 | c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH)); |
| 174 | return c.writerIndex() - tlvStartIndex; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Reads the channel buffer and returns object of AutonomousSystemTlv. |
| 179 | * |
| 180 | * @param c input channel buffer |
| 181 | * @param hLength length of subtlvs. |
| 182 | * @return object of AutonomousSystemTlv |
| 183 | * @throws PcepParseException if mandatory fields are missing |
| 184 | */ |
| 185 | public static PcepValueType read(ChannelBuffer c, short hLength) throws PcepParseException { |
| 186 | |
| 187 | // Node Descriptor Sub-TLVs (variable) |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 188 | List<PcepValueType> llNodeDescriptorSubTLVs = new LinkedList<>(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 189 | |
| 190 | ChannelBuffer tempCb = c.readBytes(hLength); |
| 191 | |
| 192 | while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) { |
| 193 | |
| 194 | PcepValueType tlv; |
| 195 | short hType = tempCb.readShort(); |
| 196 | int iValue = 0; |
| 197 | short length = tempCb.readShort(); |
| 198 | |
| 199 | switch (hType) { |
| 200 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 201 | case AutonomousSystemSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 202 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 203 | tlv = new AutonomousSystemSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 204 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 205 | case BgpLsIdentifierSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 206 | iValue = tempCb.readInt(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 207 | tlv = new BgpLsIdentifierSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 208 | break; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 209 | case OspfAreaIdSubTlv.TYPE: |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 210 | iValue = tempCb.readInt(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 211 | tlv = new OspfAreaIdSubTlv(iValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 212 | break; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 213 | case IgpRouterIdSubTlv.TYPE: |
| 214 | tlv = IgpRouterIdSubTlv.read(tempCb, length); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 215 | break; |
| 216 | |
| 217 | default: |
| 218 | throw new PcepParseException("Unsupported Sub TLV type :" + hType); |
| 219 | } |
| 220 | |
| 221 | // Check for the padding |
| 222 | int pad = length % 4; |
| 223 | if (0 < pad) { |
| 224 | pad = 4 - pad; |
| 225 | if (pad <= tempCb.readableBytes()) { |
| 226 | tempCb.skipBytes(pad); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | llNodeDescriptorSubTLVs.add(tlv); |
| 231 | } |
| 232 | |
| 233 | if (0 < tempCb.readableBytes()) { |
| 234 | throw new PcepParseException("Sub Tlv parsing error. Extra bytes received."); |
| 235 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 236 | return new LocalNodeDescriptorsTlv(llNodeDescriptorSubTLVs); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 241 | return MoreObjects.toStringHelper(getClass()) |
| 242 | .add("Type", TYPE) |
| 243 | .add("Length", hLength) |
| 244 | .add("NodeDescriptorSubTLVs", llNodeDescriptorSubTLVs) |
| 245 | .toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 246 | } |
| 247 | } |