blob: 842c6f02bb0235a9eca08e0c66552efafed69356 [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 AreaID Tlv which contains opaque value (32 Bit Area-ID).
26 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053027public class AreaIDTlv 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= 514 | Length=4 |
34 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 | opaque value (32 Bit Area-ID) |
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 */
38
Priyanka Bb2988fa2015-10-09 12:45:36 +053039 public static final short TYPE = 514;
40 public static final short LENGTH = 4;
41
42 private final int areaID;
43
44 /**
45 * Constructor to initialize areaID.
46 *
47 * @param areaID of BGP AreaID Tlv
48 */
49 public AreaIDTlv(int areaID) {
50 this.areaID = areaID;
51 }
52
53 /**
54 * Returns object of this class with specified areaID.
55 *
56 * @param areaID opaque value of area id
57 * @return object of AreaIDTlv
58 */
59 public static AreaIDTlv of(final int areaID) {
60 return new AreaIDTlv(areaID);
61 }
62
63 /**
64 * Returns opaque value of area id.
65 *
66 * @return opaque value of area id
67 */
68 public int getAreaID() {
69 return areaID;
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hash(areaID);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82
83 if (obj instanceof AreaIDTlv) {
84 AreaIDTlv other = (AreaIDTlv) obj;
85 return Objects.equals(areaID, other.areaID);
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(areaID);
96 return c.writerIndex() - iLenStartIndex;
97 }
98
99 /**
100 * Reads the channel buffer and returns object of AreaIDTlv.
101 *
102 * @param cb ChannelBuffer
103 * @return object of AreaIDTlv
104 */
105 public static AreaIDTlv read(ChannelBuffer cb) {
106 return AreaIDTlv.of(cb.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.areaID)).compareTo((Integer) (((AreaIDTlv) o).areaID));
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("Value", areaID)
128 .toString();
129 }
130}