blob: e9faf3e49e5bb90c2dcd4cc4e88f205f40176cf5 [file] [log] [blame]
Dhruv Dhody43f3ce62016-02-16 22:44:21 +05301/*
2 * Copyright 2016 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.ospf.protocol.lsa.types;
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.ospf.controller.OspfLsaType;
23import org.onosproject.ospf.exceptions.OspfErrorType;
24import org.onosproject.ospf.exceptions.OspfParseException;
25import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
26import org.onosproject.ospf.protocol.lsa.TlvHeader;
27import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
28import org.onosproject.ospf.protocol.lsa.tlvtypes.OpaqueTopLevelTlvTypes;
29import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
30import org.onosproject.ospf.protocol.util.OspfParameters;
31
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * Representation of an Opaque LSA of type area local (10).
37 */
38public class OpaqueLsa10 extends OpaqueLsaHeader {
39 /*
40 0 1 2 3
41 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
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | LS age | Options | 9, 10 or 11 |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Opaque Type | Opaque ID |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 | Advertising Router |
48 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 | LS Sequence Number |
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 | LS checksum | Length |
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 | |
54 + +
55 | Opaque Information |
56 + +
57 | ... |
58
59 Opaque LSA format
60 REFERENCE : RFC 5250
61 */
62 private List<TopLevelTlv> topLevelValues = new ArrayList<>();
63 private byte[] opaqueInfo = null;
64
65 /**
66 * Creates an instance of Opaque type 10 LSA.
67 *
68 * @param lsaHeader LSA header instance
69 */
70 public OpaqueLsa10(OpaqueLsaHeader lsaHeader) {
71 populateHeader(lsaHeader);
72 }
73
74 /**
75 * Returns the list of top level TLVs.
76 *
77 * @return list of top level TLVs
78 */
79 public List<TopLevelTlv> topLevelValues() {
80 return topLevelValues;
81 }
82
83 /**
84 * Adds TLV value.
85 *
86 * @param value TLV value
87 */
88 public void addValue(TopLevelTlv value) {
89 topLevelValues.add(value);
90 }
91
92 /**
93 * Reads from channel buffer and populate instance.
94 *
95 * @param channelBuffer channelBuffer instance
96 * @throws OspfParseException might throws exception while parsing buffer
97 */
98 public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
99
100 try {
101 if (this.opaqueId() == OspfParameters.TRAFFIC_ENGINEERING) {
102 while (channelBuffer.readableBytes() > 0) {
103 TlvHeader tlvHeader = new TlvHeader();
104 tlvHeader.setTlvType(channelBuffer.readUnsignedShort());
105 tlvHeader.setTlvLength(channelBuffer.readUnsignedShort());
106 if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.ROUTER.value()) {
107 RouterTlv routerTlv = new RouterTlv(tlvHeader);
108 routerTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
109 this.addValue(routerTlv);
110 } else if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.LINK.value()) {
111 LinkTlv linkTlv = new LinkTlv(tlvHeader);
112 linkTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
113 this.addValue(linkTlv);
114 }
115 }
116 } else {
117 int length = channelBuffer.readableBytes();
118 opaqueInfo = new byte[length];
119 channelBuffer.readBytes(opaqueInfo, 0, length);
120 }
121
122 } catch (Exception e) {
123 log.debug("Error::OpaqueLsa10:: {}", e.getMessage());
124 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
125 OspfErrorType.BAD_MESSAGE);
126 }
127 }
128
129 /**
130 * Returns instance as bytes.
131 *
132 * @return instance as bytes
133 * @throws Exception might throws exception while parsing packet
134 */
135 public byte[] asBytes() throws Exception {
136
137 byte[] lsaMessage = null;
138 byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray();
139 byte[] lsaBody = getLsaBodyAsByteArray();
140 lsaMessage = Bytes.concat(lsaHeader, lsaBody);
141 return lsaMessage;
142
143 }
144
145 /**
146 * Gets the LSA body as byte array.
147 *
148 * @return the lsa body as byte array
149 * @throws Exception might throws exception while parsing packet
150 */
151 public byte[] getLsaBodyAsByteArray() throws Exception {
152 List<Byte> bodyLst = new ArrayList<>();
153 if (this.opaqueId() == 1) {
154 for (TopLevelTlv tlv : this.topLevelValues) {
155 //Check the sub type of lsa and build bytes accordingly
156 if (tlv instanceof RouterTlv) {
157 RouterTlv routerTlv = (RouterTlv) tlv;
158 bodyLst.addAll(Bytes.asList(routerTlv.asBytes()));
159 } else if (tlv instanceof LinkTlv) {
160 LinkTlv linkTlv = (LinkTlv) tlv;
161 bodyLst.addAll(Bytes.asList(linkTlv.asBytes()));
162 }
163 }
164 } else {
165 return opaqueInfo;
166 }
167
168 return Bytes.toArray(bodyLst);
169 }
170
171 @Override
172 public OspfLsaType getOspfLsaType() {
173 return OspfLsaType.AREA_LOCAL_OPAQUE_LSA;
174 }
175
176 @Override
177 public String toString() {
178 return MoreObjects.toStringHelper(getClass())
179 .omitNullValues()
180 .add("topLevelValues", topLevelValues)
181 .add("opaqueInfo", opaqueInfo)
182 .toString();
183 }
184
185 @Override
186 public boolean equals(Object o) {
187 if (this == o) {
188 return true;
189 }
190 if (o == null || getClass() != o.getClass()) {
191 return false;
192 }
193 OpaqueLsa10 that = (OpaqueLsa10) o;
194 return Objects.equal(topLevelValues, that.topLevelValues) &&
195 Objects.equal(opaqueInfo, that.opaqueInfo);
196 }
197
198 @Override
199 public int hashCode() {
200 return Objects.hashCode(opaqueInfo, topLevelValues);
201 }
202}