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