blob: 119926c830578a985034ca1370bbb343812c6823 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
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.bgpio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Priyanka Bb2988fa2015-10-09 12:45:36 +053021
22import com.google.common.base.MoreObjects;
23
24/**
25 * Provides Autonomous System Tlv which contains opaque value (32 Bit AS Number).
26 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053027public class AutonomousSystemTlv implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053028
29 /* Reference :draft-ietf-idr-ls-distribution-11
30 * 0 1 2 3
31 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
32 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 | Type= 512 | Length=4 |
34 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 | opaque value (32 Bit AS Number) |
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 */
38
Priyanka Bb2988fa2015-10-09 12:45:36 +053039 public static final short TYPE = 512;
40 public static final short LENGTH = 4;
41
42 private final int asNum;
43
44 /**
45 * Constructor to initialize asNum.
46 *
47 * @param asNum 32 Bit AS Number
48 */
49 public AutonomousSystemTlv(int asNum) {
50 this.asNum = asNum;
51 }
52
53 /**
54 * Returns object of this class with specified asNum.
55 *
56 * @param asNum 32 Bit AS Number
57 * @return object of AutonomousSystemTlv
58 */
59 public static AutonomousSystemTlv of(final int asNum) {
60 return new AutonomousSystemTlv(asNum);
61 }
62
63 /**
64 * Returns opaque value of AS Number.
65 *
66 * @return opaque value of AS Number
67 */
68 public int getAsNum() {
69 return asNum;
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hash(asNum);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82
83 if (obj instanceof AutonomousSystemTlv) {
84 AutonomousSystemTlv other = (AutonomousSystemTlv) obj;
85 return Objects.equals(asNum, other.asNum);
86 }
87 return false;
88 }
89
90 @Override
91 public int write(ChannelBuffer c) {
92 int iLenStartIndex = c.writerIndex();
93 c.writeShort(TYPE);
94 c.writeShort(LENGTH);
95 c.writeInt(asNum);
96 return c.writerIndex() - iLenStartIndex;
97 }
98
99 /**
100 * Reads the channel buffer and returns object of AutonomousSystemTlv.
101 *
102 * @param c ChannelBuffer
103 * @return object of AutonomousSystemTlv
104 */
105 public static AutonomousSystemTlv read(ChannelBuffer c) {
106 return AutonomousSystemTlv.of(c.readInt());
107 }
108
109 @Override
110 public short getType() {
111 return TYPE;
112 }
113
114 @Override
Priyanka B02040732015-11-29 11:30:29 +0530115 public int compareTo(Object o) {
116 if (this.equals(o)) {
117 return 0;
118 }
119 return ((Integer) (this.asNum)).compareTo((Integer) (((AutonomousSystemTlv) o).asNum));
120 }
121
122 @Override
Priyanka Bb2988fa2015-10-09 12:45:36 +0530123 public String toString() {
124 return MoreObjects.toStringHelper(getClass())
125 .add("Type", TYPE)
126 .add("Length", LENGTH)
127 .add("asNum", asNum)
128 .toString();
129 }
130}