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 | 2cee68c | 2015-11-28 14:54:12 +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 SharedRiskLinkGroupTlv. |
| 30 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 31 | public class SharedRiskLinkGroupSubTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Reference :[I-D.ietf-idr- Group ls-distribution] /3.3.2.5 |
| 35 | * |
| 36 | * 0 1 2 3 |
| 37 | 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 |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | Type =TDB41 | Length | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | Shared Risk Link Group Value | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | // ............ // |
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | | Shared Risk Link Group Value | |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | */ |
| 48 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 49 | protected static final Logger log = LoggerFactory.getLogger(SharedRiskLinkGroupSubTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 50 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 51 | public static final short TYPE = 30; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 52 | |
| 53 | private final short hLength; |
| 54 | |
| 55 | private final int[] srlgValue; |
| 56 | |
| 57 | /** |
| 58 | * Constructor to initialize SRLG value. |
| 59 | * |
| 60 | * @param srlgValue Shared Risk Link Group Value |
| 61 | * @param hLength length |
| 62 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 63 | public SharedRiskLinkGroupSubTlv(int[] srlgValue, short hLength) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 64 | this.srlgValue = srlgValue; |
| 65 | if (0 == hLength) { |
| 66 | this.hLength = (short) ((srlgValue.length) * 4); |
| 67 | } else { |
| 68 | this.hLength = hLength; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns object of SharedRiskLinkGroupTlv. |
| 74 | * |
| 75 | * @param raw value |
| 76 | * @param hLength length |
| 77 | * @return object of SharedRiskLinkGroupTlv |
| 78 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 79 | public static SharedRiskLinkGroupSubTlv of(final int[] raw, short hLength) { |
| 80 | return new SharedRiskLinkGroupSubTlv(raw, hLength); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns SRLG Value. |
| 85 | * |
| 86 | * @return srlgValue |
| 87 | */ |
| 88 | public int[] getValue() { |
| 89 | return srlgValue; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public PcepVersion getVersion() { |
| 94 | return PcepVersion.PCEP_1; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public short getType() { |
| 99 | return TYPE; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public short getLength() { |
| 104 | return hLength; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
Satish K | 2cee68c | 2015-11-28 14:54:12 +0530 | [diff] [blame] | 109 | return Arrays.hashCode(srlgValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean equals(Object obj) { |
| 114 | if (this == obj) { |
| 115 | return true; |
| 116 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 117 | if (obj instanceof SharedRiskLinkGroupSubTlv) { |
| 118 | SharedRiskLinkGroupSubTlv other = (SharedRiskLinkGroupSubTlv) obj; |
Satish K | 2cee68c | 2015-11-28 14:54:12 +0530 | [diff] [blame] | 119 | return Arrays.equals(this.srlgValue, other.srlgValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int write(ChannelBuffer c) { |
| 126 | int iLenStartIndex = c.writerIndex(); |
| 127 | c.writeShort(TYPE); |
| 128 | c.writeShort(hLength); |
| 129 | for (int b : srlgValue) { |
| 130 | c.writeInt(b); |
| 131 | } |
| 132 | return c.writerIndex() - iLenStartIndex; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Reads from channel buffer and returns object of SharedRiskLinkGroupTlv. |
| 137 | * |
| 138 | * @param c input channel buffer |
| 139 | * @param hLength length |
| 140 | * @return object of SharedRiskLinkGroupTlv |
| 141 | */ |
| 142 | public static PcepValueType read(ChannelBuffer c, short hLength) { |
| 143 | int iLength = hLength / 4; |
| 144 | int[] iSharedRiskLinkGroup = new int[iLength]; |
| 145 | for (int i = 0; i < iLength; i++) { |
| 146 | iSharedRiskLinkGroup[i] = c.readInt(); |
| 147 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 148 | return new SharedRiskLinkGroupSubTlv(iSharedRiskLinkGroup, hLength); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
| 152 | @Override |
| 153 | public String toString() { |
| 154 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass()); |
| 155 | |
| 156 | toStrHelper.add("Type", TYPE); |
| 157 | toStrHelper.add("Length", hLength); |
| 158 | |
| 159 | StringBuffer result = new StringBuffer(); |
| 160 | for (int b : srlgValue) { |
| 161 | result.append(String.format("%02X ", b)); |
| 162 | } |
| 163 | toStrHelper.add("Value", result); |
| 164 | |
| 165 | return toStrHelper.toString(); |
| 166 | } |
| 167 | } |