blob: 6ff5639be7d3e6ab8d07b7bba8d034ad7932ad7a [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07003 *
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.pcepio.types;
17
Satish K2cee68c2015-11-28 14:54:12 +053018import java.util.Arrays;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070019
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26import com.google.common.base.MoreObjects.ToStringHelper;
27
28/**
29 * Provides SharedRiskLinkGroupTlv.
30 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053031public class SharedRiskLinkGroupSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070032
33 /*
34 * Reference :[I-D.ietf-idr- Group ls-distribution] /3.3.2.5
35 *
36 * 0 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 // ............ //
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Shared Risk Link Group Value |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 */
48
Ray Milkey9c9cde42018-01-12 14:22:06 -080049 private static final Logger log = LoggerFactory.getLogger(SharedRiskLinkGroupSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070050
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053051 public static final short TYPE = 30;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070052
53 private final short hLength;
54
55 private final int[] srlgValue;
56
57 /**
58 * Constructor to initialize SRLG value.
59 *
60 * @param srlgValue Shared Risk Link Group Value
61 * @param hLength length
62 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053063 public SharedRiskLinkGroupSubTlv(int[] srlgValue, short hLength) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070064 this.srlgValue = srlgValue;
65 if (0 == hLength) {
66 this.hLength = (short) ((srlgValue.length) * 4);
67 } else {
68 this.hLength = hLength;
69 }
70 }
71
72 /**
73 * Returns object of SharedRiskLinkGroupTlv.
74 *
75 * @param raw value
76 * @param hLength length
77 * @return object of SharedRiskLinkGroupTlv
78 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053079 public static SharedRiskLinkGroupSubTlv of(final int[] raw, short hLength) {
80 return new SharedRiskLinkGroupSubTlv(raw, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070081 }
82
83 /**
84 * Returns SRLG Value.
85 *
86 * @return srlgValue
87 */
88 public int[] getValue() {
89 return srlgValue;
90 }
91
92 @Override
93 public PcepVersion getVersion() {
94 return PcepVersion.PCEP_1;
95 }
96
97 @Override
98 public short getType() {
99 return TYPE;
100 }
101
102 @Override
103 public short getLength() {
104 return hLength;
105 }
106
107 @Override
108 public int hashCode() {
Satish K2cee68c2015-11-28 14:54:12 +0530109 return Arrays.hashCode(srlgValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530117 if (obj instanceof SharedRiskLinkGroupSubTlv) {
118 SharedRiskLinkGroupSubTlv other = (SharedRiskLinkGroupSubTlv) obj;
Satish K2cee68c2015-11-28 14:54:12 +0530119 return Arrays.equals(this.srlgValue, other.srlgValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700120 }
121 return false;
122 }
123
124 @Override
125 public int write(ChannelBuffer c) {
126 int iLenStartIndex = c.writerIndex();
127 c.writeShort(TYPE);
128 c.writeShort(hLength);
129 for (int b : srlgValue) {
130 c.writeInt(b);
131 }
132 return c.writerIndex() - iLenStartIndex;
133 }
134
135 /**
136 * Reads from channel buffer and returns object of SharedRiskLinkGroupTlv.
137 *
138 * @param c input channel buffer
139 * @param hLength length
140 * @return object of SharedRiskLinkGroupTlv
141 */
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();
147 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530148 return new SharedRiskLinkGroupSubTlv(iSharedRiskLinkGroup, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700149 }
150
151
152 @Override
153 public String toString() {
154 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
155
156 toStrHelper.add("Type", TYPE);
157 toStrHelper.add("Length", hLength);
158
159 StringBuffer result = new StringBuffer();
160 for (int b : srlgValue) {
161 result.append(String.format("%02X ", b));
162 }
163 toStrHelper.add("Value", result);
164
165 return toStrHelper.toString();
166 }
167}