blob: 189dcadc75af107d4dec136fc581d473e0c84fef [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 */
30public class TedCapabilityTlv implements PcepValueType {
31
32 /*
33 * Reference :PCEP Extension for Transporting TE Data draft-dhodylee-pce-pcep-te-data-extn-02
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
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Type=[TBD5] | Length=4 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Flags |R|
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 */
42
43 protected static final Logger log = LoggerFactory.getLogger(TedCapabilityTlv.class);
44
45 public static final short TYPE = 132; //TODO: need to change this TBD5
46 public static final short LENGTH = 4;
47 public static final int SET = 1;
48 public static final byte RFLAG_CHECK = 0x01;
49
50 private final boolean bRFlag;
51 private final int rawValue;
52 private final boolean isRawValueSet;
53
54 /**
55 * Constructor to initialize raw Value.
56 *
57 * @param rawValue Flags
58 */
59 public TedCapabilityTlv(final int rawValue) {
60 this.rawValue = rawValue;
61 this.isRawValueSet = true;
62 int temp = rawValue;
63 temp = temp & RFLAG_CHECK;
64 if (temp == SET) {
65 this.bRFlag = true;
66 } else {
67 this.bRFlag = false;
68 }
69
70 }
71
72 /**
73 * Constructor to initialize bRFlag.
74 *
75 * @param bRFlag R-flag
76 */
77 public TedCapabilityTlv(boolean bRFlag) {
78 this.bRFlag = bRFlag;
79 this.rawValue = 0;
80 this.isRawValueSet = false;
81 }
82
83 /**
84 * Returns R-flag.
85 *
86 * @return bRFlag
87 */
88 public boolean getbRFlag() {
89 return bRFlag;
90 }
91
92 /**
93 * Returns an object of TedCapabilityTlv.
94 *
95 * @param raw value Flags
96 * @return object of TedCapabilityTlv
97 */
98 public static TedCapabilityTlv of(final int raw) {
99 return new TedCapabilityTlv(raw);
100 }
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 {
126 return Objects.hash(bRFlag);
127 }
128 }
129
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
135 if (obj instanceof TedCapabilityTlv) {
136 TedCapabilityTlv other = (TedCapabilityTlv) obj;
137 if (isRawValueSet) {
138 return Objects.equals(this.rawValue, other.rawValue);
139 } else {
140 return Objects.equals(this.bRFlag, other.bRFlag);
141 }
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 {
155 if (bRFlag) {
156 temp = temp | RFLAG_CHECK;
157 }
158 c.writeInt(temp);
159 }
160 return c.writerIndex() - iStartIndex;
161 }
162
163 /**
164 * Reads channel buffer and returns object of TedCapabilityTlv.
165 *
166 * @param c input channel buffer
167 * @return object of TedCapabilityTlv
168 */
169 public static TedCapabilityTlv read(ChannelBuffer c) {
170 return TedCapabilityTlv.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)
178 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700179 .toString();
180 }
181}