Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.MoreObjects.ToStringHelper; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 25 | import java.util.Arrays; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Provides IPv6 Interface Address. REFERENCE :[RFC6119]/4.2. |
| 29 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 30 | public class IPv6InterfaceAddressSubTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 31 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 32 | protected static final Logger log = LoggerFactory.getLogger(IPv6InterfaceAddressSubTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 33 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 34 | public static final short TYPE = 9; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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}; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 39 | public static final IPv6InterfaceAddressSubTlv NONE = new IPv6InterfaceAddressSubTlv(NONE_VAL); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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}; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 44 | public static final IPv6InterfaceAddressSubTlv NO_MASK = new IPv6InterfaceAddressSubTlv(NO_MASK_VAL); |
| 45 | public static final IPv6InterfaceAddressSubTlv FULL_MASK = NONE; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 46 | |
| 47 | private final byte[] rawValue; |
| 48 | |
| 49 | /** |
| 50 | * Constructor to initialize rawValue. |
| 51 | * |
| 52 | * @param rawValue IPv6 Interface Address Tlv |
| 53 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 54 | public IPv6InterfaceAddressSubTlv(byte[] rawValue) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 55 | log.debug("IPv6InterfaceAddressTlv"); |
| 56 | this.rawValue = rawValue; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns newly created IPv6InterfaceAddressTlv object. |
| 61 | * |
| 62 | * @param raw IPv6 Interface Address |
| 63 | * @return object of IPv6InterfaceAddressTlv |
| 64 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 65 | public static IPv6InterfaceAddressSubTlv of(final byte[] raw) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 66 | //check NONE_VAL |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 67 | boolean bFoundNone = true; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 68 | //value starts from 3rd byte. |
| 69 | for (int i = 2; i < 20; ++i) { |
| 70 | if (NONE_VAL[i] != raw[i]) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 71 | bFoundNone = false; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 75 | if (bFoundNone) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 76 | return NONE; |
| 77 | } |
| 78 | |
| 79 | //check NO_MASK_VAL |
| 80 | boolean bFoundNoMask = true; |
| 81 | //value starts from 3rd byte. |
| 82 | for (int i = 2; i < 20; ++i) { |
| 83 | if (0xFF != raw[i]) { |
| 84 | bFoundNoMask = false; |
| 85 | } |
| 86 | } |
| 87 | if (bFoundNoMask) { |
| 88 | return NO_MASK; |
| 89 | } |
| 90 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 91 | return new IPv6InterfaceAddressSubTlv(raw); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns value of IPv6 Interface Address. |
| 96 | * |
| 97 | * @return rawValue raw value |
| 98 | */ |
| 99 | public byte[] getBytes() { |
| 100 | return rawValue; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns value of IPv6 Interface Address. |
| 105 | * |
| 106 | * @return rawValue raw value |
| 107 | */ |
| 108 | public byte[] getValue() { |
| 109 | return rawValue; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public PcepVersion getVersion() { |
| 114 | return PcepVersion.PCEP_1; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public short getType() { |
| 119 | return TYPE; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public short getLength() { |
| 124 | return LENGTH; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int hashCode() { |
Satish K | ff6c11c | 2015-11-22 13:13:28 +0530 | [diff] [blame] | 129 | return Arrays.hashCode(rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object obj) { |
| 134 | if (this == obj) { |
| 135 | return true; |
| 136 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 137 | if (obj instanceof IPv6InterfaceAddressSubTlv) { |
| 138 | IPv6InterfaceAddressSubTlv other = (IPv6InterfaceAddressSubTlv) obj; |
Satish K | ff6c11c | 2015-11-22 13:13:28 +0530 | [diff] [blame] | 139 | return Arrays.equals(rawValue, other.rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public int write(ChannelBuffer c) { |
| 146 | int iLenStartIndex = c.writerIndex(); |
| 147 | c.writeShort(TYPE); |
| 148 | c.writeShort(LENGTH); |
| 149 | c.writeBytes(rawValue); |
| 150 | return c.writerIndex() - iLenStartIndex; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Reads the channel buffer and returns object of IPv6InterfaceAddressTlv. |
| 155 | * |
| 156 | * @param c input channel buffer |
| 157 | * @return object of IPv6InterfaceAddressTlv |
| 158 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 159 | public static IPv6InterfaceAddressSubTlv read20Bytes(ChannelBuffer c) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 160 | byte[] yTemp = new byte[20]; |
| 161 | c.readBytes(yTemp, 0, 20); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 162 | return IPv6InterfaceAddressSubTlv.of(yTemp); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public String toString() { |
| 167 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass()); |
| 168 | |
| 169 | toStrHelper.add("Type", TYPE); |
| 170 | toStrHelper.add("Length", LENGTH); |
| 171 | |
| 172 | StringBuffer result = new StringBuffer(); |
| 173 | for (byte b : rawValue) { |
| 174 | result.append(String.format("%02X ", b)); |
| 175 | } |
| 176 | toStrHelper.add("Value", result); |
| 177 | |
| 178 | return toStrHelper.toString(); |
| 179 | } |
| 180 | } |