blob: 72a18787c5cb7353cb9bc5c6662817ca44438463 [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 */
16package org.onosproject.pcepio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides IPv4 Interface Address .
29 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class IPv4InterfaceAddressSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070031
32 /*
33 * reference :[RFC5305]/3.2
34 0 1 2 3
35 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
36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 | Type=6 | Length=4 |
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | IPv4 Interface Address |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 */
42
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053043 protected static final Logger log = LoggerFactory.getLogger(IPv4InterfaceAddressSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070044
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053045 public static final short TYPE = 7;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070046 public static final short LENGTH = 4;
47
48 private final int rawValue;
49
50 /**
51 * Constructor to initialize rawValue.
52 *
53 * @param rawValue of IPv4-Interface-Address.
54 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053055 public IPv4InterfaceAddressSubTlv(int rawValue) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070056 this.rawValue = rawValue;
57 }
58
59 /**
60 * Returns newly created IPv4InterfaceAddressTlv object.
61 *
62 * @param raw value of IPv4-Interface-Address
63 * @return object of IPv4-Interface-Address-Tlv
64 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053065 public static IPv4InterfaceAddressSubTlv of(final int raw) {
66 return new IPv4InterfaceAddressSubTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070067 }
68
69 /**
70 * Returns value of IPv4 Interface Address.
71 *
72 * @return rawValue IPv4 Interface Address
73 */
74 public int getInt() {
75 return rawValue;
76 }
77
78 @Override
79 public PcepVersion getVersion() {
80 return PcepVersion.PCEP_1;
81 }
82
83 @Override
84 public short getType() {
85 return TYPE;
86 }
87
88 @Override
89 public short getLength() {
90 return LENGTH;
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(rawValue);
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530103 if (obj instanceof IPv4InterfaceAddressSubTlv) {
104 IPv4InterfaceAddressSubTlv other = (IPv4InterfaceAddressSubTlv) obj;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700105 return Objects.equals(rawValue, other.rawValue);
106 }
107 return false;
108 }
109
110 @Override
111 public int write(ChannelBuffer c) {
112 int iLenStartIndex = c.writerIndex();
113 c.writeShort(TYPE);
114 c.writeShort(LENGTH);
115 c.writeInt(rawValue);
116 return c.writerIndex() - iLenStartIndex;
117 }
118
119 /**
120 * Reads the channel buffer and returns object of IPv4InterfaceAddressTlv.
121 *
122 * @param c input channel buffer
123 * @return object of IPv4-Interface-Address-Tlv
124 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530125 public static IPv4InterfaceAddressSubTlv read(ChannelBuffer c) {
126 return IPv4InterfaceAddressSubTlv.of(c.readInt());
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700127 }
128
129 @Override
130 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700131 return MoreObjects.toStringHelper(getClass())
132 .add("Type", TYPE)
133 .add("Length", LENGTH)
134 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700135 .toString();
136 }
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700137}