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 | /** |
| 49 | * Sets neighbor ID. |
| 50 | * |
| 51 | * @param neighborId neighbor ID |
| 52 | */ |
| 53 | public void setNeighborId(String neighborId) { |
| 54 | this.neighborId = neighborId; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns metric. |
| 59 | * |
| 60 | * @return metric |
| 61 | */ |
| 62 | public int metric() { |
| 63 | return metric; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Sets metric. |
| 68 | * |
| 69 | * @param metric metric |
| 70 | */ |
| 71 | public void setMetric(int metric) { |
| 72 | this.metric = metric; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Adds the TE for extended IS instance to IS extended reachability TLV. |
| 77 | * |
| 78 | * @param trafficEngineeringSubTlv TE for extended IS instance |
| 79 | */ |
| 80 | public void addSubTlv(TrafficEngineeringSubTlv trafficEngineeringSubTlv) { |
| 81 | this.teSubTlv.add(trafficEngineeringSubTlv); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Reads from the channel buffer and populate this instance. |
| 86 | * |
| 87 | * @param channelBuffer channel buffer instance |
| 88 | */ |
| 89 | public void readFrom(ChannelBuffer channelBuffer) { |
| 90 | byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE]; |
| 91 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE); |
| 92 | this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray)); |
| 93 | this.setMetric(channelBuffer.readUnsignedMedium()); |
| 94 | int nTlvPresent = channelBuffer.readByte(); |
| 95 | if (nTlvPresent > 0) { |
| 96 | while (channelBuffer.readableBytes() > IsisUtil.TWO_BYTES) { |
| 97 | TlvHeader tlvHeader = new TlvHeader(); |
| 98 | tlvHeader.setTlvType(channelBuffer.readByte()); |
| 99 | tlvHeader.setTlvLength(channelBuffer.readByte()); |
| 100 | SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType()); |
| 101 | int tlvLength = tlvHeader.tlvLength(); |
| 102 | if (tlvValue != null) { |
| 103 | if (channelBuffer.readableBytes() >= tlvLength) { |
| 104 | this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader, |
| 105 | channelBuffer.readBytes(tlvHeader.tlvLength()))); |
| 106 | } |
| 107 | } else { |
| 108 | if (channelBuffer.readableBytes() >= tlvLength) { |
| 109 | channelBuffer.readBytes(tlvLength); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns neighbor details of IS extended reachability TLV. |
| 118 | * |
| 119 | * @return neighbor details of IS extended reachability TLV. |
| 120 | */ |
| 121 | public byte[] neighborBodyAsbytes() { |
| 122 | List<Byte> byteList = new ArrayList<>(); |
| 123 | byteList.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId())); |
| 124 | byteList.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.metric()))); |
| 125 | if (this.teSubTlv.size() > 0) { |
| 126 | for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.teSubTlv) { |
| 127 | byteList.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv)); |
| 128 | } |
| 129 | } |
| 130 | return Bytes.toArray(byteList); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String toString() { |
| 135 | return MoreObjects.toStringHelper(getClass()) |
| 136 | .omitNullValues() |
| 137 | .add("neighborId", neighborId) |
| 138 | .add("metric", metric) |
| 139 | .add("teSubTlv", teSubTlv) |
| 140 | .toString(); |
| 141 | } |
| 142 | } |