2  * Copyright 2015 Open Networking Laboratory
 
   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
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17 package org.onosproject.pcepio.types;
 
  19 import java.util.Objects;
 
  21 import org.jboss.netty.buffer.ChannelBuffer;
 
  22 import org.onosproject.pcepio.protocol.PcepVersion;
 
  23 import org.slf4j.Logger;
 
  24 import org.slf4j.LoggerFactory;
 
  26 import com.google.common.base.MoreObjects;
 
  27 import com.google.common.base.MoreObjects.ToStringHelper;
 
  30  * NexthopIPv6addressTlv provides Ipv6  address of next hop.
 
  32 public class NexthopIPv6addressTlv implements PcepValueType {
 
  35        Reference: draft-zhao-pce-pcep-extension-for-pce-controller-01.
 
  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        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
  43        //               nexthop IPv6 address (16 bytes)                //
 
  45        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
  47                      NEXTHOP-IPV6-ADDRESS TLV:
 
  50     protected static final Logger log = LoggerFactory.getLogger(NexthopIPv6addressTlv.class);
 
  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;
 
  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);
 
  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;
 
  66     private final byte[] rawValue;
 
  69      * Constructor to initialize IP address for next hop IPv6 address tlv.
 
  71      * @param rawValue value of Next hop ipAddress
 
  73     public NexthopIPv6addressTlv(byte[] rawValue) {
 
  74         log.debug("NexthopIPv6addressTlv");
 
  75         this.rawValue = rawValue;
 
  79      * Creates next hop IPv6 address tlv.
 
  81      * @param raw value of Next hop ipAddress
 
  82      * @return object of NexthopIPv6addressTlv
 
  85     public static NexthopIPv6addressTlv of(final byte[] raw) {
 
  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]) {
 
 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;
 
 110         return new NexthopIPv6addressTlv(raw);
 
 114      * Returns next hop IPv6 address.
 
 116      * @return next hop IPv6 address
 
 118     public byte[] getBytes() {
 
 123     public PcepVersion getVersion() {
 
 124         return PcepVersion.PCEP_1;
 
 128     public short getType() {
 
 133     public short getLength() {
 
 138     public int hashCode() {
 
 139         return Objects.hash(rawValue);
 
 143     public boolean equals(Object obj) {
 
 147         if (obj instanceof NexthopIPv6addressTlv) {
 
 148             NexthopIPv6addressTlv other = (NexthopIPv6addressTlv) obj;
 
 149             return Objects.equals(this.rawValue, other.rawValue);
 
 155     public int write(ChannelBuffer c) {
 
 156         int iStartIndex = c.writerIndex();
 
 158         c.writeShort(LENGTH);
 
 159         c.writeBytes(rawValue);
 
 160         return c.writerIndex() - iStartIndex;
 
 164      * Reads the channel buffer and returns object of NexthopIPv6addressTlv.
 
 166      * @param c type of channel buffer
 
 167      * @return object of NexthopIPv6addressTlv
 
 169     public static NexthopIPv6addressTlv read(ChannelBuffer c) {
 
 170         byte[] yTemp = new byte[20];
 
 171         c.readBytes(yTemp, 0, 20);
 
 172         return NexthopIPv6addressTlv.of(yTemp);
 
 176     public String toString() {
 
 177         ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
 
 179         toStrHelper.add("Type", TYPE);
 
 180         toStrHelper.add("Length", LENGTH);
 
 182         StringBuffer result = new StringBuffer();
 
 183         for (byte b : rawValue) {
 
 184             result.append(String.format("%02X ", b));
 
 186         toStrHelper.add("IpAddress", result);
 
 188         return toStrHelper.toString();