8b3390b6a1df8eb1d221057abc0423d69d8f3e4f
[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 package org.onosproject.pcepio.types;
17
18 import java.util.Objects;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.pcepio.protocol.PcepVersion;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.MoreObjects;
26 import com.google.common.base.MoreObjects.ToStringHelper;
27
28 /**
29  * Provides IPv6 Neighbor Address. Reference :[RFC6119]/4.3.
30  */
31 public class IPv6NeighborAddressTlv implements PcepValueType {
32     protected static final Logger log = LoggerFactory.getLogger(IPv6NeighborAddressTlv.class);
33
34     public static final short TYPE = 13; // TDB19
35     public static final short LENGTH = 20;
36     public static final byte VALUE_LENGTH = 18;
37
38     private static final byte[] NONE_VAL = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
39     public static final IPv6NeighborAddressTlv NONE = new IPv6NeighborAddressTlv(NONE_VAL);
40
41     private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
42             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
43             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
44     public static final IPv6NeighborAddressTlv NO_MASK = new IPv6NeighborAddressTlv(NO_MASK_VAL);
45     public static final IPv6NeighborAddressTlv FULL_MASK = NONE;
46
47     private final byte[] rawValue;
48
49     /**
50      * Constructor to initialize rawValue.
51      *
52      * @param rawValue IPv6 Neighbor Address Tlv
53      */
54     public IPv6NeighborAddressTlv(byte[] rawValue) {
55         this.rawValue = rawValue;
56     }
57
58     /**
59      * Returns newly created IPv6NeighborAddressTlv object.
60      *
61      * @param raw IPv6 Neighbor Address
62      * @return object of IPv6 Neighbor Address Tlv
63      */
64     public static IPv6NeighborAddressTlv of(final byte[] raw) {
65         //check NONE_VAL
66         boolean bFoundNONE = true;
67         //value starts from 3rd byte.
68         for (int i = 2; i < 20; ++i) {
69             if (NONE_VAL[i] != raw[i]) {
70                 bFoundNONE = false;
71             }
72         }
73
74         if (bFoundNONE) {
75             return NONE;
76         }
77
78         //check NO_MASK_VAL
79         boolean bFoundNoMask = true;
80         //value starts from 3rd byte.
81         for (int i = 2; i < 20; ++i) {
82             if (0xFF != raw[i]) {
83                 bFoundNoMask = false;
84             }
85         }
86         if (bFoundNoMask) {
87             return NO_MASK;
88         }
89
90         return new IPv6NeighborAddressTlv(raw);
91     }
92
93     /**
94      * Returns value of IPv6 Neighbor Address.
95      *
96      * @return rawValue raw value
97      */
98     public byte[] getBytes() {
99         return rawValue;
100     }
101
102     /**
103      * Returns value of IPv6 Neighbor Address.
104      *
105      * @return rawValue raw value
106      */
107     public byte[] getValue() {
108         return rawValue;
109     }
110
111     @Override
112     public PcepVersion getVersion() {
113         return PcepVersion.PCEP_1;
114     }
115
116     @Override
117     public short getType() {
118         return TYPE;
119     }
120
121     @Override
122     public short getLength() {
123         return LENGTH;
124     }
125
126     @Override
127     public int hashCode() {
128         return Objects.hash(rawValue);
129     }
130
131     @Override
132     public boolean equals(Object obj) {
133         if (this == obj) {
134             return true;
135         }
136         if (obj instanceof IPv6NeighborAddressTlv) {
137             IPv6NeighborAddressTlv other = (IPv6NeighborAddressTlv) obj;
138             return Objects.equals(rawValue, other.rawValue);
139         }
140         return false;
141     }
142
143     @Override
144     public int write(ChannelBuffer c) {
145         int iStartIndex = c.writerIndex();
146         c.writeShort(TYPE);
147         c.writeShort(LENGTH);
148         c.writeBytes(rawValue);
149         return c.writerIndex() - iStartIndex;
150     }
151
152     /**
153      * Reads the channel buffer and returns object of IPv6NeighborAddressTlv.
154      *
155      * @param c input channel buffer
156      * @return object of IPv6NeighborAddressTlv
157      */
158     public static IPv6NeighborAddressTlv read20Bytes(ChannelBuffer c) {
159         byte[] yTemp = new byte[20];
160         c.readBytes(yTemp, 0, 20);
161         return IPv6NeighborAddressTlv.of(yTemp);
162     }
163
164     @Override
165     public String toString() {
166         ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
167
168         toStrHelper.add("Type", TYPE);
169         toStrHelper.add("Length", LENGTH);
170
171         StringBuffer result = new StringBuffer();
172         for (byte b : rawValue) {
173             result.append(String.format("%02X ", b));
174         }
175         toStrHelper.add("Value", result);
176
177         return toStrHelper.toString();
178     }
179 }