Mahesh Poojary S | 5afd06c | 2015-08-21 14:51:04 +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 | package org.onosproject.pcepio.types;
|
| 17 |
|
| 18 | import java.util.Iterator;
|
| 19 | import java.util.LinkedList;
|
| 20 | import java.util.ListIterator;
|
| 21 | import java.util.Objects;
|
| 22 |
|
| 23 | import org.jboss.netty.buffer.ChannelBuffer;
|
| 24 | import org.onosproject.pcepio.exceptions.PcepParseException;
|
| 25 | import org.onosproject.pcepio.protocol.PcepVersion;
|
| 26 | import org.slf4j.Logger;
|
| 27 | import org.slf4j.LoggerFactory;
|
| 28 |
|
| 29 | import com.google.common.base.MoreObjects;
|
| 30 |
|
| 31 | public class TENodeAttributesTlv implements PcepValueType {
|
| 32 | /*
|
| 33 | * Reference :PCEP Extension for Transporting TE Data draft-dhodylee-pce-pcep-te-data-extn-02
|
| 34 | *
|
| 35 | 0 1 2 3
|
| 36 | 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
|
| 37 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 38 | | Type=[TBD20] | Length |
|
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 40 | | |
|
| 41 | // Node Attributes Sub-TLVs (variable) //
|
| 42 | | |
|
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 44 |
|
| 45 |
|
| 46 | */
|
| 47 |
|
| 48 | protected static final Logger log = LoggerFactory.getLogger(TENodeAttributesTlv.class);
|
| 49 |
|
| 50 | public static final short TYPE = 1267; //TODD:change this TBD20
|
| 51 | public short hLength;
|
| 52 |
|
| 53 | public static final int TLV_HEADER_LENGTH = 4;
|
| 54 | // LinkDescriptors Sub-TLVs (variable)
|
| 55 | private LinkedList<PcepValueType> llNodeAttributesSubTLVs;
|
| 56 |
|
| 57 | /**
|
| 58 | * Constructor to initialize llNodeAttributesSubTLVs.
|
| 59 | *
|
| 60 | * @param llNodeAttributesSubTLVs linked list of Node Attributes Sub-TLVs
|
| 61 | */
|
| 62 | public TENodeAttributesTlv(LinkedList<PcepValueType> llNodeAttributesSubTLVs) {
|
| 63 | this.llNodeAttributesSubTLVs = llNodeAttributesSubTLVs;
|
| 64 | }
|
| 65 |
|
| 66 | /**
|
| 67 | * Returns object of TENodeAttributesTlv.
|
| 68 | *
|
| 69 | * @param llNodeAttributesSubTLVs LinkedList of PcepValueType
|
| 70 | * @return object of TENodeAttributesTlv
|
| 71 | */
|
| 72 | public static TENodeAttributesTlv of(LinkedList<PcepValueType> llNodeAttributesSubTLVs) {
|
| 73 | return new TENodeAttributesTlv(llNodeAttributesSubTLVs);
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * Returns Node Attributes Sub-TLVs.
|
| 78 | *
|
| 79 | * @return llNodeAttributesSubTLVs linked list of Node Attributes Sub-TLVs
|
| 80 | */
|
| 81 | public LinkedList<PcepValueType> getllNodeAttributesSubTLVs() {
|
| 82 | return llNodeAttributesSubTLVs;
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public PcepVersion getVersion() {
|
| 87 | return PcepVersion.PCEP_1;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public short getType() {
|
| 92 | return TYPE;
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public short getLength() {
|
| 97 | return hLength;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public int hashCode() {
|
| 102 | return Objects.hash(llNodeAttributesSubTLVs.hashCode());
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public boolean equals(Object obj) {
|
| 107 | if (this == obj) {
|
| 108 | return true;
|
| 109 | }
|
| 110 | /*
|
| 111 | * Here we have a list of Tlv so to compare each sub tlv between the object
|
| 112 | * we have to take a list iterator so one by one we can get each sub tlv object
|
| 113 | * and can compare them.
|
| 114 | * it may be possible that the size of 2 lists is not equal so we have to first check
|
| 115 | * the size, if both are same then we should check for the subtlv objects otherwise
|
| 116 | * we should return false.
|
| 117 | */
|
| 118 | if (obj instanceof TENodeAttributesTlv) {
|
| 119 | int countObjSubTlv = 0;
|
| 120 | int countOtherSubTlv = 0;
|
| 121 | boolean isCommonSubTlv = true;
|
| 122 | TENodeAttributesTlv other = (TENodeAttributesTlv) obj;
|
| 123 | Iterator<PcepValueType> objListIterator = ((TENodeAttributesTlv) obj).llNodeAttributesSubTLVs.iterator();
|
| 124 | countObjSubTlv = ((TENodeAttributesTlv) obj).llNodeAttributesSubTLVs.size();
|
| 125 | countOtherSubTlv = other.llNodeAttributesSubTLVs.size();
|
| 126 | if (countObjSubTlv != countOtherSubTlv) {
|
| 127 | return false;
|
| 128 | } else {
|
| 129 | while (objListIterator.hasNext() && isCommonSubTlv) {
|
| 130 | PcepValueType subTlv = objListIterator.next();
|
| 131 | isCommonSubTlv = Objects.equals(llNodeAttributesSubTLVs.contains(subTlv),
|
| 132 | other.llNodeAttributesSubTLVs.contains(subTlv));
|
| 133 | }
|
| 134 | return isCommonSubTlv;
|
| 135 | }
|
| 136 | }
|
| 137 | return false;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public int write(ChannelBuffer c) {
|
| 142 | int tlvStartIndex = c.writerIndex();
|
| 143 | c.writeShort(TYPE);
|
| 144 | int tlvLenIndex = c.writerIndex();
|
| 145 | hLength = 0;
|
| 146 | c.writeShort(hLength);
|
| 147 |
|
| 148 | ListIterator<PcepValueType> listIterator = llNodeAttributesSubTLVs.listIterator();
|
| 149 |
|
| 150 | while (listIterator.hasNext()) {
|
| 151 | PcepValueType tlv = listIterator.next();
|
| 152 |
|
| 153 | tlv.write(c);
|
| 154 |
|
| 155 | // need to take care of padding
|
| 156 | int pad = tlv.getLength() % 4;
|
| 157 |
|
| 158 | if (0 != pad) {
|
| 159 | pad = 4 - pad;
|
| 160 | for (int i = 0; i < pad; ++i) {
|
| 161 | c.writeByte((byte) 0);
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | hLength = (short) (c.writerIndex() - tlvStartIndex);
|
| 167 | c.setShort(tlvLenIndex, hLength);
|
| 168 |
|
| 169 | return c.writerIndex() - tlvStartIndex;
|
| 170 | }
|
| 171 |
|
| 172 | /**
|
| 173 | * Reads the channel buffer and returns object of TENodeAttributesTlv.
|
| 174 | *
|
| 175 | * @param c input channel buffer
|
| 176 | * @param hLength length
|
| 177 | * @return object of TENodeAttributesTlv
|
| 178 | * @throws PcepParseException if mandatory fields are missing
|
| 179 | */
|
| 180 | public static PcepValueType read(ChannelBuffer c, short hLength) throws PcepParseException {
|
| 181 |
|
| 182 | // Node Descriptor Sub-TLVs (variable)
|
| 183 | LinkedList<PcepValueType> llNodeAttributesSubTLVs = new LinkedList<PcepValueType>();
|
| 184 |
|
| 185 | ChannelBuffer tempCb = c.readBytes(hLength - TLV_HEADER_LENGTH);
|
| 186 |
|
| 187 | while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) {
|
| 188 | PcepValueType tlv;
|
| 189 | short hType = tempCb.readShort();
|
| 190 | int iValue = 0;
|
| 191 | short length = tempCb.readShort();
|
| 192 | switch (hType) {
|
| 193 |
|
| 194 | case NodeFlagBitsTlv.TYPE:
|
| 195 | byte cValue = tempCb.readByte();
|
| 196 | tlv = new NodeFlagBitsTlv(cValue);
|
| 197 | break;
|
| 198 | case OpaqueNodeAttributeTlv.TYPE:
|
| 199 | tlv = OpaqueNodeAttributeTlv.read(tempCb, length);
|
| 200 | break;
|
| 201 | case NodeNameTlv.TYPE:
|
| 202 | tlv = NodeNameTlv.read(tempCb, length);
|
| 203 | break;
|
| 204 | case ISISAreaIdentifierTlv.TYPE:
|
| 205 | tlv = ISISAreaIdentifierTlv.read(tempCb, length);
|
| 206 | break;
|
| 207 | case IPv4TERouterIdOfLocalNodeTlv.TYPE:
|
| 208 | iValue = tempCb.readInt();
|
| 209 | tlv = new IPv4TERouterIdOfLocalNodeTlv(iValue);
|
| 210 | break;
|
| 211 | case IPv6TERouterIdofLocalNodeTlv.TYPE:
|
| 212 | byte[] ipv6Value = new byte[IPv6TERouterIdofLocalNodeTlv.VALUE_LENGTH];
|
| 213 | tempCb.readBytes(ipv6Value, 0, IPv6TERouterIdofLocalNodeTlv.VALUE_LENGTH);
|
| 214 | tlv = new IPv6TERouterIdofLocalNodeTlv(ipv6Value);
|
| 215 | break;
|
| 216 | default:
|
| 217 | throw new PcepParseException("Unsupported Sub TLV type :" + hType);
|
| 218 | }
|
| 219 |
|
| 220 | // Check for the padding
|
| 221 | int pad = length % 4;
|
| 222 | if (0 < pad) {
|
| 223 | pad = 4 - pad;
|
| 224 | if (pad <= tempCb.readableBytes()) {
|
| 225 | tempCb.skipBytes(pad);
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | llNodeAttributesSubTLVs.add(tlv);
|
| 230 | }
|
| 231 |
|
| 232 | if (0 < tempCb.readableBytes()) {
|
| 233 |
|
| 234 | throw new PcepParseException("Sub Tlv parsing error. Extra bytes received.");
|
| 235 | }
|
| 236 | return new TENodeAttributesTlv(llNodeAttributesSubTLVs);
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public String toString() {
|
| 241 | return MoreObjects.toStringHelper(getClass()).add("Type", TYPE).add("Length", hLength)
|
| 242 | .add("NodeAttributesSubTLVs", llNodeAttributesSubTLVs).toString();
|
| 243 | }
|
| 244 | }
|