blob: 549ac38f9cfcbf09ad97918790e1303ec601c363 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.pcepio.types;
17
18import java.util.Objects;
19
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;
26
27/**
28 * Provides TED Capability Tlv.
29 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class LsCapabilityTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070031
32 /*
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053033 * Reference :draft-dhodylee-pce-pcep-ls-01, section 9.1.1.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070034 * 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
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Type=[TBD5] | Length=4 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Flags |R|
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 */
42
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053043 protected static final Logger log = LoggerFactory.getLogger(LsCapabilityTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070044
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053045 public static final short TYPE = (short) 65280;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070046 public static final short LENGTH = 4;
47 public static final int SET = 1;
48 public static final byte RFLAG_CHECK = 0x01;
49
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053050 private final boolean rFlag;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070051 private final int rawValue;
52 private final boolean isRawValueSet;
53
54 /**
55 * Constructor to initialize raw Value.
56 *
57 * @param rawValue Flags
58 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053059 public LsCapabilityTlv(final int rawValue) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070060 this.rawValue = rawValue;
61 this.isRawValueSet = true;
62 int temp = rawValue;
63 temp = temp & RFLAG_CHECK;
64 if (temp == SET) {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053065 this.rFlag = true;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070066 } else {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053067 this.rFlag = false;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070068 }
69
70 }
71
72 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053073 * Constructor to initialize rFlag.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070074 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053075 * @param rFlag R-flag
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070076 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053077 public LsCapabilityTlv(boolean rFlag) {
78 this.rFlag = rFlag;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070079 this.rawValue = 0;
80 this.isRawValueSet = false;
81 }
82
83 /**
84 * Returns R-flag.
85 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053086 * @return rFlag
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070087 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053088 public boolean getrFlag() {
89 return rFlag;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070090 }
91
92 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053093 * Returns an object of LsCapabilityTlv.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070094 *
95 * @param raw value Flags
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053096 * @return object of LsCapabilityTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070097 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053098 public static LsCapabilityTlv of(final int raw) {
99 return new LsCapabilityTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700100 }
101
102 @Override
103 public PcepVersion getVersion() {
104 return PcepVersion.PCEP_1;
105 }
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 {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530126 return Objects.hash(rFlag);
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 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530135 if (obj instanceof LsCapabilityTlv) {
136 LsCapabilityTlv other = (LsCapabilityTlv) obj;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700137 if (isRawValueSet) {
138 return Objects.equals(this.rawValue, other.rawValue);
139 } else {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530140 return Objects.equals(this.rFlag, other.rFlag);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700141 }
142 }
143 return false;
144 }
145
146 @Override
147 public int write(ChannelBuffer c) {
148 int iStartIndex = c.writerIndex();
149 int temp = 0;
150 c.writeShort(TYPE);
151 c.writeShort(LENGTH);
152 if (isRawValueSet) {
153 c.writeInt(rawValue);
154 } else {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530155 if (rFlag) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700156 temp = temp | RFLAG_CHECK;
157 }
158 c.writeInt(temp);
159 }
160 return c.writerIndex() - iStartIndex;
161 }
162
163 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530164 * Reads channel buffer and returns object of LsCapabilityTlv.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700165 *
166 * @param c input channel buffer
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530167 * @return object of LsCapabilityTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700168 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530169 public static LsCapabilityTlv read(ChannelBuffer c) {
170 return LsCapabilityTlv.of(c.readInt());
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700171 }
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)
178 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700179 .toString();
180 }
181}