45bf7ac56eaecb749aab7bb7686d3935a86efd63
[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 import com.google.common.base.MoreObjects.ToStringHelper;
28
29 /**
30  * NexthopIPv6addressTlv provides Ipv6  address of next hop.
31  */
32 public class NexthopIPv6addressTlv implements PcepValueType {
33
34     /*
35        Reference: draft-zhao-pce-pcep-extension-for-pce-controller-01.
36
37         0                   1                   2                     3
38         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
39        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40        | Type=TBD                      | Length = 20                   |
41        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42        |                                                               |
43        //               nexthop IPv6 address (16 bytes)                //
44        |                                                               |
45        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46
47                      NEXTHOP-IPV6-ADDRESS TLV:
48
49      */
50     protected static final Logger log = LoggerFactory.getLogger(NexthopIPv6addressTlv.class);
51
52     public static final short TYPE = 100; //to be defined
53     //Length is header + value
54     public static final short LENGTH = 20;
55     public static final short VALUE_LENGTH = 16;
56
57     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 };
58     public static final NexthopIPv6addressTlv NONE = new NexthopIPv6addressTlv(NONE_VAL);
59
60     private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
61         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
62         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
63     public static final NexthopIPv6addressTlv NO_MASK = new NexthopIPv6addressTlv(NO_MASK_VAL);
64     public static final NexthopIPv6addressTlv FULL_MASK = NONE;
65
66     private final byte[] rawValue;
67
68     /**
69      * Constructor to initialize IP address for next hop IPv6 address tlv.
70      *
71      * @param rawValue value of Next hop ipAddress
72      */
73     public NexthopIPv6addressTlv(byte[] rawValue) {
74         log.debug("NexthopIPv6addressTlv");
75         this.rawValue = rawValue;
76     }
77
78     /**
79      * Creates next hop IPv6 address tlv.
80      *
81      * @param raw value of Next hop ipAddress
82      * @return object of NexthopIPv6addressTlv
83      */
84     //logic to be checked
85     public static NexthopIPv6addressTlv of(final byte[] raw) {
86         //check NONE_VAL
87         boolean bFoundNONE = true;
88         //value starts from 3rd byte.
89         for (int i = 5; i < 20; ++i) {
90             if (NONE_VAL[i] != raw[i]) {
91                 bFoundNONE = false;
92             }
93         }
94
95         if (bFoundNONE) {
96             return NONE;
97         }
98
99         //check NO_MASK_VAL
100         boolean bFoundNoMask = true;
101         //value starts from 3rd byte.
102         for (int i = 5; i < 20; ++i) {
103             if (0xFF != raw[i]) {
104                 bFoundNoMask = false;
105             }
106         }
107         if (bFoundNoMask) {
108             return NO_MASK;
109         }
110         return new NexthopIPv6addressTlv(raw);
111     }
112
113     /**
114      * Returns next hop IPv6 address.
115      *
116      * @return next hop IPv6 address
117      */
118     public byte[] getBytes() {
119         return rawValue;
120     }
121
122     @Override
123     public PcepVersion getVersion() {
124         return PcepVersion.PCEP_1;
125     }
126
127     @Override
128     public short getType() {
129         return TYPE;
130     }
131
132     @Override
133     public short getLength() {
134         return LENGTH;
135     }
136
137     @Override
138     public int hashCode() {
139         return Objects.hash(rawValue);
140     }
141
142     @Override
143     public boolean equals(Object obj) {
144         if (this == obj) {
145             return true;
146         }
147         if (obj instanceof NexthopIPv6addressTlv) {
148             NexthopIPv6addressTlv other = (NexthopIPv6addressTlv) obj;
149             return Objects.equals(this.rawValue, other.rawValue);
150         }
151         return false;
152     }
153
154     @Override
155     public int write(ChannelBuffer c) {
156         int iStartIndex = c.writerIndex();
157         c.writeShort(TYPE);
158         c.writeShort(LENGTH);
159         c.writeBytes(rawValue);
160         return c.writerIndex() - iStartIndex;
161     }
162
163     /**
164      * Reads the channel buffer and returns object of NexthopIPv6addressTlv.
165      *
166      * @param c type of channel buffer
167      * @return object of NexthopIPv6addressTlv
168      */
169     public static NexthopIPv6addressTlv read(ChannelBuffer c) {
170         byte[] yTemp = new byte[20];
171         c.readBytes(yTemp, 0, 20);
172         return NexthopIPv6addressTlv.of(yTemp);
173     }
174
175     @Override
176     public String toString() {
177         ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
178
179         toStrHelper.add("Type", TYPE);
180         toStrHelper.add("Length", LENGTH);
181
182         StringBuffer result = new StringBuffer();
183         for (byte b : rawValue) {
184             result.append(String.format("%02X ", b));
185         }
186         toStrHelper.add("IpAddress", result);
187
188         return toStrHelper.toString();
189     }
190 }