Shashikanth VH | 60e7398 | 2016-02-10 11:25:34 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.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; |
Shashikanth VH | 60e7398 | 2016-02-10 11:25:34 +0530 | [diff] [blame] | 26 | |
| 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 BgpFsSourcePrefix implements BgpValueType { |
| 34 | |
| 35 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_SRC_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 | 60e7398 | 2016-02-10 11:25:34 +0530 | [diff] [blame] | 39 | /** |
| 40 | * Constructor to initialize parameters. |
| 41 | * |
| 42 | * @param length length of the prefix |
| 43 | * @param ipPrefix ip prefix |
| 44 | */ |
| 45 | public BgpFsSourcePrefix(byte length, IpPrefix ipPrefix) { |
| 46 | this.ipPrefix = Preconditions.checkNotNull(ipPrefix); |
| 47 | this.length = length; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns ip prefix. |
| 52 | * |
| 53 | * @return ipPrefix ip prefix |
| 54 | */ |
| 55 | public IpPrefix ipPrefix() { |
| 56 | return ipPrefix; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public short getType() { |
| 61 | return this.FLOW_SPEC_TYPE; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int hashCode() { |
| 66 | return Objects.hash(ipPrefix); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean equals(Object obj) { |
| 71 | if (this == obj) { |
| 72 | return true; |
| 73 | } |
| 74 | if (obj instanceof BgpFsSourcePrefix) { |
| 75 | BgpFsSourcePrefix other = (BgpFsSourcePrefix) obj; |
| 76 | return Objects.equals(this.ipPrefix, other.ipPrefix) && Objects.equals(this.length, other.length); |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int write(ChannelBuffer cb) { |
| 83 | int iLenStartIndex = cb.writerIndex(); |
| 84 | cb.writeByte(FLOW_SPEC_TYPE); |
| 85 | cb.writeByte(length); |
| 86 | cb.writeInt(ipPrefix.getIp4Prefix().address().toInt()); |
| 87 | return cb.writerIndex() - iLenStartIndex; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Reads the channel buffer and returns object of IPv4AddressTlv. |
| 92 | * |
| 93 | * @param cb channelBuffer |
Shashikanth VH | 60e7398 | 2016-02-10 11:25:34 +0530 | [diff] [blame] | 94 | * @return object of flow spec source prefix |
| 95 | * @throws BgpParseException while parsing BgpFsSourcePrefix |
| 96 | */ |
Shashikanth VH | c53a413 | 2016-02-11 18:33:34 +0530 | [diff] [blame] | 97 | public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException { |
| 98 | IpPrefix ipPrefix; |
| 99 | |
| 100 | int length = cb.readByte(); |
| 101 | if (length == 0) { |
| 102 | byte[] prefix = new byte[] {0}; |
| 103 | ipPrefix = Validation.bytesToPrefix(prefix, length); |
| 104 | return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix); |
| 105 | } |
| 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 BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix); |
Shashikanth VH | 60e7398 | 2016-02-10 11:25:34 +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 BgpFsSourcePrefix of(final IpPrefix ipPrefix, final byte length) { |
| 131 | return new BgpFsSourcePrefix(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 BgpFsSourcePrefix) { |
| 141 | BgpFsSourcePrefix that = (BgpFsSourcePrefix) 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).add("length", length) |
| 162 | .add("ipPrefix", ipPrefix).toString(); |
| 163 | } |
| 164 | } |