blob: 22cd4a62b331d8288093062f649fea703ee4f4df [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07003 *
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.pcepio.types;
17
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070019import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.protocol.PcepVersion;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
Jonathan Hart51539b82015-10-29 09:53:04 -070024import java.util.Objects;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070025
26/**
27 * Provides area ID for OSPF area.
28 */
Jonathan Hart51539b82015-10-29 09:53:04 -070029public class OspfAreaIdSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070030
31 /* Reference :draft-ietf-idr-ls-distribution-10.
32 * 0 1 2 3
33 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
34 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 | Type=[TBD12] | Length=4 |
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | opaque value (32 Bit AS Number) |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 */
40
Jonathan Hart51539b82015-10-29 09:53:04 -070041 protected static final Logger log = LoggerFactory.getLogger(OspfAreaIdSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070042
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053043 public static final short TYPE = 3;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070044 public static final short LENGTH = 4;
45
46 private final int rawValue;
47
48 /**
49 * constructor to initialize rawValue.
50 *
51 * @param rawValue area ID for OSPF area.
52 */
Jonathan Hart51539b82015-10-29 09:53:04 -070053 public OspfAreaIdSubTlv(int rawValue) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070054 this.rawValue = rawValue;
55 }
56
57 /**
58 * Returns newly created OSPFareaIDsubTlv object.
59 *
60 * @param raw opaque value of AreaID
61 * @return new object of OSPF area ID sub TLV
62 */
Jonathan Hart51539b82015-10-29 09:53:04 -070063 public static OspfAreaIdSubTlv of(final int raw) {
64 return new OspfAreaIdSubTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070065 }
66
67 /**
68 * Returns RawValue opaque value of AreaID.
69 *
70 * @return rawValue Area ID
71 */
72 public int getInt() {
73 return rawValue;
74 }
75
76 @Override
77 public PcepVersion getVersion() {
78 return PcepVersion.PCEP_1;
79 }
80
81 @Override
82 public short getType() {
83 return TYPE;
84 }
85
86 @Override
87 public short getLength() {
88 return LENGTH;
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(rawValue);
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700101 if (obj instanceof OspfAreaIdSubTlv) {
102 OspfAreaIdSubTlv other = (OspfAreaIdSubTlv) obj;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700103 return Objects.equals(this.rawValue, other.rawValue);
104 }
105 return false;
106 }
107
108 @Override
109 public int write(ChannelBuffer c) {
110 int iLenStartIndex = c.writerIndex();
111 c.writeShort(TYPE);
112 c.writeShort(LENGTH);
113 c.writeInt(rawValue);
114 return c.writerIndex() - iLenStartIndex;
115 }
116
117 /**
118 * Reads the channel buffer and returns object of OSPFAreaIdSubTlv.
119 *
120 * @param c input channel buffer
121 * @return object of OSPFAreaIdSubTlv
122 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700123 public static OspfAreaIdSubTlv read(ChannelBuffer c) {
124 return OspfAreaIdSubTlv.of(c.readInt());
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700125 }
126
127 @Override
128 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700129 return MoreObjects.toStringHelper(getClass())
130 .add("Type", TYPE)
131 .add("Length", LENGTH)
132 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700133 .toString();
134 }
135}