6f193384b2021d0ee2228ef28837a2e5bb92db04
[onosfw.git] /
1 /*
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  */
16
17 package org.onosproject.pcepio.types;
18
19 import java.util.Objects;
20
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.onosproject.pcepio.protocol.PcepVersion;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.MoreObjects;
27
28 /**
29  * NexthopIPv6addressTlv provides Ipv4 address of next hop.
30  */
31 public 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        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39        | Type=TBD                      | Length = 8                    |
40        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41        |                     nexthop IPv4 address                      |
42        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
44                       NEXTHOP-IPV4-ADDRESS TLV
45
46      */
47     protected static final Logger log = LoggerFactory.getLogger(NexthopIPv4addressTlv.class);
48
49     public static final short TYPE = 2; //to be defined
50     //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() {
96         return LENGTH;
97     }
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);
120         c.writeShort(LENGTH);
121         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)
139                 .add("Length", LENGTH)
140                 .add("Ipv4Address ", rawValue)
141                 .toString();
142     }
143 }