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 | |
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; |
| 26 | import java.util.Objects; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Provides ISIS Area Identifier. |
| 30 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 31 | public class IsisAreaIdentifierTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 32 | |
| 33 | /* Reference :[I-D.ietf-idr- ls-distribution]/3.3.1.2 |
| 34 | * 0 1 2 3 |
| 35 | 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 |
| 36 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | | Type=[TBD24] | Length | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | // Area Identifier (variable) // |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | */ |
| 42 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 43 | protected static final Logger log = LoggerFactory.getLogger(IsisAreaIdentifierTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 44 | |
| 45 | public static final short TYPE = 107; //TODO:NEED TO HANDLE TBD24 |
| 46 | private short hLength; |
| 47 | |
| 48 | private final byte[] rawValue; |
| 49 | |
| 50 | /** |
| 51 | * Constructor to initialize rawValue. |
| 52 | * |
| 53 | * @param rawValue ISIS-Area-Identifier |
| 54 | * @param hLength length |
| 55 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 56 | public IsisAreaIdentifierTlv(byte[] rawValue, short hLength) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 57 | log.debug("ISISAreaIdentifierTlv"); |
| 58 | this.rawValue = rawValue; |
| 59 | if (0 == hLength) { |
| 60 | this.hLength = (short) rawValue.length; |
| 61 | } else { |
| 62 | this.hLength = hLength; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns newly created ISISAreaIdentifierTlv object. |
| 68 | * |
| 69 | * @param raw ISIS-Area-Identifier |
| 70 | * @param hLength length |
| 71 | * @return object of ISISAreaIdentifierTlv |
| 72 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 73 | public static IsisAreaIdentifierTlv of(final byte[] raw, short hLength) { |
| 74 | return new IsisAreaIdentifierTlv(raw, hLength); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns value of ISIS-Area-Identifier. |
| 79 | * |
| 80 | * @return byte array of rawValue |
| 81 | */ |
| 82 | public byte[] getValue() { |
| 83 | return rawValue; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public PcepVersion getVersion() { |
| 88 | return PcepVersion.PCEP_1; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public short getType() { |
| 93 | return TYPE; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public short getLength() { |
| 98 | return hLength; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int hashCode() { |
Shashikanth VH | e8b45b7 | 2015-12-01 08:00:12 +0530 | [diff] [blame] | 103 | return Objects.hash(Arrays.hashCode(rawValue), rawValue.length); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean equals(Object obj) { |
| 108 | if (this == obj) { |
| 109 | return true; |
| 110 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 111 | if (obj instanceof IsisAreaIdentifierTlv) { |
| 112 | IsisAreaIdentifierTlv other = (IsisAreaIdentifierTlv) obj; |
Shashikanth VH | e8b45b7 | 2015-12-01 08:00:12 +0530 | [diff] [blame] | 113 | return Objects.equals(hLength, other.hLength) && Arrays.equals(rawValue, other.rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int write(ChannelBuffer c) { |
| 120 | int iLenStartIndex = c.writerIndex(); |
| 121 | c.writeShort(TYPE); |
| 122 | c.writeShort(hLength); |
| 123 | c.writeBytes(rawValue); |
| 124 | return c.writerIndex() - iLenStartIndex; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Reads the channel buffer and returns object of ISISAreaIdentifierTlv. |
| 129 | * |
| 130 | * @param c input channel buffer |
| 131 | * @param hLength length |
| 132 | * @return object of ISISAreaIdentifierTlv |
| 133 | */ |
| 134 | public static PcepValueType read(ChannelBuffer c, short hLength) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 135 | byte[] iIsisAreaIdentifier = new byte[hLength]; |
| 136 | c.readBytes(iIsisAreaIdentifier, 0, hLength); |
| 137 | return new IsisAreaIdentifierTlv(iIsisAreaIdentifier, hLength); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public String toString() { |
| 142 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass()); |
| 143 | |
| 144 | toStrHelper.add("Type", TYPE); |
| 145 | toStrHelper.add("Length", hLength); |
| 146 | |
| 147 | StringBuffer result = new StringBuffer(); |
| 148 | for (byte b : rawValue) { |
| 149 | result.append(String.format("%02X ", b)); |
| 150 | } |
| 151 | toStrHelper.add("Value", result); |
| 152 | |
| 153 | return toStrHelper.toString(); |
| 154 | } |
| 155 | } |