blob: 93199c53c26ee30011950c18cd44cdac435145bd [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
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
19import com.google.common.base.MoreObjects.ToStringHelper;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070020import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Jonathan Hart51539b82015-10-29 09:53:04 -070025import java.util.Arrays;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070026
27/**
28 * Provides IGP Link Metric .
29 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class IgpMetricSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070031
32 /* Reference :[I-D.ietf-idr-ls-distribution] /3.3.2.4
33 * 0 1 2 3
34 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
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | Type=TDB40 | Length |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 // IGP Link Metric (variable length) //
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 */
41
Ray Milkey9c9cde42018-01-12 14:22:06 -080042 private static final Logger log = LoggerFactory.getLogger(IgpMetricSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070043
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053044 public static final short TYPE = 29;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070045 private short hLength;
46
47 private final byte[] rawValue;
48
49 /**
50 * Constructor to initialize raw value.
51 *
52 * @param rawValue IGP Link Metric
53 * @param hLength length
54 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053055 public IgpMetricSubTlv(byte[] rawValue, short hLength) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070056 this.rawValue = rawValue;
57 this.hLength = hLength;
58 }
59
60 /**
61 * Returns newly created IGPMetricTlv object.
62 *
63 * @param raw value of IGP Link Metric
64 * @param hLength length
65 * @return object of IGPMetricTlv
66 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053067 public static IgpMetricSubTlv of(final byte[] raw, short hLength) {
68 return new IgpMetricSubTlv(raw, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070069 }
70
71 /**
72 * Returns value of IGP Link Metric.
73 *
74 * @return rawValue of IGP Link Metric
75 */
76 public byte[] getValue() {
77 return rawValue;
78 }
79
80 @Override
81 public PcepVersion getVersion() {
82 return PcepVersion.PCEP_1;
83 }
84
85 @Override
86 public short getType() {
87 return TYPE;
88 }
89
90 @Override
91 public short getLength() {
92 return hLength;
93 }
94
95 @Override
96 public int hashCode() {
Satish Kff6c11c2015-11-22 13:13:28 +053097 return Arrays.hashCode(rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070098 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530105 if (obj instanceof IgpMetricSubTlv) {
106 IgpMetricSubTlv other = (IgpMetricSubTlv) obj;
Satish Kff6c11c2015-11-22 13:13:28 +0530107 return Arrays.equals(rawValue, other.rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700108 }
109 return false;
110 }
111
112 @Override
113 public int write(ChannelBuffer c) {
114 int iLenStartIndex = c.writerIndex();
115 c.writeShort(TYPE);
116 c.writeShort(hLength);
117 c.writeBytes(rawValue);
118 return c.writerIndex() - iLenStartIndex;
119 }
120
121 /**
122 * Reads the channel buffer and returns object of IGPMetricTlv.
123 *
124 * @param c input channel buffer
125 * @param hLength length
126 * @return object of IGPMetricTlv
127 */
128 public static PcepValueType read(ChannelBuffer c, short hLength) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700129 byte[] iIgpMetric = new byte[hLength];
130 c.readBytes(iIgpMetric, 0, hLength);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530131 return new IgpMetricSubTlv(iIgpMetric, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700132 }
133
134 @Override
135 public String toString() {
136 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
137
138 toStrHelper.add("Type", TYPE);
139 toStrHelper.add("Length", hLength);
140
141 StringBuffer result = new StringBuffer();
142 for (byte b : rawValue) {
143 result.append(String.format("%02X ", b));
144 }
145 toStrHelper.add("Value", result);
146
147 return toStrHelper.toString();
148 }
149}