blob: 5b1c2438cd4dc0c85fa3809a4e63006495c99679 [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 */
16
17package org.onosproject.pcepio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.protocol.PcepVersion;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * LabelSubObject: Provides a LabelSubObject.
30 */
31public class LabelSubObject implements PcepValueType {
32
33 /* Reference : RFC 3209
34 * LABEL Sub Object
35 *
36 0 1 2 3
37 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
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Type | Length | Flags | C-Type |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Contents of Label Object |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 */
44 protected static final Logger log = LoggerFactory.getLogger(LabelSubObject.class);
45
46 public static final short TYPE = 0x03;
47 public static final short LENGTH = 8;
48 private final byte flags;
49 private final byte cType;
50 private final int contents;
51
52 /**
53 * constructor to initialize parameters for LabelSubObject.
54 *
55 * @param flags flags
56 * @param cType C-Type
57 * @param contents Contents of label object
58 */
59 public LabelSubObject(byte flags, byte cType, int contents) {
60 this.flags = flags;
61 this.cType = cType;
62 this.contents = contents;
63 }
64
65 /**
66 * Return an object of LabelSubObject.
67 *
68 * @param flags flags
69 * @param cType C-type
70 * @param contents contents of label objects
71 * @return object of LabelSubObject
72 */
73 public static LabelSubObject of(byte flags, byte cType, int contents) {
74 return new LabelSubObject(flags, cType, contents);
75 }
76
77 /**
78 * Returns Flags.
79 *
80 * @return flags
81 */
82 public byte getFlags() {
83 return flags;
84 }
85
86 /**
87 * Returns cType.
88 *
89 * @return cType
90 */
91 public byte getCtype() {
92 return cType;
93 }
94
95 /**
96 * Returns contents.
97 *
98 * @return contents
99 */
100 public int getContents() {
101 return contents;
102 }
103
104 @Override
105 public PcepVersion getVersion() {
106 return PcepVersion.PCEP_1;
107 }
108
109 @Override
110 public short getType() {
111 return TYPE;
112 }
113
114 @Override
115 public short getLength() {
116 return LENGTH;
117 }
118
119 @Override
120 public int hashCode() {
121 return Objects.hash(flags, cType, contents);
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129 if (obj instanceof LabelSubObject) {
130 LabelSubObject other = (LabelSubObject) obj;
131 return Objects.equals(this.flags, other.flags) && Objects.equals(this.cType, other.cType)
132 && Objects.equals(this.contents, other.contents);
133 }
134 return false;
135 }
136
137 @Override
138 public int write(ChannelBuffer c) {
139 int iStartIndex = c.writerIndex();
140 c.writeShort(TYPE);
141 c.writeShort(LENGTH);
142 c.writeByte(flags);
143 c.writeByte(cType);
144 c.writeByte(contents);
145 return c.writerIndex() - iStartIndex;
146 }
147
148 /**
149 * Reads the channel buffer and returns object of LabelSubObject.
150 *
151 * @param c type of channel buffer
152 * @return object of LabelSubObject
153 */
154 public static PcepValueType read(ChannelBuffer c) {
155 byte flags = c.readByte();
156 byte cType = c.readByte();
157 int contents = c.readInt();
158 return new LabelSubObject(flags, cType, contents);
159 }
160
161 @Override
162 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700163 return MoreObjects.toStringHelper(getClass())
164 .add("type", TYPE)
165 .add("Length", LENGTH)
166 .add("flags", flags)
167 .add("C-type", cType)
168 .add("contents", contents)
169 .toString();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700170 }
171}