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 2015-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; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 24 | import java.util.Objects; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Provides area ID for OSPF area. |
| 28 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 29 | public class OspfAreaIdSubTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 30 | |
| 31 | /* Reference :draft-ietf-idr-ls-distribution-10. |
| 32 | * 0 1 2 3 |
| 33 | 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 |
| 34 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 35 | | Type=[TBD12] | Length=4 | |
| 36 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | | opaque value (32 Bit AS Number) | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | */ |
| 40 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 41 | protected static final Logger log = LoggerFactory.getLogger(OspfAreaIdSubTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 42 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 43 | public static final short TYPE = 3; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 44 | public static final short LENGTH = 4; |
| 45 | |
| 46 | private final int rawValue; |
| 47 | |
| 48 | /** |
| 49 | * constructor to initialize rawValue. |
| 50 | * |
| 51 | * @param rawValue area ID for OSPF area. |
| 52 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 53 | public OspfAreaIdSubTlv(int rawValue) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 54 | this.rawValue = rawValue; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns newly created OSPFareaIDsubTlv object. |
| 59 | * |
| 60 | * @param raw opaque value of AreaID |
| 61 | * @return new object of OSPF area ID sub TLV |
| 62 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 63 | public static OspfAreaIdSubTlv of(final int raw) { |
| 64 | return new OspfAreaIdSubTlv(raw); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns RawValue opaque value of AreaID. |
| 69 | * |
| 70 | * @return rawValue Area ID |
| 71 | */ |
| 72 | public int getInt() { |
| 73 | return rawValue; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public PcepVersion getVersion() { |
| 78 | return PcepVersion.PCEP_1; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public short getType() { |
| 83 | return TYPE; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public short getLength() { |
| 88 | return LENGTH; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int hashCode() { |
| 93 | return Objects.hash(rawValue); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean equals(Object obj) { |
| 98 | if (this == obj) { |
| 99 | return true; |
| 100 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 101 | if (obj instanceof OspfAreaIdSubTlv) { |
| 102 | OspfAreaIdSubTlv other = (OspfAreaIdSubTlv) obj; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 103 | return Objects.equals(this.rawValue, other.rawValue); |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int write(ChannelBuffer c) { |
| 110 | int iLenStartIndex = c.writerIndex(); |
| 111 | c.writeShort(TYPE); |
| 112 | c.writeShort(LENGTH); |
| 113 | c.writeInt(rawValue); |
| 114 | return c.writerIndex() - iLenStartIndex; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Reads the channel buffer and returns object of OSPFAreaIdSubTlv. |
| 119 | * |
| 120 | * @param c input channel buffer |
| 121 | * @return object of OSPFAreaIdSubTlv |
| 122 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 123 | public static OspfAreaIdSubTlv read(ChannelBuffer c) { |
| 124 | return OspfAreaIdSubTlv.of(c.readInt()); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 129 | return MoreObjects.toStringHelper(getClass()) |
| 130 | .add("Type", TYPE) |
| 131 | .add("Length", LENGTH) |
| 132 | .add("Value", rawValue) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 133 | .toString(); |
| 134 | } |
| 135 | } |