Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +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.ospf.protocol.lsa.tlvtypes; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.ospf.protocol.lsa.TlvHeader; |
| 22 | import org.onosproject.ospf.protocol.lsa.linksubtype.AdministrativeGroup; |
| 23 | import org.onosproject.ospf.protocol.lsa.linksubtype.LinkId; |
| 24 | import org.onosproject.ospf.protocol.lsa.linksubtype.LinkSubType; |
| 25 | import org.onosproject.ospf.protocol.lsa.linksubtype.LinkSubTypes; |
| 26 | import org.onosproject.ospf.protocol.lsa.linksubtype.LinkType; |
| 27 | import org.onosproject.ospf.protocol.lsa.linksubtype.LocalInterfaceIpAddress; |
| 28 | import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumBandwidth; |
| 29 | import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumReservableBandwidth; |
| 30 | import org.onosproject.ospf.protocol.lsa.linksubtype.RemoteInterfaceIpAddress; |
| 31 | import org.onosproject.ospf.protocol.lsa.linksubtype.TrafficEngineeringMetric; |
| 32 | import org.onosproject.ospf.protocol.lsa.linksubtype.UnknownLinkSubType; |
| 33 | import org.onosproject.ospf.protocol.lsa.linksubtype.UnreservedBandwidth; |
| 34 | import org.onosproject.ospf.protocol.lsa.types.TopLevelTlv; |
| 35 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 36 | |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * Representation of an OSPF Opaque link tlv. |
| 42 | */ |
| 43 | public 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 | } |