Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.isis.io.isispacket.pdu; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import com.google.common.primitives.Bytes; |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.isis.io.isispacket.IsisHeader; |
| 23 | import org.onosproject.isis.io.isispacket.tlv.IsisTlv; |
| 24 | import org.onosproject.isis.io.isispacket.tlv.TlvFinder; |
| 25 | import org.onosproject.isis.io.isispacket.tlv.TlvHeader; |
| 26 | import org.onosproject.isis.io.isispacket.tlv.TlvType; |
| 27 | import org.onosproject.isis.io.isispacket.tlv.TlvsToBytes; |
| 28 | import org.onosproject.isis.io.util.IsisUtil; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Representation of L1L2 hello PDU. |
| 35 | */ |
| 36 | public class L1L2HelloPdu extends HelloPdu { |
| 37 | |
| 38 | /* |
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | | Intra-domain Routing Protocol Discriminator | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | Length Indicator | |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | | Version/Protocol ID Extension | |
| 45 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | | ID Length | |
| 47 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | | R | R | R | PDU Type | |
| 49 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | | Version | |
| 51 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | | Reserved | |
| 53 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 54 | | Maximum area address | |
| 55 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 56 | | Circuit Type | |
| 57 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 58 | | Source ID | |
| 59 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 60 | | Holding Time | |
| 61 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | | PDU Length | |
| 63 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | | PDU Length | |
| 65 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | | R | Priority | |
| 67 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | | LAN ID | |
| 69 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | | Variable Lengths Fields | |
| 71 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | |
| 73 | Hello Message Format |
| 74 | REFERENCE : ISO/IECĀ 10589 |
| 75 | */ |
| 76 | |
| 77 | private byte priority; |
| 78 | private String lanId; |
| 79 | |
| 80 | /** |
| 81 | * Parametrized constructor. |
| 82 | * |
| 83 | * @param isisHeader ISIs header |
| 84 | */ |
| 85 | public L1L2HelloPdu(IsisHeader isisHeader) { |
| 86 | populateHeader(isisHeader); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the LAN ID. |
| 91 | * |
| 92 | * @return LAN ID |
| 93 | */ |
| 94 | |
| 95 | public String lanId() { |
| 96 | return lanId; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets the LAN ID. |
| 101 | * |
| 102 | * @param lanId LAN ID |
| 103 | */ |
| 104 | public void setLanId(String lanId) { |
| 105 | this.lanId = lanId; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the priority. |
| 110 | * |
| 111 | * @return priority |
| 112 | */ |
| 113 | public byte priority() { |
| 114 | return priority; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Sets priority. |
| 119 | * |
| 120 | * @param priority priority |
| 121 | */ |
| 122 | public void setPriority(byte priority) { |
| 123 | this.priority = priority; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void readFrom(ChannelBuffer channelBuffer) { |
| 128 | this.setCircuitType(channelBuffer.readByte()); |
| 129 | //sorce id |
| 130 | byte[] tempByteArray = new byte[IsisUtil.ID_SIX_BYTES]; |
| 131 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_SIX_BYTES); |
| 132 | this.setSourceId(IsisUtil.systemId(tempByteArray)); |
| 133 | this.setHoldingTime(channelBuffer.readUnsignedShort()); |
| 134 | this.setPduLength(channelBuffer.readUnsignedShort()); |
| 135 | this.setPriority(channelBuffer.readByte()); |
| 136 | //landid id + 1 value |
| 137 | tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE]; |
| 138 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE); |
| 139 | this.setLanId(IsisUtil.systemIdPlus(tempByteArray)); |
| 140 | //tlv here |
| 141 | while (channelBuffer.readableBytes() > 0) { |
| 142 | TlvHeader tlvHeader = new TlvHeader(); |
| 143 | tlvHeader.setTlvType(channelBuffer.readUnsignedByte()); |
| 144 | tlvHeader.setTlvLength(channelBuffer.readUnsignedByte()); |
| 145 | TlvType tlvType = TlvType.get(tlvHeader.tlvType()); |
| 146 | if (tlvType != null) { |
| 147 | IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength())); |
| 148 | this.variableLengths.add(tlv); |
| 149 | } else { |
| 150 | channelBuffer.readBytes(tlvHeader.tlvLength()); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public byte[] asBytes() { |
| 157 | byte[] helloMessage = null; |
| 158 | byte[] helloHeader = l1l2IsisPduHeader(); |
| 159 | byte[] helloBody = l1l2HelloPduBody(); |
| 160 | helloMessage = Bytes.concat(helloHeader, helloBody); |
| 161 | return helloMessage; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Parse the ISIS L1L2 PDU header. |
| 166 | * |
| 167 | * @return ISIS L1L2 PDU header |
| 168 | */ |
| 169 | public byte[] l1l2IsisPduHeader() { |
| 170 | List<Byte> headerLst = new ArrayList<>(); |
| 171 | headerLst.add(this.irpDiscriminator()); |
| 172 | headerLst.add((byte) IsisUtil.getPduHeaderLength(this.pduType())); |
| 173 | headerLst.add(this.version()); |
| 174 | headerLst.add(this.idLength()); |
| 175 | headerLst.add((byte) this.pduType()); |
| 176 | headerLst.add(this.version2()); |
| 177 | headerLst.add(this.reserved()); |
| 178 | headerLst.add(this.maximumAreaAddresses()); |
| 179 | return Bytes.toArray(headerLst); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Parse the ISIS L1L2 PDU body. |
| 184 | * |
| 185 | * @return ISIS L1L2 PDU body |
| 186 | */ |
| 187 | public byte[] l1l2HelloPduBody() { |
| 188 | List<Byte> bodyLst = new ArrayList<>(); |
| 189 | |
| 190 | bodyLst.add(this.circuitType()); |
| 191 | bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId())); |
| 192 | bodyLst.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.holdingTime()))); |
| 193 | bodyLst.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength()))); |
| 194 | bodyLst.add(this.priority); |
| 195 | bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.lanId())); |
| 196 | for (IsisTlv isisTlv : variableLengths) { |
| 197 | bodyLst.addAll(TlvsToBytes.tlvToBytes(isisTlv)); |
| 198 | } |
| 199 | return Bytes.toArray(bodyLst); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public String toString() { |
| 204 | return MoreObjects.toStringHelper(getClass()) |
| 205 | .omitNullValues() |
| 206 | .add("priority", priority) |
| 207 | .add("lanId", lanId) |
| 208 | .toString(); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public boolean equals(Object o) { |
| 213 | if (this == o) { |
| 214 | return true; |
| 215 | } |
| 216 | if (o == null || getClass() != o.getClass()) { |
| 217 | return false; |
| 218 | } |
| 219 | L1L2HelloPdu that = (L1L2HelloPdu) o; |
| 220 | return Objects.equal(priority, that.priority) && |
| 221 | Objects.equal(lanId, that.lanId); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public int hashCode() { |
| 226 | return Objects.hashCode(priority, lanId); |
| 227 | } |
| 228 | } |