bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [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 | package org.onosproject.pcepio.types;
|
| 17 |
|
| 18 | import java.util.Objects;
|
| 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 |
|
| 27 | /**
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 28 | * Provides Autonomous-System-Tlv which contains opaque value (32 Bit AS Number).
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 29 | */
|
| 30 | public class AutonomousSystemTlv implements PcepValueType {
|
| 31 |
|
| 32 | /* Reference :RFC3209
|
| 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=[TBD10] | Length=4 |
|
| 37 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 38 | | opaque value (32 Bit AS Number) |
|
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 40 | */
|
| 41 |
|
| 42 | protected static final Logger log = LoggerFactory.getLogger(AutonomousSystemTlv.class);
|
| 43 |
|
| 44 | public static final short TYPE = 100; //TODD:change this TBD10
|
| 45 | public static final short LENGTH = 4;
|
| 46 |
|
| 47 | private final int rawValue;
|
| 48 |
|
| 49 | /**
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 50 | * Constructor to initialize rawValue.
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 51 | *
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 52 | * @param rawValue Autonomous-System-Tlv
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 53 | */
|
| 54 | public AutonomousSystemTlv(int rawValue) {
|
| 55 | this.rawValue = rawValue;
|
| 56 | }
|
| 57 |
|
| 58 | /**
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 59 | * Returns newly created AutonomousSystemTlv object.
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 60 | *
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 61 | * @param raw value of opaque.
|
| 62 | * @return object of Autonomous-System-Tlv
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 63 | */
|
| 64 | public static AutonomousSystemTlv of(final int raw) {
|
| 65 | return new AutonomousSystemTlv(raw);
|
| 66 | }
|
| 67 |
|
| 68 | /**
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 69 | * Returns opaque value.
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 70 | *
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 71 | * @return rawValue opaque value.
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 72 | */
|
| 73 | public int getInt() {
|
| 74 | return rawValue;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public PcepVersion getVersion() {
|
| 79 | return PcepVersion.PCEP_1;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public short getType() {
|
| 84 | return TYPE;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public short getLength() {
|
| 89 | return LENGTH;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public int hashCode() {
|
| 94 | return Objects.hash(rawValue);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean equals(Object obj) {
|
| 99 | if (this == obj) {
|
| 100 | return true;
|
| 101 | }
|
| 102 | if (obj instanceof AutonomousSystemTlv) {
|
| 103 | AutonomousSystemTlv other = (AutonomousSystemTlv) obj;
|
| 104 | return Objects.equals(rawValue, other.rawValue);
|
| 105 | }
|
| 106 | return false;
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public int write(ChannelBuffer c) {
|
| 111 | int iLenStartIndex = c.writerIndex();
|
| 112 | c.writeShort(TYPE);
|
| 113 | c.writeShort(LENGTH);
|
| 114 | c.writeInt(rawValue);
|
| 115 | return c.writerIndex() - iLenStartIndex;
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * Reads the channel buffer and returns object of AutonomousSystemTlv.
|
| 120 | *
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 121 | * @param c input channel buffer
|
| 122 | * @return object of Autonomous-System-Tlv
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 123 | */
|
| 124 | public static AutonomousSystemTlv read(ChannelBuffer c) {
|
| 125 | return AutonomousSystemTlv.of(c.readInt());
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 129 | public String toString() {
|
| 130 | return MoreObjects.toStringHelper(getClass())
|
| 131 | .add("TYPE", TYPE)
|
| 132 | .add("Length", LENGTH)
|
| 133 | .add("value", rawValue)
|
| 134 | .toString();
|
| 135 | }
|
| 136 | }
|