Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [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.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 P2P hello. |
| 35 | */ |
| 36 | public class P2PHelloPdu extends HelloPdu { |
| 37 | /* |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | Intra-domain Routing Protocol Discriminator | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | Length Indicator | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | | Version/Protocol ID Extension | |
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | | ID Length | |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | | R | R | R | PDU Type | |
| 48 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 49 | | Version | |
| 50 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 51 | | Reserved | |
| 52 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 53 | | Maximum area address | |
| 54 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 55 | | Circuit Type | |
| 56 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 57 | | Source ID | |
| 58 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 | | Holding Time | |
| 60 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 61 | | PDU Length | |
| 62 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 63 | | Local Circuit Id | |
| 64 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 65 | | Variable Lengths Fields | |
| 66 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 67 | |
| 68 | P2P Hello Message Format |
| 69 | REFERENCE : ISO/IECĀ 10589 |
| 70 | */ |
| 71 | private byte localCircuitId; |
| 72 | |
| 73 | /** |
| 74 | * Sets the ISIS header. |
| 75 | * |
| 76 | * @param isisHeader isisHeader |
| 77 | */ |
| 78 | public P2PHelloPdu(IsisHeader isisHeader) { |
| 79 | populateHeader(isisHeader); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the local circuit ID. |
| 84 | * |
| 85 | * @return Local circuit ID |
| 86 | */ |
| 87 | public byte localCircuitId() { |
| 88 | return localCircuitId; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sets the local circuit ID. |
| 93 | * |
| 94 | * @param localCircuitId Local circuit ID |
| 95 | */ |
| 96 | public void setLocalCircuitId(byte localCircuitId) { |
| 97 | this.localCircuitId = localCircuitId; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the variable lengths. |
| 102 | * |
| 103 | * @param variableLengths variable lengths. |
| 104 | */ |
| 105 | public void setVariableLengths(List<IsisTlv> variableLengths) { |
| 106 | this.variableLengths = variableLengths; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | @Override |
| 111 | public void readFrom(ChannelBuffer channelBuffer) { |
| 112 | this.setCircuitType(channelBuffer.readByte()); |
| 113 | //source id |
| 114 | byte[] tempByteArray = new byte[IsisUtil.ID_SIX_BYTES]; |
| 115 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_SIX_BYTES); |
| 116 | this.setSourceId(IsisUtil.systemId(tempByteArray)); |
| 117 | this.setHoldingTime(channelBuffer.readUnsignedShort()); |
| 118 | this.setPduLength(channelBuffer.readUnsignedShort()); |
| 119 | this.setLocalCircuitId((byte) channelBuffer.readUnsignedByte()); |
| 120 | while (channelBuffer.readableBytes() > 0) { |
| 121 | TlvHeader tlvHeader = new TlvHeader(); |
| 122 | tlvHeader.setTlvType(channelBuffer.readUnsignedByte()); |
| 123 | tlvHeader.setTlvLength(channelBuffer.readUnsignedByte()); |
| 124 | TlvType tlvType = TlvType.get(tlvHeader.tlvType()); |
| 125 | if (tlvType != null) { |
| 126 | IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength())); |
sunish vk | c3824e8 | 2016-05-11 19:38:24 +0530 | [diff] [blame] | 127 | if (tlv != null) { |
| 128 | this.variableLengths.add(tlv); |
| 129 | } |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 130 | } else { |
| 131 | channelBuffer.readBytes(tlvHeader.tlvLength()); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public byte[] asBytes() { |
| 138 | byte[] helloMessage = null; |
| 139 | byte[] helloHeader = p2PHeader(); |
| 140 | byte[] helloBody = p2P2HelloPduBody(); |
| 141 | helloMessage = Bytes.concat(helloHeader, helloBody); |
| 142 | return helloMessage; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Builds the point to point header. |
| 147 | * |
| 148 | * @return headerList point to point header |
| 149 | */ |
| 150 | public byte[] p2PHeader() { |
| 151 | List<Byte> headerList = new ArrayList<>(); |
| 152 | headerList.add(this.irpDiscriminator()); |
| 153 | headerList.add((byte) IsisUtil.getPduHeaderLength(this.pduType())); |
| 154 | headerList.add(this.version()); |
| 155 | headerList.add(this.idLength()); |
| 156 | headerList.add((byte) this.pduType()); |
| 157 | headerList.add(this.version2()); |
| 158 | headerList.add(this.reserved()); |
| 159 | headerList.add(this.maximumAreaAddresses()); |
| 160 | return Bytes.toArray(headerList); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Builds the point to point hello PDU body. |
| 165 | * |
| 166 | * @return bodyList point to point hello PDU body |
| 167 | */ |
| 168 | public byte[] p2P2HelloPduBody() { |
| 169 | List<Byte> bodyList = new ArrayList<>(); |
| 170 | bodyList.add(this.circuitType()); |
| 171 | bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId())); |
| 172 | bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.holdingTime()))); |
| 173 | bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength()))); |
| 174 | bodyList.add((byte) this.localCircuitId()); |
| 175 | for (IsisTlv isisTlv : variableLengths) { |
| 176 | bodyList.addAll(TlvsToBytes.tlvToBytes(isisTlv)); |
| 177 | } |
| 178 | return Bytes.toArray(bodyList); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public String toString() { |
| 183 | return MoreObjects.toStringHelper(getClass()) |
| 184 | .omitNullValues() |
| 185 | .add("localCircuitId", localCircuitId) |
| 186 | .toString(); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean equals(Object o) { |
| 191 | if (this == o) { |
| 192 | return true; |
| 193 | } |
| 194 | if (o == null || getClass() != o.getClass()) { |
| 195 | return false; |
| 196 | } |
| 197 | P2PHelloPdu that = (P2PHelloPdu) o; |
| 198 | return Objects.equal(localCircuitId, that.localCircuitId); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public int hashCode() { |
| 203 | return Objects.hashCode(localCircuitId); |
| 204 | } |
| 205 | } |