Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -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 | */ |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 16 | package org.onosproject.pcepio.types; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | /** |
| 27 | * Provides PceccCapabilityTlv. |
| 28 | */ |
| 29 | public class PceccCapabilityTlv implements PcepValueType { |
| 30 | |
| 31 | /* PCECC CAPABILITY TLV |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 32 | * Reference : draft-zhao-pce-pcep-extension-for-pce-controller-03, section-7.1.1 |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 33 | |
| 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 |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 36 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | | Type=[TBD] | Length=4 | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | Flags |S| |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 41 | |
| 42 | */ |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 43 | private static final Logger log = LoggerFactory.getLogger(PceccCapabilityTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 44 | |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 45 | public static final short TYPE = (short) 65287; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 46 | public static final short LENGTH = 4; |
| 47 | public static final int SET = 1; |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 48 | public static final byte SBIT_CHECK = 0x01; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 49 | |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 50 | private final boolean sBit; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 51 | |
| 52 | private final int rawValue; |
| 53 | private final boolean isRawValueSet; |
| 54 | |
| 55 | /** |
| 56 | * Constructor to initialize raw Value. |
| 57 | * |
| 58 | * @param rawValue raw value |
| 59 | */ |
| 60 | public PceccCapabilityTlv(final int rawValue) { |
| 61 | this.rawValue = rawValue; |
| 62 | this.isRawValueSet = true; |
| 63 | |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 64 | sBit = (rawValue & SBIT_CHECK) == SBIT_CHECK; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Constructor to initialize G-flag L-flag. |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 69 | * |
| 70 | * @param sBit pcecc sr capbaility bit |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 71 | */ |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 72 | public PceccCapabilityTlv(boolean sBit) { |
| 73 | this.sBit = sBit; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 74 | this.rawValue = 0; |
| 75 | this.isRawValueSet = false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns newly created PceccCapabilityTlv object. |
| 80 | * |
| 81 | * @param raw value |
| 82 | * @return object of Pcecc Capability Tlv |
| 83 | */ |
| 84 | public static PceccCapabilityTlv of(final int raw) { |
| 85 | return new PceccCapabilityTlv(raw); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public PcepVersion getVersion() { |
| 90 | return PcepVersion.PCEP_1; |
| 91 | } |
| 92 | |
| 93 | /** |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 94 | * Returns sBit. |
| 95 | * |
| 96 | * @return sBit S bit |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 97 | */ |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 98 | public boolean sBit() { |
| 99 | return sBit; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the raw value. |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 104 | * |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 105 | * @return rawValue Flags |
| 106 | */ |
| 107 | public int getInt() { |
| 108 | return rawValue; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public short getType() { |
| 113 | return TYPE; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public short getLength() { |
| 118 | return LENGTH; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
| 123 | if (isRawValueSet) { |
| 124 | return Objects.hash(rawValue); |
| 125 | } else { |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 126 | return Objects.hash(sBit); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean equals(Object obj) { |
| 132 | if (this == obj) { |
| 133 | return true; |
| 134 | } |
| 135 | if (obj instanceof PceccCapabilityTlv) { |
| 136 | PceccCapabilityTlv other = (PceccCapabilityTlv) obj; |
| 137 | if (isRawValueSet) { |
| 138 | return Objects.equals(this.rawValue, other.rawValue); |
| 139 | } else { |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 140 | return Objects.equals(this.sBit, other.sBit); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int write(ChannelBuffer c) { |
| 148 | int iLenStartIndex = c.writerIndex(); |
| 149 | int temp = 0; |
| 150 | c.writeShort(TYPE); |
| 151 | c.writeShort(LENGTH); |
| 152 | if (isRawValueSet) { |
| 153 | c.writeInt(rawValue); |
| 154 | } else { |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 155 | if (sBit) { |
| 156 | temp = temp | SBIT_CHECK; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 157 | } |
| 158 | c.writeInt(temp); |
| 159 | } |
| 160 | return c.writerIndex() - iLenStartIndex; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Reads channel buffer and returns object of PceccCapabilityTlv. |
| 165 | * |
| 166 | * @param c input channel buffer |
| 167 | * @return object of PceccCapabilityTlv |
| 168 | */ |
| 169 | public static PceccCapabilityTlv read(ChannelBuffer c) { |
| 170 | return PceccCapabilityTlv.of(c.readInt()); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 175 | return MoreObjects.toStringHelper(getClass()) |
| 176 | .add("Type", TYPE) |
| 177 | .add("Length", LENGTH) |
Priyanka B | 21f4b73 | 2016-03-21 20:54:12 +0530 | [diff] [blame] | 178 | .add("rawValue", rawValue) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 179 | .toString(); |
| 180 | } |
| 181 | } |