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