blob: a36fc01072723579995a15611be6bc30eca7b107 [file] [log] [blame]
Shashikanth VH60e73982016-02-10 11:25:34 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Shashikanth VH60e73982016-02-10 11:25:34 +05303 *
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 */
16package org.onosproject.bgpio.types;
17
mohamedrahil00f6f262016-11-24 20:20:41 +053018import com.google.common.base.MoreObjects;
19import com.google.common.base.Preconditions;
Shashikanth VH60e73982016-02-10 11:25:34 +053020import org.jboss.netty.buffer.ChannelBuffer;
21import org.onlab.packet.IpPrefix;
22import org.onosproject.bgpio.exceptions.BgpParseException;
23import org.onosproject.bgpio.util.Constants;
Shashikanth VHc53a4132016-02-11 18:33:34 +053024import org.onosproject.bgpio.util.Validation;
Shashikanth VH60e73982016-02-10 11:25:34 +053025
mohamedrahil00f6f262016-11-24 20:20:41 +053026import java.nio.ByteBuffer;
27import java.util.Objects;
Shashikanth VH60e73982016-02-10 11:25:34 +053028
29/**
30 * Provides implementation of IPv4AddressTlv.
31 */
32public class BgpFsSourcePrefix implements BgpValueType {
33
34 public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_SRC_PREFIX;
mohamedrahil00f6f262016-11-24 20:20:41 +053035 public static final int BYTE_IN_BITS = 8;
Shashikanth VH60e73982016-02-10 11:25:34 +053036 private byte length;
37 private IpPrefix ipPrefix;
mohamedrahil00f6f262016-11-24 20:20:41 +053038
Shashikanth VH60e73982016-02-10 11:25:34 +053039 /**
40 * Constructor to initialize parameters.
41 *
mohamedrahil00f6f262016-11-24 20:20:41 +053042 * @param length length of the prefix
Shashikanth VH60e73982016-02-10 11:25:34 +053043 * @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 /**
mohamedrahil00f6f262016-11-24 20:20:41 +053051 * Reads the channel buffer and returns object of IPv4AddressTlv.
52 *
53 * @param cb channelBuffer
54 * @return object of flow spec source prefix
55 * @throws BgpParseException while parsing BgpFsSourcePrefix
56 */
57 public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException {
58 IpPrefix ipPrefix;
59
60 int length = cb.readByte();
61 if (length == 0) {
62 byte[] prefix = new byte[]{0};
63 ipPrefix = Validation.bytesToPrefix(prefix, length);
64 return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
65 }
66
67 int len = length / BYTE_IN_BITS;
68 int reminder = length % BYTE_IN_BITS;
69 if (reminder > 0) {
70 len = len + 1;
71 }
72 if (cb.readableBytes() < len) {
73 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
74 BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes());
75 }
76 byte[] prefix = new byte[len];
77 cb.readBytes(prefix, 0, len);
78 ipPrefix = Validation.bytesToPrefix(prefix, length);
79
80 return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
81 }
82
83 /**
84 * Returns object of this class with specified values.
85 *
86 * @param ipPrefix ip prefix
87 * @param length length of ip prefix
88 * @return object of this class
89 */
90 public static BgpFsSourcePrefix of(final IpPrefix ipPrefix, final byte length) {
91 return new BgpFsSourcePrefix(length, ipPrefix);
92 }
93
94 /**
Shashikanth VH60e73982016-02-10 11:25:34 +053095 * Returns ip prefix.
96 *
97 * @return ipPrefix ip prefix
98 */
99 public IpPrefix ipPrefix() {
100 return ipPrefix;
101 }
102
103 @Override
104 public short getType() {
105 return this.FLOW_SPEC_TYPE;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(ipPrefix);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118 if (obj instanceof BgpFsSourcePrefix) {
119 BgpFsSourcePrefix other = (BgpFsSourcePrefix) obj;
120 return Objects.equals(this.ipPrefix, other.ipPrefix) && Objects.equals(this.length, other.length);
121 }
122 return false;
123 }
124
125 @Override
126 public int write(ChannelBuffer cb) {
127 int iLenStartIndex = cb.writerIndex();
128 cb.writeByte(FLOW_SPEC_TYPE);
129 cb.writeByte(length);
130 cb.writeInt(ipPrefix.getIp4Prefix().address().toInt());
131 return cb.writerIndex() - iLenStartIndex;
132 }
133
Shashikanth VH60e73982016-02-10 11:25:34 +0530134 @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}