Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 17 | package org.onosproject.pcepio.types; |
| 18 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.MoreObjects.ToStringHelper; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Provides IPv6 Sub Object. |
| 30 | */ |
| 31 | public class IPv6SubObject implements PcepValueType { |
| 32 | |
| 33 | /* reference :RFC 4874. |
| 34 | Subobject : IPv6 address |
| 35 | |
| 36 | 0 1 2 3 |
| 37 | 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 |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | Type | Length | IPv6 address (16 bytes) | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | IPv6 address (continued) | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | | IPv6 address (continued) | |
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | | IPv6 address (continued) | |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | | IPv6 address (continued) | Prefix Length | Flags | |
| 48 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 49 | |
| 50 | Type |
| 51 | |
| 52 | 0x02 IPv6 address |
| 53 | |
| 54 | Length |
| 55 | |
| 56 | The Length contains the total length of the subobject in bytes, |
| 57 | including the Type and Length fields. The Length is always 20. |
| 58 | |
| 59 | IPv6 address |
| 60 | |
| 61 | A 128-bit unicast host address. |
| 62 | |
| 63 | Prefix length |
| 64 | |
| 65 | 128 |
| 66 | |
| 67 | Flags |
| 68 | |
| 69 | 0x01 Local protection available |
| 70 | |
| 71 | Indicates that the link downstream of this node is |
| 72 | protected via a local repair mechanism. This flag can |
| 73 | only be set if the Local protection flag was set in the |
| 74 | SESSION_ATTRIBUTE object of the corresponding Path |
| 75 | message. |
| 76 | |
| 77 | 0x02 Local protection in use |
| 78 | |
| 79 | Indicates that a local repair mechanism is in use to |
| 80 | maintain this tunnel (usually in the face of an outage |
| 81 | of the link it was previously routed over). |
| 82 | */ |
| 83 | protected static final Logger log = LoggerFactory.getLogger(IPv6SubObject.class); |
| 84 | |
| 85 | public static final short TYPE = 0x02; |
| 86 | public static final short LENGTH = 20; |
| 87 | public static final byte VALUE_LENGTH = 18; |
| 88 | |
| 89 | 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 }; |
| 90 | public static final IPv6SubObject NONE = new IPv6SubObject(NONE_VAL); |
| 91 | |
| 92 | private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, |
| 93 | (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, |
| 94 | (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; |
| 95 | public static final IPv6SubObject NO_MASK = new IPv6SubObject(NO_MASK_VAL); |
| 96 | public static final IPv6SubObject FULL_MASK = NONE; |
| 97 | |
| 98 | private final byte[] rawValue; |
| 99 | |
| 100 | /** |
| 101 | * constructor to initialize rawValue with ipv6 address. |
| 102 | * |
| 103 | * @param rawValue ipv6 address |
| 104 | */ |
| 105 | public IPv6SubObject(byte[] rawValue) { |
| 106 | this.rawValue = rawValue; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * To create instance of IPv6SubObject. |
| 111 | * |
| 112 | * @param raw byte array of ipv6 address |
| 113 | * @return object of IPv6SubObject |
| 114 | */ |
| 115 | public static IPv6SubObject of(final byte[] raw) { |
| 116 | //check NONE_VAL |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 117 | boolean bFoundNone = true; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 118 | //value starts from 3rd byte. |
| 119 | for (int i = 2; i < 20; ++i) { |
| 120 | if (NONE_VAL[i] != raw[i]) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 121 | bFoundNone = false; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 125 | if (bFoundNone) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 126 | return NONE; |
| 127 | } |
| 128 | |
| 129 | //check NO_MASK_VAL |
| 130 | boolean bFoundNoMask = true; |
| 131 | //value starts from 3rd byte. |
| 132 | for (int i = 2; i < 20; ++i) { |
| 133 | if (0xFF != raw[i]) { |
| 134 | bFoundNoMask = false; |
| 135 | } |
| 136 | } |
| 137 | if (bFoundNoMask) { |
| 138 | return NO_MASK; |
| 139 | } |
| 140 | |
| 141 | return new IPv6SubObject(raw); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns value of IPv6 Sub Object. |
| 146 | * |
| 147 | * @return byte array of ipv6 address |
| 148 | */ |
| 149 | public byte[] getValue() { |
| 150 | return rawValue; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public PcepVersion getVersion() { |
| 155 | return PcepVersion.PCEP_1; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public short getType() { |
| 160 | return TYPE; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public short getLength() { |
| 165 | return LENGTH; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public int hashCode() { |
Satish K | e70d88a | 2015-11-24 21:47:51 +0530 | [diff] [blame] | 170 | return Arrays.hashCode(rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(Object obj) { |
| 175 | if (this == obj) { |
| 176 | return true; |
| 177 | } |
| 178 | if (obj instanceof IPv6SubObject) { |
| 179 | IPv6SubObject other = (IPv6SubObject) obj; |
Satish K | e70d88a | 2015-11-24 21:47:51 +0530 | [diff] [blame] | 180 | return Arrays.equals(rawValue, other.rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public int write(ChannelBuffer c) { |
| 187 | int iStartIndex = c.writerIndex(); |
| 188 | c.writeShort(TYPE); |
| 189 | c.writeShort(LENGTH); |
| 190 | c.writeBytes(rawValue); |
| 191 | return c.writerIndex() - iStartIndex; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Reads the channel buffer and returns object of IPv6SubObject. |
| 196 | * |
| 197 | * @param c type of channel buffer |
| 198 | * @return object of IPv6SubObject |
| 199 | */ |
| 200 | public static IPv6SubObject read20Bytes(ChannelBuffer c) { |
| 201 | byte[] yTemp = new byte[20]; |
| 202 | c.readBytes(yTemp, 0, 20); |
| 203 | return IPv6SubObject.of(yTemp); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public String toString() { |
| 208 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass()); |
| 209 | |
| 210 | toStrHelper.add("Type", TYPE); |
| 211 | toStrHelper.add("Length", LENGTH); |
| 212 | |
| 213 | StringBuffer result = new StringBuffer(); |
| 214 | for (byte b : rawValue) { |
| 215 | result.append(String.format("%02X ", b)); |
| 216 | } |
| 217 | toStrHelper.add("Value", result); |
| 218 | |
| 219 | return toStrHelper.toString(); |
| 220 | } |
| 221 | } |