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 |
|
| 17 | /**
|
| 18 | * @author b00295750
|
| 19 | *
|
| 20 | */
|
| 21 | package org.onosproject.pcepio.types;
|
| 22 |
|
| 23 | import java.util.Objects;
|
| 24 |
|
| 25 | import org.jboss.netty.buffer.ChannelBuffer;
|
| 26 | import org.onosproject.pcepio.protocol.PcepVersion;
|
| 27 | import org.slf4j.Logger;
|
| 28 | import org.slf4j.LoggerFactory;
|
| 29 |
|
| 30 | import com.google.common.base.MoreObjects;
|
| 31 |
|
| 32 | /**
|
| 33 | * Provides IPv4 Sub Object.
|
| 34 | */
|
| 35 | public class IPv4SubObject implements PcepValueType {
|
| 36 |
|
| 37 | /*Reference : RFC 4874:3.1.1
|
| 38 | * 0 1 2 3
|
| 39 | 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
|
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 41 | |L| Type | Length | IPv4 address (4 bytes) |
|
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 43 | | IPv4 address (continued) | Prefix Length | Resvd |
|
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 45 | */
|
| 46 | protected static final Logger log = LoggerFactory.getLogger(IPv4SubObject.class);
|
| 47 |
|
| 48 | public static final byte TYPE = 0x01;
|
| 49 | public static final byte LENGTH = 8;
|
| 50 | public static final byte VALUE_LENGTH = 6;
|
| 51 | public static final byte OBJ_LENGTH = 8;
|
| 52 | public static final byte LBIT = 0;
|
| 53 | public static final int SHIFT_LBIT_POSITION = 7;
|
| 54 | private int ipAddress;
|
| 55 | private byte prefixLen;
|
| 56 | private byte resvd;
|
| 57 |
|
| 58 | /**
|
| 59 | * Constructor to initialize ipv4 address.
|
| 60 | *
|
| 61 | * @param ipAddr ipv4 address
|
| 62 | */
|
| 63 | public IPv4SubObject(int ipAddr) {
|
| 64 | this.ipAddress = ipAddr;
|
| 65 | }
|
| 66 |
|
| 67 | /**
|
| 68 | * constructor to initialize ipAddress, prefixLen and resvd.
|
| 69 | *
|
| 70 | * @param ipAddress ipv4 address
|
| 71 | * @param prefixLen prefix length
|
| 72 | * @param resvd reserved flags value
|
| 73 | */
|
| 74 | public IPv4SubObject(int ipAddress, byte prefixLen, byte resvd) {
|
| 75 | this.ipAddress = ipAddress;
|
| 76 | this.prefixLen = prefixLen;
|
| 77 | this.resvd = resvd;
|
| 78 | }
|
| 79 |
|
| 80 | /**
|
| 81 | * Returns a new instance of IPv4SubObject.
|
| 82 | *
|
| 83 | * @param ipAddress ipv4 address
|
| 84 | * @param prefixLen prefix length
|
| 85 | * @param resvd reserved flags value
|
| 86 | * @return object of IPv4SubObject
|
| 87 | */
|
| 88 | public static IPv4SubObject of(int ipAddress, byte prefixLen, byte resvd) {
|
| 89 | return new IPv4SubObject(ipAddress, prefixLen, resvd);
|
| 90 | }
|
| 91 |
|
| 92 | /**
|
| 93 | * Returns prefixLen of IPv4 IP address.
|
| 94 | *
|
| 95 | * @return byte value of rawValue
|
| 96 | */
|
| 97 | public byte getPrefixLen() {
|
| 98 | return prefixLen;
|
| 99 | }
|
| 100 |
|
| 101 | /**
|
| 102 | * Returns value of IPv4 IP address.
|
| 103 | *
|
| 104 | * @return int value of ipv4 address
|
| 105 | */
|
| 106 | public int getIpAddress() {
|
| 107 | return ipAddress;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public PcepVersion getVersion() {
|
| 112 | return PcepVersion.PCEP_1;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public short getType() {
|
| 117 | return TYPE;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public short getLength() {
|
| 122 | return LENGTH;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public int hashCode() {
|
| 127 | return Objects.hash(ipAddress, prefixLen, resvd);
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean equals(Object obj) {
|
| 132 | if (this == obj) {
|
| 133 | return true;
|
| 134 | }
|
| 135 | if (obj instanceof IPv4SubObject) {
|
| 136 | IPv4SubObject other = (IPv4SubObject) obj;
|
| 137 | return Objects.equals(this.ipAddress, other.ipAddress) && Objects.equals(this.prefixLen, other.prefixLen)
|
| 138 | && Objects.equals(this.resvd, other.resvd);
|
| 139 | }
|
| 140 | return false;
|
| 141 | }
|
| 142 |
|
| 143 | /**
|
| 144 | * Reads the channel buffer and returns object of IPv4SubObject.
|
| 145 | *
|
| 146 | * @param c type of channel buffer
|
| 147 | * @return object of IPv4SubObject
|
| 148 | */
|
| 149 | public static PcepValueType read(ChannelBuffer c) {
|
| 150 | int ipAddess = c.readInt();
|
| 151 | byte prefixLen = c.readByte();
|
| 152 | byte resvd = c.readByte();
|
| 153 | return new IPv4SubObject(ipAddess, prefixLen, resvd);
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public int write(ChannelBuffer c) {
|
| 158 | int iLenStartIndex = c.writerIndex();
|
| 159 | byte bValue = LBIT;
|
| 160 | bValue = (byte) (bValue << SHIFT_LBIT_POSITION);
|
| 161 | bValue = (byte) (bValue | TYPE);
|
| 162 | c.writeByte(bValue);
|
| 163 | c.writeByte(OBJ_LENGTH);
|
| 164 | c.writeInt(ipAddress);
|
| 165 | c.writeByte(prefixLen);
|
| 166 | c.writeByte(resvd);
|
| 167 |
|
| 168 | return c.writerIndex() - iLenStartIndex;
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 172 | public String toString() {
|
| 173 | return MoreObjects.toStringHelper(getClass())
|
| 174 | .add("Type", TYPE)
|
| 175 | .add("Length", LENGTH)
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 176 | .add("IPv4Address", ipAddress)
|
| 177 | .add("PrefixLength", prefixLen)
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 178 | .toString();
|
| 179 | }
|
| 180 | }
|