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