blob: b97c4fcd5021a25887bced52f5abcd83345323e4 [file] [log] [blame]
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +05303 *
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
17/**
18 * @author b00295750
19 *
20 */
21package org.onosproject.pcepio.types;
22
23import java.util.Objects;
24
25import org.jboss.netty.buffer.ChannelBuffer;
26import org.onosproject.pcepio.protocol.PcepVersion;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * Provides Autonomous system number sub object.
34 */
35public class AutonomousSystemNumberSubObject implements PcepValueType {
36
37 /*Reference : RFC 3209 : 4.3.3.4
38 * 0 1 2 3
39 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
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 |L| Type | Length | AS number (2-octet) |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 */
44 protected static final Logger log = LoggerFactory.getLogger(AutonomousSystemNumberSubObject.class);
45
46 public static final byte TYPE = (byte) 0x32;
47 public static final byte LENGTH = 4;
48 public static final byte VALUE_LENGTH = 2;
49 public static final byte OBJ_LENGTH = 4;
50 public static final byte LBIT = 0;
51 public static final int SHIFT_LBIT_POSITION = 7;
52 private short asNumber;
53
54 /**
55 * Constructor to initialize AS number.
56 *
57 * @param asNumber AS number
58 */
59 public AutonomousSystemNumberSubObject(short asNumber) {
60 this.asNumber = asNumber;
61 }
62
63 /**
64 * Returns a new instance of AutonomousSystemNumberSubObject.
65 *
66 * @param asNumber AS number
67 * @return object of AutonomousSystemNumberSubObject
68 */
69 public static AutonomousSystemNumberSubObject of(short asNumber) {
70 return new AutonomousSystemNumberSubObject(asNumber);
71 }
72
73 /**
74 * Returns value of AS number.
75 *
76 * @return value of AS number
77 */
78 public short getAsNumber() {
79 return asNumber;
80 }
81
82 @Override
83 public PcepVersion getVersion() {
84 return PcepVersion.PCEP_1;
85 }
86
87 @Override
88 public short getType() {
89 return TYPE;
90 }
91
92 @Override
93 public short getLength() {
94 return LENGTH;
95 }
96
97 @Override
98 public int hashCode() {
99 return Objects.hash(asNumber);
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (this == obj) {
105 return true;
106 }
107 if (obj instanceof AutonomousSystemNumberSubObject) {
108 AutonomousSystemNumberSubObject other = (AutonomousSystemNumberSubObject) obj;
109 return Objects.equals(this.asNumber, other.asNumber);
110 }
111 return false;
112 }
113
114 /**
115 * Reads the channel buffer and returns object of AutonomousSystemNumberSubObject.
116 *
117 * @param c type of channel buffer
118 * @return object of AutonomousSystemNumberSubObject
119 */
120 public static PcepValueType read(ChannelBuffer c) {
121 short asNumber = c.readShort();
122 return new AutonomousSystemNumberSubObject(asNumber);
123 }
124
125 @Override
126 public int write(ChannelBuffer c) {
127 int iLenStartIndex = c.writerIndex();
128 byte bValue = LBIT;
129 bValue = (byte) (bValue << SHIFT_LBIT_POSITION);
130 bValue = (byte) (bValue | TYPE);
131 c.writeByte(bValue);
132 c.writeByte(OBJ_LENGTH);
133 c.writeShort(asNumber);
134
135 return c.writerIndex() - iLenStartIndex;
136 }
137
138 @Override
139 public String toString() {
140 return MoreObjects.toStringHelper(getClass())
141 .add("Type", TYPE)
142 .add("Length", LENGTH)
143 .add("AsNumber", asNumber)
144 .toString();
145 }
146}