blob: 1848bf5ea18e480e3d01cf891e3b7b74214babda [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 */
16
17package org.onosproject.pcepio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.protocol.PcepVersion;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * NexthopIPv6addressTlv provides Ipv4 address of next hop.
30 */
31public class NexthopIPv4addressTlv implements PcepValueType {
32
33 /*
34 Reference :draft-zhao-pce-pcep-extension-for-pce-controller-01
35
36 0 1 2 3
37 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
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +053039 | Type=TBD | Length = 4 |
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070040 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | nexthop IPv4 address |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
44 NEXTHOP-IPV4-ADDRESS TLV
45
46 */
Ray Milkey9c9cde42018-01-12 14:22:06 -080047 private static final Logger log = LoggerFactory.getLogger(NexthopIPv4addressTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070048
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +053049 public static final short TYPE = (short) 65289; //to be defined
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070050 //Length is header + value
51 public static final short LENGTH = 8;
52 public static final short VALUE_LENGTH = 4;
53
54 private final int rawValue;
55
56 /**
57 * Constructor to initialize next hop IPv4 address.
58 *
59 * @param rawValue next hop IPv4 address
60 */
61 public NexthopIPv4addressTlv(int rawValue) {
62 this.rawValue = rawValue;
63 }
64
65 /**
66 * Return next hop IPv4 address tlv.
67 *
68 * @param raw of next hop IPv4 address
69 * @return object of NexthopIPv4addressTlv
70 */
71 public static NexthopIPv4addressTlv of(final int raw) {
72 return new NexthopIPv4addressTlv(raw);
73 }
74
75 /**
76 * Returns next hop IPv4 address.
77 *
78 * @return next hop IPv4 address
79 */
80 public int getInt() {
81 return rawValue;
82 }
83
84 @Override
85 public PcepVersion getVersion() {
86 return PcepVersion.PCEP_1;
87 }
88
89 @Override
90 public short getType() {
91 return TYPE;
92 }
93
94 @Override
95 public short getLength() {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +053096 return VALUE_LENGTH;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070097 }
98
99 @Override
100 public int hashCode() {
101 return Objects.hash(rawValue);
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109 if (obj instanceof NexthopIPv4addressTlv) {
110 NexthopIPv4addressTlv other = (NexthopIPv4addressTlv) obj;
111 return Objects.equals(this.rawValue, other.rawValue);
112 }
113 return false;
114 }
115
116 @Override
117 public int write(ChannelBuffer c) {
118 int iStartIndex = c.writerIndex();
119 c.writeShort(TYPE);
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530120 c.writeShort(VALUE_LENGTH);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700121 c.writeInt(rawValue);
122 return c.writerIndex() - iStartIndex;
123 }
124
125 /**
126 * Reads the channel buffer and returns object of NexthopIPv4addressTlv.
127 *
128 * @param c type of channel buffer
129 * @return object of NexthopIPv4addressTlv
130 */
131 public static NexthopIPv4addressTlv read(ChannelBuffer c) {
132 return NexthopIPv4addressTlv.of(c.readInt());
133 }
134
135 @Override
136 public String toString() {
137 return MoreObjects.toStringHelper(getClass())
138 .add("Type", TYPE)
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530139 .add("Length", VALUE_LENGTH)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700140 .add("Ipv4Address ", rawValue)
141 .toString();
142 }
143}