blob: b8ae1e87347ca4ff8195d98ac67dd767aabeb596 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
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
32 * Reference : draft-zhao-pce-pcep-extension-for-pce-controller-01, section-7.1.1
33
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=32 | Length=4 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Flags |G|L|
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41
42 */
43 protected static final Logger log = LoggerFactory.getLogger(PceccCapabilityTlv.class);
44
45 public static final short TYPE = 32;
46 public static final short LENGTH = 4;
47 public static final int SET = 1;
48 public static final byte LFLAG_CHECK = 0x01;
49 public static final byte GFLAG_CHECK = 0x02;
50
51 private final boolean bGFlag;
52 private final boolean bLFlag;
53
54 private final int rawValue;
55 private final boolean isRawValueSet;
56
57 /**
58 * Constructor to initialize raw Value.
59 *
60 * @param rawValue raw value
61 */
62 public PceccCapabilityTlv(final int rawValue) {
63 this.rawValue = rawValue;
64 this.isRawValueSet = true;
65
Phanendra Mandaf346ea82015-09-04 15:21:39 -070066 bLFlag = (rawValue & LFLAG_CHECK) == LFLAG_CHECK;
67 bGFlag = (rawValue & GFLAG_CHECK) == GFLAG_CHECK;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070068 }
69
70 /**
71 * Constructor to initialize G-flag L-flag.
72 * @param bGFlag G-flag
73 * @param bLFlag L-flag
74 */
75 public PceccCapabilityTlv(boolean bGFlag, boolean bLFlag) {
76 this.bGFlag = bGFlag;
77 this.bLFlag = bLFlag;
78 this.rawValue = 0;
79 this.isRawValueSet = false;
80 }
81
82 /**
83 * Returns newly created PceccCapabilityTlv object.
84 *
85 * @param raw value
86 * @return object of Pcecc Capability Tlv
87 */
88 public static PceccCapabilityTlv of(final int raw) {
89 return new PceccCapabilityTlv(raw);
90 }
91
92 @Override
93 public PcepVersion getVersion() {
94 return PcepVersion.PCEP_1;
95 }
96
97 /**
98 * Returns G-flag.
99 * @return bGFlag G-flag
100 */
101 public boolean getGFlag() {
102 return bGFlag;
103 }
104
105 /**
106 * Returns L-flag.
107 * @return bLFlag L-flag
108 */
109 public boolean getLFlag() {
110 return bLFlag;
111 }
112
113 /**
114 * Returns the raw value.
115 * @return rawValue Flags
116 */
117 public int getInt() {
118 return rawValue;
119 }
120
121 @Override
122 public short getType() {
123 return TYPE;
124 }
125
126 @Override
127 public short getLength() {
128 return LENGTH;
129 }
130
131 @Override
132 public int hashCode() {
133 if (isRawValueSet) {
134 return Objects.hash(rawValue);
135 } else {
136 return Objects.hash(bLFlag, bGFlag);
137 }
138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 }
145 if (obj instanceof PceccCapabilityTlv) {
146 PceccCapabilityTlv other = (PceccCapabilityTlv) obj;
147 if (isRawValueSet) {
148 return Objects.equals(this.rawValue, other.rawValue);
149 } else {
150 return Objects.equals(this.bGFlag, other.bGFlag) && Objects.equals(this.bLFlag, other.bLFlag);
151 }
152 }
153 return false;
154 }
155
156 @Override
157 public int write(ChannelBuffer c) {
158 int iLenStartIndex = c.writerIndex();
159 int temp = 0;
160 c.writeShort(TYPE);
161 c.writeShort(LENGTH);
162 if (isRawValueSet) {
163 c.writeInt(rawValue);
164 } else {
165 if (bGFlag) {
166 temp = temp | GFLAG_CHECK;
167 }
168 if (bLFlag) {
169 temp = temp | LFLAG_CHECK;
170 }
171 c.writeInt(temp);
172 }
173 return c.writerIndex() - iLenStartIndex;
174 }
175
176 /**
177 * Reads channel buffer and returns object of PceccCapabilityTlv.
178 *
179 * @param c input channel buffer
180 * @return object of PceccCapabilityTlv
181 */
182 public static PceccCapabilityTlv read(ChannelBuffer c) {
183 return PceccCapabilityTlv.of(c.readInt());
184 }
185
186 @Override
187 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700188 return MoreObjects.toStringHelper(getClass())
189 .add("Type", TYPE)
190 .add("Length", LENGTH)
191 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700192 .toString();
193 }
194}