Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [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.bgpio.types; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 24 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | c53a413 | 2016-02-11 18:33:34 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.util.Validation; |
| 26 | |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 27 | import com.google.common.base.MoreObjects; |
| 28 | import com.google.common.base.Preconditions; |
| 29 | |
| 30 | /** |
| 31 | * Provides implementation of IPv4AddressTlv. |
| 32 | */ |
| 33 | public class BgpFsDestinationPrefix implements BgpValueType { |
| 34 | |
| 35 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DST_PREFIX; |
| 36 | private byte length; |
| 37 | private IpPrefix ipPrefix; |
Shashikanth VH | c53a413 | 2016-02-11 18:33:34 +0530 | [diff] [blame] | 38 | public static final int BYTE_IN_BITS = 8; |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Constructor to initialize parameters. |
| 42 | * |
| 43 | * @param length length of the prefix |
| 44 | * @param ipPrefix ip prefix |
| 45 | */ |
| 46 | public BgpFsDestinationPrefix(byte length, IpPrefix ipPrefix) { |
| 47 | this.ipPrefix = Preconditions.checkNotNull(ipPrefix); |
| 48 | this.length = length; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns ip prefix. |
| 53 | * |
| 54 | * @return ipPrefix ip prefix |
| 55 | */ |
| 56 | public IpPrefix ipPrefix() { |
| 57 | return ipPrefix; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public short getType() { |
| 62 | return FLOW_SPEC_TYPE; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int hashCode() { |
| 67 | return Objects.hash(ipPrefix); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean equals(Object obj) { |
| 72 | if (this == obj) { |
| 73 | return true; |
| 74 | } |
| 75 | if (obj instanceof BgpFsDestinationPrefix) { |
| 76 | BgpFsDestinationPrefix other = (BgpFsDestinationPrefix) obj; |
| 77 | return Objects.equals(this.ipPrefix, other.ipPrefix); |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int write(ChannelBuffer cb) { |
| 84 | int iLenStartIndex = cb.writerIndex(); |
| 85 | cb.writeByte(FLOW_SPEC_TYPE); |
| 86 | cb.writeByte(length); |
| 87 | cb.writeInt(ipPrefix.getIp4Prefix().address().toInt()); |
| 88 | return cb.writerIndex() - iLenStartIndex; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Reads the channel buffer and returns object of IPv4AddressTlv. |
| 93 | * |
| 94 | * @param cb channelBuffer |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 95 | * @return object of flow spec destination prefix |
| 96 | * @throws BgpParseException while parsing BgpFsDestinationPrefix |
| 97 | */ |
Shashikanth VH | c53a413 | 2016-02-11 18:33:34 +0530 | [diff] [blame] | 98 | public static BgpFsDestinationPrefix read(ChannelBuffer cb) throws BgpParseException { |
| 99 | IpPrefix ipPrefix; |
| 100 | |
| 101 | int length = cb.readByte(); |
| 102 | if (length == 0) { |
| 103 | byte[] prefix = new byte[] {0}; |
| 104 | ipPrefix = Validation.bytesToPrefix(prefix, length); |
| 105 | return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix); |
| 106 | } |
| 107 | int len = length / BYTE_IN_BITS; |
| 108 | int reminder = length % BYTE_IN_BITS; |
| 109 | if (reminder > 0) { |
| 110 | len = len + 1; |
| 111 | } |
| 112 | if (cb.readableBytes() < len) { |
| 113 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 114 | BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes()); |
| 115 | } |
| 116 | byte[] prefix = new byte[len]; |
| 117 | cb.readBytes(prefix, 0, len); |
| 118 | ipPrefix = Validation.bytesToPrefix(prefix, length); |
| 119 | |
| 120 | return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix); |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns object of this class with specified values. |
| 125 | * |
| 126 | * @param ipPrefix ip prefix |
| 127 | * @param length length of ip prefix |
| 128 | * @return object of this class |
| 129 | */ |
| 130 | public static BgpFsDestinationPrefix of(final IpPrefix ipPrefix, final byte length) { |
| 131 | return new BgpFsDestinationPrefix(length, ipPrefix); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public int compareTo(Object o) { |
| 136 | if (this.equals(o)) { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | if (o instanceof BgpFsDestinationPrefix) { |
| 141 | BgpFsDestinationPrefix that = (BgpFsDestinationPrefix) o; |
| 142 | |
| 143 | if (this.ipPrefix().prefixLength() == that.ipPrefix().prefixLength()) { |
| 144 | ByteBuffer value1 = ByteBuffer.wrap(this.ipPrefix.address().toOctets()); |
| 145 | ByteBuffer value2 = ByteBuffer.wrap(that.ipPrefix.address().toOctets()); |
| 146 | return value1.compareTo(value2); |
| 147 | } |
| 148 | |
| 149 | if (this.ipPrefix().prefixLength() > that.ipPrefix().prefixLength()) { |
| 150 | return 1; |
| 151 | } else if (this.ipPrefix().prefixLength() < that.ipPrefix().prefixLength()) { |
| 152 | return -1; |
| 153 | } |
| 154 | } |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public String toString() { |
| 160 | return MoreObjects.toStringHelper(getClass()) |
| 161 | .add("FLOW_SPEC_TYPE", FLOW_SPEC_TYPE) |
| 162 | .add("length", length) |
| 163 | .add("ipPrefix", ipPrefix).toString(); |
| 164 | } |
| 165 | } |