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 | |
| 17 | package org.onosproject.pcepio.types; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import com.google.common.base.MoreObjects; |
| 27 | |
| 28 | /** |
| 29 | * Provides GMPLS Capability Tlv. |
| 30 | */ |
| 31 | public class GmplsCapabilityTlv implements PcepValueType { |
| 32 | |
| 33 | /* |
| 34 | * GMPLS-CAPABILITY TLV format |
| 35 | * reference :draft-ietf-pce-gmpls-pcep-extensions -2.1.1 |
| 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=14 | Length | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | Flags | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | */ |
| 44 | protected static final Logger log = LoggerFactory.getLogger(GmplsCapabilityTlv.class); |
| 45 | |
| 46 | public static final short TYPE = 14; |
| 47 | public static final short LENGTH = 4; |
| 48 | |
| 49 | private final int rawValue; |
| 50 | |
| 51 | /** |
| 52 | * Constructor to initialize raw value. |
| 53 | * |
| 54 | * @param rawValue of Gmpls-Capability-Tlv |
| 55 | */ |
| 56 | public GmplsCapabilityTlv(int rawValue) { |
| 57 | this.rawValue = rawValue; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns newly created GmplsCapabilityTlv object. |
| 62 | * |
| 63 | * @param raw Flags value |
| 64 | * @return object of Gmpls-Capability-Tlv |
| 65 | */ |
| 66 | public static GmplsCapabilityTlv of(final int raw) { |
| 67 | return new GmplsCapabilityTlv(raw); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns value of Flags. |
| 72 | * |
| 73 | * @return rawValue Flags |
| 74 | */ |
| 75 | public int getInt() { |
| 76 | return rawValue; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public PcepVersion getVersion() { |
| 81 | return PcepVersion.PCEP_1; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public short getType() { |
| 86 | return TYPE; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public short getLength() { |
| 91 | return LENGTH; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return Objects.hash(rawValue); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (obj instanceof GmplsCapabilityTlv) { |
| 105 | GmplsCapabilityTlv other = (GmplsCapabilityTlv) obj; |
| 106 | return Objects.equals(rawValue, other.rawValue); |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int write(ChannelBuffer c) { |
| 113 | int iLenStartIndex = c.writerIndex(); |
| 114 | c.writeShort(TYPE); |
| 115 | c.writeShort(LENGTH); |
| 116 | c.writeInt(rawValue); |
| 117 | return c.writerIndex() - iLenStartIndex; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Reads the channel buffer and returns object of Gmpls-Capability-Tlv. |
| 122 | * |
| 123 | * @param c input channel buffer |
| 124 | * @return object of Gmpls-Capability-Tlv |
| 125 | */ |
| 126 | public static GmplsCapabilityTlv read(ChannelBuffer c) { |
| 127 | return GmplsCapabilityTlv.of(c.readInt()); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 132 | return MoreObjects.toStringHelper(getClass()) |
| 133 | .add("Type", TYPE) |
| 134 | .add("Length", LENGTH) |
| 135 | .add("Value", rawValue) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 136 | .toString(); |
| 137 | } |
| 138 | } |