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.
 
  16 package org.onosproject.pcepio.types;
 
  18 import java.util.Objects;
 
  20 import org.jboss.netty.buffer.ChannelBuffer;
 
  21 import org.onosproject.pcepio.protocol.PcepVersion;
 
  22 import org.slf4j.Logger;
 
  23 import org.slf4j.LoggerFactory;
 
  25 import com.google.common.base.MoreObjects;
 
  26 import com.google.common.base.MoreObjects.ToStringHelper;
 
  29  * Provides IPv6 TE Router Id of Local Node. Reference :[RFC6119]/4.1.
 
  31 public class IPv6TERouterIdofLocalNodeTlv implements PcepValueType {
 
  32     protected static final Logger log = LoggerFactory.getLogger(IPv6TERouterIdofLocalNodeTlv.class);
 
  34     public static final short TYPE = 140; //TDB26
 
  35     public static final short LENGTH = 20;
 
  36     public static final byte VALUE_LENGTH = 18;
 
  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 IPv6TERouterIdofLocalNodeTlv NONE = new IPv6TERouterIdofLocalNodeTlv(NONE_VAL);
 
  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 IPv6TERouterIdofLocalNodeTlv NO_MASK = new IPv6TERouterIdofLocalNodeTlv(NO_MASK_VAL);
 
  45     public static final IPv6TERouterIdofLocalNodeTlv FULL_MASK = NONE;
 
  47     private final byte[] rawValue;
 
  50      * Constructor to initialize rawValue.
 
  52      * @param rawValue IPv6TERouterIdofLocalNodeTlv
 
  54     public IPv6TERouterIdofLocalNodeTlv(byte[] rawValue) {
 
  55         this.rawValue = rawValue;
 
  59      * Returns newly created IPv6TERouterIdofLocalNodeTlv object.
 
  61      * @param raw IPv6 TE Router Id of Local Node
 
  62      * @return object of IPv6TERouterIdofLocalNodeTlv
 
  64     public static IPv6TERouterIdofLocalNodeTlv of(final byte[] raw) {
 
  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]) {
 
  79         boolean bFoundNoMask = true;
 
  80         //value starts from 3rd byte.
 
  81         for (int i = 2; i < 20; ++i) {
 
  90         return new IPv6TERouterIdofLocalNodeTlv(raw);
 
  94      * Returns value of IPv6 TE Router Id of Local Node.
 
  96      * @return byte array value of rawValue
 
  98     public byte[] getBytes() {
 
 103      * Returns value of IPv6 TE Router Id of Local Node.
 
 105      * @return byte array value of rawValue
 
 107     public byte[] getValue() {
 
 112     public PcepVersion getVersion() {
 
 113         return PcepVersion.PCEP_1;
 
 117     public short getType() {
 
 122     public short getLength() {
 
 127     public int hashCode() {
 
 128         return Objects.hash(rawValue);
 
 132     public boolean equals(Object obj) {
 
 136         if (obj instanceof IPv6TERouterIdofLocalNodeTlv) {
 
 137             IPv6TERouterIdofLocalNodeTlv other = (IPv6TERouterIdofLocalNodeTlv) obj;
 
 138             return Objects.equals(rawValue, other.rawValue);
 
 144     public int write(ChannelBuffer c) {
 
 145         int iStartIndex = c.writerIndex();
 
 147         c.writeShort(LENGTH);
 
 148         c.writeBytes(rawValue);
 
 149         return c.writerIndex() - iStartIndex;
 
 153      * Reads the channel buffer and returns object of IPv6TERouterIdofLocalNodeTlv.
 
 155      * @param c input channel buffer
 
 156      * @return object of IPv6TERouterIdofLocalNodeTlv
 
 158     public static IPv6TERouterIdofLocalNodeTlv read20Bytes(ChannelBuffer c) {
 
 159         byte[] yTemp = new byte[20];
 
 160         c.readBytes(yTemp, 0, 20);
 
 161         return IPv6TERouterIdofLocalNodeTlv.of(yTemp);
 
 165     public String toString() {
 
 166         ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
 
 168         toStrHelper.add("Type", TYPE);
 
 169         toStrHelper.add("Length", LENGTH);
 
 171         StringBuffer result = new StringBuffer();
 
 172         for (byte b : rawValue) {
 
 173             result.append(String.format("%02X ", b));
 
 175         toStrHelper.add("Value", result);
 
 177         return toStrHelper.toString();