2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.pcepio.types;
18 import java.util.Objects;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.pcepio.protocol.PcepVersion;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.MoreObjects;
26 import com.google.common.base.MoreObjects.ToStringHelper;
29 * Provides SharedRiskLinkGroupTlv.
31 public class SharedRiskLinkGroupTlv implements PcepValueType {
34 * Reference :[I-D.ietf-idr- Group ls-distribution] /3.3.2.5
37 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Type =TDB41 | Length |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Shared Risk Link Group Value |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Shared Risk Link Group Value |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 protected static final Logger log = LoggerFactory.getLogger(SharedRiskLinkGroupTlv.class);
51 public static final short TYPE = 1096; //TODO:NEED TO HANDLE TDB41
53 private final short hLength;
55 private final int[] srlgValue;
58 * Constructor to initialize SRLG value.
60 * @param srlgValue Shared Risk Link Group Value
61 * @param hLength length
63 public SharedRiskLinkGroupTlv(int[] srlgValue, short hLength) {
64 this.srlgValue = srlgValue;
66 this.hLength = (short) ((srlgValue.length) * 4);
68 this.hLength = hLength;
73 * Returns object of SharedRiskLinkGroupTlv.
76 * @param hLength length
77 * @return object of SharedRiskLinkGroupTlv
79 public static SharedRiskLinkGroupTlv of(final int[] raw, short hLength) {
80 return new SharedRiskLinkGroupTlv(raw, hLength);
88 public int[] getValue() {
93 public PcepVersion getVersion() {
94 return PcepVersion.PCEP_1;
98 public short getType() {
103 public short getLength() {
108 public int hashCode() {
109 return Objects.hash(srlgValue);
113 public boolean equals(Object obj) {
117 if (obj instanceof SharedRiskLinkGroupTlv) {
118 SharedRiskLinkGroupTlv other = (SharedRiskLinkGroupTlv) obj;
119 return Objects.equals(this.srlgValue, other.srlgValue);
125 public int write(ChannelBuffer c) {
126 int iLenStartIndex = c.writerIndex();
128 c.writeShort(hLength);
129 for (int b : srlgValue) {
132 return c.writerIndex() - iLenStartIndex;
136 * Reads from channel buffer and returns object of SharedRiskLinkGroupTlv.
138 * @param c input channel buffer
139 * @param hLength length
140 * @return object of SharedRiskLinkGroupTlv
142 public static PcepValueType read(ChannelBuffer c, short hLength) {
143 int iLength = hLength / 4;
144 int[] iSharedRiskLinkGroup = new int[iLength];
145 for (int i = 0; i < iLength; i++) {
146 iSharedRiskLinkGroup[i] = c.readInt();
148 return new SharedRiskLinkGroupTlv(iSharedRiskLinkGroup, hLength);
153 public String toString() {
154 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
156 toStrHelper.add("Type", TYPE);
157 toStrHelper.add("Length", hLength);
159 StringBuffer result = new StringBuffer();
160 for (int b : srlgValue) {
161 result.append(String.format("%02X ", b));
163 toStrHelper.add("Value", result);
165 return toStrHelper.toString();