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