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 2016-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 | package org.onosproject.pcepio.types; |
| 17 | |
Satish K | 5bae20c | 2015-11-28 14:43:48 +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 router id. |
| 30 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 31 | public class IgpRouterIdSubTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 32 | |
| 33 | /* reference :I-D.ietf-idr-ls-distribution. |
| 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=[TBD13] | Length | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | opaque value | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | */ |
| 42 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 43 | protected static final Logger log = LoggerFactory.getLogger(IgpRouterIdSubTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 44 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 45 | public static final short TYPE = 4; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 46 | private final short hLength; |
| 47 | |
| 48 | private final byte[] rawValue; |
| 49 | |
| 50 | /** |
| 51 | * constructor to initialize rawValue. |
| 52 | * |
| 53 | * @param rawValue raw value |
| 54 | * @param hLength length |
| 55 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 56 | public IgpRouterIdSubTlv(byte[] rawValue, short hLength) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 57 | this.rawValue = rawValue; |
| 58 | if (0 == hLength) { |
| 59 | this.hLength = (short) rawValue.length; |
| 60 | } else { |
| 61 | this.hLength = hLength; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns object of Router ID Sub Tlv. |
| 67 | * |
| 68 | * @param raw value |
| 69 | * @param hLength length |
| 70 | * @return object of Router ID Sub Tlv |
| 71 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 72 | public static IgpRouterIdSubTlv of(final byte[] raw, short hLength) { |
| 73 | return new IgpRouterIdSubTlv(raw, hLength); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns raw value. |
| 78 | * |
| 79 | * @return rawValue value |
| 80 | */ |
| 81 | public byte[] getValue() { |
| 82 | return rawValue; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public PcepVersion getVersion() { |
| 87 | return PcepVersion.PCEP_1; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public short getType() { |
| 92 | return TYPE; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public short getLength() { |
| 97 | return hLength; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public int hashCode() { |
Satish K | 5bae20c | 2015-11-28 14:43:48 +0530 | [diff] [blame] | 102 | return Arrays.hashCode(rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object obj) { |
| 107 | if (this == obj) { |
| 108 | return true; |
| 109 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 110 | if (obj instanceof IgpRouterIdSubTlv) { |
| 111 | IgpRouterIdSubTlv other = (IgpRouterIdSubTlv) obj; |
Satish K | 5bae20c | 2015-11-28 14:43:48 +0530 | [diff] [blame] | 112 | return Arrays.equals(this.rawValue, other.rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public int write(ChannelBuffer c) { |
| 119 | int iLenStartIndex = c.writerIndex(); |
| 120 | c.writeShort(TYPE); |
| 121 | c.writeShort(hLength); |
| 122 | c.writeBytes(rawValue); |
| 123 | return c.writerIndex() - iLenStartIndex; |
| 124 | } |
| 125 | |
| 126 | /** |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 127 | * Reads channel buffer and returns object of IgpRouterIDTlv. |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 128 | * |
| 129 | * @param c input channel buffer |
| 130 | * @param hLength length |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 131 | * @return object of IgpRouterIDTlv |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 132 | */ |
| 133 | public static PcepValueType read(ChannelBuffer c, short hLength) { |
| 134 | byte[] iOpaqueValue = new byte[hLength]; |
| 135 | c.readBytes(iOpaqueValue, 0, hLength); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 136 | return new IgpRouterIdSubTlv(iOpaqueValue, hLength); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public String toString() { |
| 141 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass()); |
| 142 | |
| 143 | toStrHelper.add("Type", TYPE); |
| 144 | toStrHelper.add("Length", hLength); |
| 145 | |
| 146 | StringBuffer result = new StringBuffer(); |
| 147 | for (byte b : rawValue) { |
| 148 | result.append(String.format("%02X ", b)); |
| 149 | } |
| 150 | toStrHelper.add("Value", result); |
| 151 | |
| 152 | return toStrHelper.toString(); |
| 153 | } |
| 154 | } |