PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.isis.io.isispacket.tlv; |
| 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.isis.io.isispacket.tlv.subtlv.SubTlvFinder; |
| 22 | import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes; |
| 23 | import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvType; |
| 24 | import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv; |
| 25 | import org.onosproject.isis.io.util.IsisUtil; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Representation of IS extended reachability neighbors. |
| 32 | */ |
| 33 | public class NeighborForExtendedIs { |
| 34 | |
| 35 | private String neighborId; |
| 36 | private int metric; |
| 37 | private List<TrafficEngineeringSubTlv> teSubTlv = new ArrayList<>(); |
| 38 | |
| 39 | /** |
| 40 | * Returns neighbor ID. |
| 41 | * |
| 42 | * @return neighbor ID |
| 43 | */ |
| 44 | public String neighborId() { |
| 45 | return neighborId; |
| 46 | } |
| 47 | |
| 48 | /** |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame^] | 49 | * Returns list of sub tlvs. |
| 50 | * |
| 51 | * @return teSubTlv list of sub tlvs |
| 52 | */ |
| 53 | public List<TrafficEngineeringSubTlv> teSubTlv() { |
| 54 | return teSubTlv; |
| 55 | } |
| 56 | |
| 57 | /** |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 58 | * Sets neighbor ID. |
| 59 | * |
| 60 | * @param neighborId neighbor ID |
| 61 | */ |
| 62 | public void setNeighborId(String neighborId) { |
| 63 | this.neighborId = neighborId; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns metric. |
| 68 | * |
| 69 | * @return metric |
| 70 | */ |
| 71 | public int metric() { |
| 72 | return metric; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets metric. |
| 77 | * |
| 78 | * @param metric metric |
| 79 | */ |
| 80 | public void setMetric(int metric) { |
| 81 | this.metric = metric; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Adds the TE for extended IS instance to IS extended reachability TLV. |
| 86 | * |
| 87 | * @param trafficEngineeringSubTlv TE for extended IS instance |
| 88 | */ |
| 89 | public void addSubTlv(TrafficEngineeringSubTlv trafficEngineeringSubTlv) { |
| 90 | this.teSubTlv.add(trafficEngineeringSubTlv); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Reads from the channel buffer and populate this instance. |
| 95 | * |
| 96 | * @param channelBuffer channel buffer instance |
| 97 | */ |
| 98 | public void readFrom(ChannelBuffer channelBuffer) { |
| 99 | byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE]; |
| 100 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE); |
| 101 | this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray)); |
| 102 | this.setMetric(channelBuffer.readUnsignedMedium()); |
| 103 | int nTlvPresent = channelBuffer.readByte(); |
| 104 | if (nTlvPresent > 0) { |
| 105 | while (channelBuffer.readableBytes() > IsisUtil.TWO_BYTES) { |
| 106 | TlvHeader tlvHeader = new TlvHeader(); |
| 107 | tlvHeader.setTlvType(channelBuffer.readByte()); |
| 108 | tlvHeader.setTlvLength(channelBuffer.readByte()); |
| 109 | SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType()); |
| 110 | int tlvLength = tlvHeader.tlvLength(); |
| 111 | if (tlvValue != null) { |
| 112 | if (channelBuffer.readableBytes() >= tlvLength) { |
sunish vk | c3824e8 | 2016-05-11 19:38:24 +0530 | [diff] [blame] | 113 | TrafficEngineeringSubTlv subTlv = |
| 114 | SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength())); |
| 115 | if (subTlv != null) { |
| 116 | this.addSubTlv(subTlv); |
| 117 | } |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 118 | } |
| 119 | } else { |
| 120 | if (channelBuffer.readableBytes() >= tlvLength) { |
| 121 | channelBuffer.readBytes(tlvLength); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns neighbor details of IS extended reachability TLV. |
| 130 | * |
| 131 | * @return neighbor details of IS extended reachability TLV. |
| 132 | */ |
| 133 | public byte[] neighborBodyAsbytes() { |
| 134 | List<Byte> byteList = new ArrayList<>(); |
| 135 | byteList.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId())); |
| 136 | byteList.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.metric()))); |
| 137 | if (this.teSubTlv.size() > 0) { |
| 138 | for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.teSubTlv) { |
| 139 | byteList.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv)); |
| 140 | } |
| 141 | } |
| 142 | return Bytes.toArray(byteList); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public String toString() { |
| 147 | return MoreObjects.toStringHelper(getClass()) |
| 148 | .omitNullValues() |
| 149 | .add("neighborId", neighborId) |
| 150 | .add("metric", metric) |
| 151 | .add("teSubTlv", teSubTlv) |
| 152 | .toString(); |
| 153 | } |
| 154 | } |