blob: 0f29c53e30451eb43f9440f9348e806989230cb9 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070016package org.onosproject.pcepio.types;
17
18import java.util.Objects;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.protocol.PcepVersion;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import com.google.common.base.MoreObjects;
25
26/**
27 * Provides PceccCapabilityTlv.
28 */
29public class PceccCapabilityTlv implements PcepValueType {
30
31 /* PCECC CAPABILITY TLV
Priyanka B21f4b732016-03-21 20:54:12 +053032 * Reference : draft-zhao-pce-pcep-extension-for-pce-controller-03, section-7.1.1
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070033
34 0 1 2 3
35 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
Priyanka B21f4b732016-03-21 20:54:12 +053036 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Type=[TBD] | Length=4 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Flags |S|
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070041
42 */
Ray Milkey9c9cde42018-01-12 14:22:06 -080043 private static final Logger log = LoggerFactory.getLogger(PceccCapabilityTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070044
Priyanka B21f4b732016-03-21 20:54:12 +053045 public static final short TYPE = (short) 65287;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070046 public static final short LENGTH = 4;
47 public static final int SET = 1;
Priyanka B21f4b732016-03-21 20:54:12 +053048 public static final byte SBIT_CHECK = 0x01;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070049
Priyanka B21f4b732016-03-21 20:54:12 +053050 private final boolean sBit;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070051
52 private final int rawValue;
53 private final boolean isRawValueSet;
54
55 /**
56 * Constructor to initialize raw Value.
57 *
58 * @param rawValue raw value
59 */
60 public PceccCapabilityTlv(final int rawValue) {
61 this.rawValue = rawValue;
62 this.isRawValueSet = true;
63
Priyanka B21f4b732016-03-21 20:54:12 +053064 sBit = (rawValue & SBIT_CHECK) == SBIT_CHECK;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070065 }
66
67 /**
68 * Constructor to initialize G-flag L-flag.
Priyanka B21f4b732016-03-21 20:54:12 +053069 *
70 * @param sBit pcecc sr capbaility bit
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070071 */
Priyanka B21f4b732016-03-21 20:54:12 +053072 public PceccCapabilityTlv(boolean sBit) {
73 this.sBit = sBit;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070074 this.rawValue = 0;
75 this.isRawValueSet = false;
76 }
77
78 /**
79 * Returns newly created PceccCapabilityTlv object.
80 *
81 * @param raw value
82 * @return object of Pcecc Capability Tlv
83 */
84 public static PceccCapabilityTlv of(final int raw) {
85 return new PceccCapabilityTlv(raw);
86 }
87
88 @Override
89 public PcepVersion getVersion() {
90 return PcepVersion.PCEP_1;
91 }
92
93 /**
Priyanka B21f4b732016-03-21 20:54:12 +053094 * Returns sBit.
95 *
96 * @return sBit S bit
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070097 */
Priyanka B21f4b732016-03-21 20:54:12 +053098 public boolean sBit() {
99 return sBit;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700100 }
101
102 /**
103 * Returns the raw value.
Priyanka B21f4b732016-03-21 20:54:12 +0530104 *
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700105 * @return rawValue Flags
106 */
107 public int getInt() {
108 return rawValue;
109 }
110
111 @Override
112 public short getType() {
113 return TYPE;
114 }
115
116 @Override
117 public short getLength() {
118 return LENGTH;
119 }
120
121 @Override
122 public int hashCode() {
123 if (isRawValueSet) {
124 return Objects.hash(rawValue);
125 } else {
Priyanka B21f4b732016-03-21 20:54:12 +0530126 return Objects.hash(sBit);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700127 }
128 }
129
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
135 if (obj instanceof PceccCapabilityTlv) {
136 PceccCapabilityTlv other = (PceccCapabilityTlv) obj;
137 if (isRawValueSet) {
138 return Objects.equals(this.rawValue, other.rawValue);
139 } else {
Priyanka B21f4b732016-03-21 20:54:12 +0530140 return Objects.equals(this.sBit, other.sBit);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700141 }
142 }
143 return false;
144 }
145
146 @Override
147 public int write(ChannelBuffer c) {
148 int iLenStartIndex = c.writerIndex();
149 int temp = 0;
150 c.writeShort(TYPE);
151 c.writeShort(LENGTH);
152 if (isRawValueSet) {
153 c.writeInt(rawValue);
154 } else {
Priyanka B21f4b732016-03-21 20:54:12 +0530155 if (sBit) {
156 temp = temp | SBIT_CHECK;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700157 }
158 c.writeInt(temp);
159 }
160 return c.writerIndex() - iLenStartIndex;
161 }
162
163 /**
164 * Reads channel buffer and returns object of PceccCapabilityTlv.
165 *
166 * @param c input channel buffer
167 * @return object of PceccCapabilityTlv
168 */
169 public static PceccCapabilityTlv read(ChannelBuffer c) {
170 return PceccCapabilityTlv.of(c.readInt());
171 }
172
173 @Override
174 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700175 return MoreObjects.toStringHelper(getClass())
176 .add("Type", TYPE)
177 .add("Length", LENGTH)
Priyanka B21f4b732016-03-21 20:54:12 +0530178 .add("rawValue", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700179 .toString();
180 }
181}