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 | |
| 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 | * NexthopIPv6addressTlv provides Ipv4 address of next hop. |
| 30 | */ |
| 31 | public class NexthopIPv4addressTlv implements PcepValueType { |
| 32 | |
| 33 | /* |
| 34 | Reference :draft-zhao-pce-pcep-extension-for-pce-controller-01 |
| 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 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Avantika-Huawei | 3c2d3eb | 2016-06-22 09:34:00 +0530 | [diff] [blame] | 39 | | Type=TBD | Length = 4 | |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | nexthop IPv4 address | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | |
| 44 | NEXTHOP-IPV4-ADDRESS TLV |
| 45 | |
| 46 | */ |
| 47 | protected static final Logger log = LoggerFactory.getLogger(NexthopIPv4addressTlv.class); |
| 48 | |
Avantika-Huawei | 3c2d3eb | 2016-06-22 09:34:00 +0530 | [diff] [blame] | 49 | public static final short TYPE = (short) 65289; //to be defined |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 50 | //Length is header + value |
| 51 | public static final short LENGTH = 8; |
| 52 | public static final short VALUE_LENGTH = 4; |
| 53 | |
| 54 | private final int rawValue; |
| 55 | |
| 56 | /** |
| 57 | * Constructor to initialize next hop IPv4 address. |
| 58 | * |
| 59 | * @param rawValue next hop IPv4 address |
| 60 | */ |
| 61 | public NexthopIPv4addressTlv(int rawValue) { |
| 62 | this.rawValue = rawValue; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return next hop IPv4 address tlv. |
| 67 | * |
| 68 | * @param raw of next hop IPv4 address |
| 69 | * @return object of NexthopIPv4addressTlv |
| 70 | */ |
| 71 | public static NexthopIPv4addressTlv of(final int raw) { |
| 72 | return new NexthopIPv4addressTlv(raw); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns next hop IPv4 address. |
| 77 | * |
| 78 | * @return next hop IPv4 address |
| 79 | */ |
| 80 | public int getInt() { |
| 81 | return rawValue; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public PcepVersion getVersion() { |
| 86 | return PcepVersion.PCEP_1; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public short getType() { |
| 91 | return TYPE; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public short getLength() { |
Avantika-Huawei | 3c2d3eb | 2016-06-22 09:34:00 +0530 | [diff] [blame] | 96 | return VALUE_LENGTH; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int hashCode() { |
| 101 | return Objects.hash(rawValue); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean equals(Object obj) { |
| 106 | if (this == obj) { |
| 107 | return true; |
| 108 | } |
| 109 | if (obj instanceof NexthopIPv4addressTlv) { |
| 110 | NexthopIPv4addressTlv other = (NexthopIPv4addressTlv) obj; |
| 111 | return Objects.equals(this.rawValue, other.rawValue); |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int write(ChannelBuffer c) { |
| 118 | int iStartIndex = c.writerIndex(); |
| 119 | c.writeShort(TYPE); |
Avantika-Huawei | 3c2d3eb | 2016-06-22 09:34:00 +0530 | [diff] [blame] | 120 | c.writeShort(VALUE_LENGTH); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 121 | c.writeInt(rawValue); |
| 122 | return c.writerIndex() - iStartIndex; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Reads the channel buffer and returns object of NexthopIPv4addressTlv. |
| 127 | * |
| 128 | * @param c type of channel buffer |
| 129 | * @return object of NexthopIPv4addressTlv |
| 130 | */ |
| 131 | public static NexthopIPv4addressTlv read(ChannelBuffer c) { |
| 132 | return NexthopIPv4addressTlv.of(c.readInt()); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String toString() { |
| 137 | return MoreObjects.toStringHelper(getClass()) |
| 138 | .add("Type", TYPE) |
Avantika-Huawei | 3c2d3eb | 2016-06-22 09:34:00 +0530 | [diff] [blame] | 139 | .add("Length", VALUE_LENGTH) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 140 | .add("Ipv4Address ", rawValue) |
| 141 | .toString(); |
| 142 | } |
| 143 | } |