blob: 6521ab267dd141aebbb21e354c0cae166e287d6c [file] [log] [blame]
Kiran Ramachandra959353a2016-02-16 22:12:07 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Kiran Ramachandra959353a2016-02-16 22:12:07 +05303 *
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.tlvtypes;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
Ray Milkey019fba42018-01-31 14:07:47 -080021import org.onosproject.ospf.exceptions.OspfParseException;
Kiran Ramachandra959353a2016-02-16 22:12:07 +053022import org.onosproject.ospf.protocol.lsa.TlvHeader;
23import org.onosproject.ospf.protocol.lsa.linksubtype.AdministrativeGroup;
24import org.onosproject.ospf.protocol.lsa.linksubtype.LinkId;
25import org.onosproject.ospf.protocol.lsa.linksubtype.LinkSubType;
26import org.onosproject.ospf.protocol.lsa.linksubtype.LinkSubTypes;
27import org.onosproject.ospf.protocol.lsa.linksubtype.LinkType;
28import org.onosproject.ospf.protocol.lsa.linksubtype.LocalInterfaceIpAddress;
29import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumBandwidth;
30import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumReservableBandwidth;
31import org.onosproject.ospf.protocol.lsa.linksubtype.RemoteInterfaceIpAddress;
32import org.onosproject.ospf.protocol.lsa.linksubtype.TrafficEngineeringMetric;
33import org.onosproject.ospf.protocol.lsa.linksubtype.UnknownLinkSubType;
34import org.onosproject.ospf.protocol.lsa.linksubtype.UnreservedBandwidth;
35import org.onosproject.ospf.protocol.lsa.types.TopLevelTlv;
36import org.onosproject.ospf.protocol.util.OspfUtil;
37
38import java.util.ArrayList;
39import java.util.List;
40
41/**
42 * Representation of an OSPF Opaque link tlv.
43 */
44public class LinkTlv extends TlvHeader implements TopLevelTlv {
45 private List<LinkSubType> subTlv = new ArrayList<>();
46
47 /**
48 * Creates an instance of link tlv.
49 *
50 * @param header tlv header
51 */
52 public LinkTlv(TlvHeader header) {
53 this.setTlvType(header.tlvType());
54 this.setTlvLength(header.tlvLength());
55 }
56
57 /**
58 * Gets sub tlv lists.
59 *
60 * @return sub tlv lists
61 */
62 public List<LinkSubType> subTlvList() {
63 return this.subTlv;
64 }
65
66 /**
67 * Reads bytes from channel buffer .
68 *
69 * @param channelBuffer channel buffer instance
Ray Milkey019fba42018-01-31 14:07:47 -080070 * @throws OspfParseException might throws exception while parsing packet
Kiran Ramachandra959353a2016-02-16 22:12:07 +053071 */
Ray Milkey019fba42018-01-31 14:07:47 -080072 public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +053073 while (channelBuffer.readableBytes() > 0) {
74 TlvHeader tlvHeader = new TlvHeader();
75 tlvHeader.setTlvType(channelBuffer.readUnsignedShort());
76 tlvHeader.setTlvLength(channelBuffer.readUnsignedShort());
77
78 if (LinkSubTypes.LINK_TYPE.value() == tlvHeader.tlvType()) {
79 LinkType linktype = new LinkType(tlvHeader);
80 linktype.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
81 subTlv.add(linktype);
82 if (tlvHeader.tlvLength() < OspfUtil.FOUR_BYTES) {
83 int readerIndex = channelBuffer.readerIndex() + (OspfUtil.FOUR_BYTES - tlvHeader.tlvLength());
84 channelBuffer.readerIndex(readerIndex);
85 }
86 } else if (LinkSubTypes.LINK_ID.value() == tlvHeader.tlvType()) {
87 LinkId linkId = new LinkId(tlvHeader);
88 linkId.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
89 subTlv.add(linkId);
90 } else if (LinkSubTypes.LOCAL_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) {
91 LocalInterfaceIpAddress localInterfaceIpAddress = new LocalInterfaceIpAddress(tlvHeader);
92 localInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
93 subTlv.add(localInterfaceIpAddress);
94 } else if (LinkSubTypes.REMOTE_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) {
95 RemoteInterfaceIpAddress remoteInterfaceIpAddress = new RemoteInterfaceIpAddress(tlvHeader);
96 remoteInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
97 subTlv.add(remoteInterfaceIpAddress);
98 } else if (LinkSubTypes.TRAFFIC_ENGINEERING_METRIC.value() == tlvHeader.tlvType()) {
99 TrafficEngineeringMetric trafficEngineeringMetric = new TrafficEngineeringMetric(tlvHeader);
100 trafficEngineeringMetric.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
101 subTlv.add(trafficEngineeringMetric);
102 } else if (LinkSubTypes.MAXIMUM_BANDWIDTH.value() == tlvHeader.tlvType()) {
103 MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader);
104 maximumBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
105 subTlv.add(maximumBandwidth);
106 } else if (LinkSubTypes.MAXIMUM_RESERVABLE_BANDWIDTH.value() == tlvHeader.tlvType()) {
107 MaximumReservableBandwidth maximumReservableBandwidth = new MaximumReservableBandwidth(tlvHeader);
108 maximumReservableBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
109 subTlv.add(maximumReservableBandwidth);
110 } else if (LinkSubTypes.UNRESERVED_BANDWIDTH.value() == tlvHeader.tlvType()) {
111 UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader);
112 unreservedBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
113 subTlv.add(unreservedBandwidth);
114 } else if (LinkSubTypes.ADMINISTRATIVE_GROUP.value() == tlvHeader.tlvType()) {
115 AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader);
116 administrativeGroup.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
117 subTlv.add(administrativeGroup);
118 } else {
119 UnknownLinkSubType unknownLinkSubType = new UnknownLinkSubType(tlvHeader);
120 unknownLinkSubType.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
121 subTlv.add(unknownLinkSubType);
122 }
123 }
124 }
125
126 /**
127 * Gets link tlv as byte array.
128 *
129 * @return link tlv as byte array
Ray Milkey019fba42018-01-31 14:07:47 -0800130 * @throws OspfParseException if the packet can't be parsed
Kiran Ramachandra959353a2016-02-16 22:12:07 +0530131 */
Ray Milkey019fba42018-01-31 14:07:47 -0800132 public byte[] asBytes() throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +0530133 byte[] lsaMessage = null;
134
135 byte[] tlvHeader = getTlvHeaderAsByteArray();
136 byte[] tlvBody = getTlvBodyAsByteArray();
137 lsaMessage = Bytes.concat(tlvHeader, tlvBody);
138
139 return lsaMessage;
140 }
141
142 /**
143 * Gets tlv body as byte array.
144 *
145 * @return tlv body as byte array
Ray Milkey019fba42018-01-31 14:07:47 -0800146 * @throws OspfParseException might throws exception while parsing buffer
Kiran Ramachandra959353a2016-02-16 22:12:07 +0530147 */
Ray Milkey019fba42018-01-31 14:07:47 -0800148 public byte[] getTlvBodyAsByteArray() throws OspfParseException {
Kiran Ramachandra959353a2016-02-16 22:12:07 +0530149
150 List<Byte> bodyLst = new ArrayList<>();
151 for (LinkSubType tlv : subTlv) {
152 //Check the type of tlv and build bytes accordingly
153 if (tlv instanceof LinkType) {
154 LinkType linkType = (LinkType) tlv;
155 bodyLst.addAll(Bytes.asList(linkType.asBytes()));
156 } else if (tlv instanceof LinkId) {
157 LinkId linkId = (LinkId) tlv;
158 bodyLst.addAll(Bytes.asList(linkId.asBytes()));
159 } else if (tlv instanceof LocalInterfaceIpAddress) {
160 LocalInterfaceIpAddress localInterfaceIpAddress = (LocalInterfaceIpAddress) tlv;
161 bodyLst.addAll(Bytes.asList(localInterfaceIpAddress.asBytes()));
162 } else if (tlv instanceof RemoteInterfaceIpAddress) {
163 RemoteInterfaceIpAddress remoteInterfaceIpAddress = (RemoteInterfaceIpAddress) tlv;
164 bodyLst.addAll(Bytes.asList(remoteInterfaceIpAddress.asBytes()));
165 } else if (tlv instanceof TrafficEngineeringMetric) {
166 TrafficEngineeringMetric trafficEngineeringMetric = (TrafficEngineeringMetric) tlv;
167 bodyLst.addAll(Bytes.asList(trafficEngineeringMetric.asBytes()));
168 } else if (tlv instanceof MaximumBandwidth) {
169 MaximumBandwidth maximumBandwidth = (MaximumBandwidth) tlv;
170 bodyLst.addAll(Bytes.asList(maximumBandwidth.asBytes()));
171 } else if (tlv instanceof MaximumReservableBandwidth) {
172 MaximumReservableBandwidth maximumReservableBandwidth = (MaximumReservableBandwidth) tlv;
173 bodyLst.addAll(Bytes.asList(maximumReservableBandwidth.asBytes()));
174 } else if (tlv instanceof UnreservedBandwidth) {
175 UnreservedBandwidth unreservedBandwidth = (UnreservedBandwidth) tlv;
176 bodyLst.addAll(Bytes.asList(unreservedBandwidth.asBytes()));
177 } else if (tlv instanceof AdministrativeGroup) {
178 AdministrativeGroup administrativeGroup = (AdministrativeGroup) tlv;
179 bodyLst.addAll(Bytes.asList(administrativeGroup.asBytes()));
180 } else {
181 UnknownLinkSubType unknownLinkSubType = (UnknownLinkSubType) tlv;
182 bodyLst.addAll(Bytes.asList(unknownLinkSubType.asBytes()));
183 }
184 }
185 return Bytes.toArray(bodyLst);
186 }
187
188 @Override
189 public String toString() {
190 return MoreObjects.toStringHelper(getClass())
191 .omitNullValues()
192 .add("subTlv", subTlv)
193 .toString();
194 }
195}