blob: e236610e294d0ecedf240ec0211f2953a2439d22 [file] [log] [blame]
Dhruv Dhodye64b93e2016-04-20 19:26:55 +05301/*
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 */
16package org.onosproject.isis.io.isispacket.pdu;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Objects;
20import com.google.common.primitives.Bytes;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.isis.io.isispacket.IsisHeader;
23import org.onosproject.isis.io.isispacket.tlv.IsisTlv;
24import org.onosproject.isis.io.isispacket.tlv.TlvFinder;
25import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
26import org.onosproject.isis.io.isispacket.tlv.TlvType;
27import org.onosproject.isis.io.isispacket.tlv.TlvsToBytes;
28import org.onosproject.isis.io.util.IsisUtil;
29
30import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Representation of P2P hello.
35 */
36public 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()));
127 this.variableLengths.add(tlv);
128 } else {
129 channelBuffer.readBytes(tlvHeader.tlvLength());
130 }
131 }
132 }
133
134 @Override
135 public byte[] asBytes() {
136 byte[] helloMessage = null;
137 byte[] helloHeader = p2PHeader();
138 byte[] helloBody = p2P2HelloPduBody();
139 helloMessage = Bytes.concat(helloHeader, helloBody);
140 return helloMessage;
141 }
142
143 /**
144 * Builds the point to point header.
145 *
146 * @return headerList point to point header
147 */
148 public byte[] p2PHeader() {
149 List<Byte> headerList = new ArrayList<>();
150 headerList.add(this.irpDiscriminator());
151 headerList.add((byte) IsisUtil.getPduHeaderLength(this.pduType()));
152 headerList.add(this.version());
153 headerList.add(this.idLength());
154 headerList.add((byte) this.pduType());
155 headerList.add(this.version2());
156 headerList.add(this.reserved());
157 headerList.add(this.maximumAreaAddresses());
158 return Bytes.toArray(headerList);
159 }
160
161 /**
162 * Builds the point to point hello PDU body.
163 *
164 * @return bodyList point to point hello PDU body
165 */
166 public byte[] p2P2HelloPduBody() {
167 List<Byte> bodyList = new ArrayList<>();
168 bodyList.add(this.circuitType());
169 bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId()));
170 bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.holdingTime())));
171 bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength())));
172 bodyList.add((byte) this.localCircuitId());
173 for (IsisTlv isisTlv : variableLengths) {
174 bodyList.addAll(TlvsToBytes.tlvToBytes(isisTlv));
175 }
176 return Bytes.toArray(bodyList);
177 }
178
179 @Override
180 public String toString() {
181 return MoreObjects.toStringHelper(getClass())
182 .omitNullValues()
183 .add("localCircuitId", localCircuitId)
184 .toString();
185 }
186
187 @Override
188 public boolean equals(Object o) {
189 if (this == o) {
190 return true;
191 }
192 if (o == null || getClass() != o.getClass()) {
193 return false;
194 }
195 P2PHelloPdu that = (P2PHelloPdu) o;
196 return Objects.equal(localCircuitId, that.localCircuitId);
197 }
198
199 @Override
200 public int hashCode() {
201 return Objects.hashCode(localCircuitId);
202 }
203}