blob: 5b7568482486a65c987cb0a67172b0ecdbd102a4 [file] [log] [blame]
Shashikanth VH60e73982016-02-10 11:25:34 +05301/*
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 */
16package org.onosproject.bgpio.types;
17
18import java.nio.ByteBuffer;
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onlab.packet.IpPrefix;
23import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.util.Constants;
25
26import com.google.common.base.MoreObjects;
27import com.google.common.base.Preconditions;
28
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;
35 private byte length;
36 private IpPrefix ipPrefix;
37
38 /**
39 * Constructor to initialize parameters.
40 *
41 * @param length length of the prefix
42 * @param ipPrefix ip prefix
43 */
44 public BgpFsSourcePrefix(byte length, IpPrefix ipPrefix) {
45 this.ipPrefix = Preconditions.checkNotNull(ipPrefix);
46 this.length = length;
47 }
48
49 /**
50 * Returns ip prefix.
51 *
52 * @return ipPrefix ip prefix
53 */
54 public IpPrefix ipPrefix() {
55 return ipPrefix;
56 }
57
58 @Override
59 public short getType() {
60 return this.FLOW_SPEC_TYPE;
61 }
62
63 @Override
64 public int hashCode() {
65 return Objects.hash(ipPrefix);
66 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (this == obj) {
71 return true;
72 }
73 if (obj instanceof BgpFsSourcePrefix) {
74 BgpFsSourcePrefix other = (BgpFsSourcePrefix) obj;
75 return Objects.equals(this.ipPrefix, other.ipPrefix) && Objects.equals(this.length, other.length);
76 }
77 return false;
78 }
79
80 @Override
81 public int write(ChannelBuffer cb) {
82 int iLenStartIndex = cb.writerIndex();
83 cb.writeByte(FLOW_SPEC_TYPE);
84 cb.writeByte(length);
85 cb.writeInt(ipPrefix.getIp4Prefix().address().toInt());
86 return cb.writerIndex() - iLenStartIndex;
87 }
88
89 /**
90 * Reads the channel buffer and returns object of IPv4AddressTlv.
91 *
92 * @param cb channelBuffer
93 * @param type address type
94 * @return object of flow spec source prefix
95 * @throws BgpParseException while parsing BgpFsSourcePrefix
96 */
97 public static BgpFsSourcePrefix read(ChannelBuffer cb, short type) throws BgpParseException {
98 return null;
99 }
100
101 /**
102 * Returns object of this class with specified values.
103 *
104 * @param ipPrefix ip prefix
105 * @param length length of ip prefix
106 * @return object of this class
107 */
108 public static BgpFsSourcePrefix of(final IpPrefix ipPrefix, final byte length) {
109 return new BgpFsSourcePrefix(length, ipPrefix);
110 }
111
112 @Override
113 public int compareTo(Object o) {
114 if (this.equals(o)) {
115 return 0;
116 }
117
118 if (o instanceof BgpFsSourcePrefix) {
119 BgpFsSourcePrefix that = (BgpFsSourcePrefix) o;
120
121 if (this.ipPrefix().prefixLength() == that.ipPrefix().prefixLength()) {
122 ByteBuffer value1 = ByteBuffer.wrap(this.ipPrefix.address().toOctets());
123 ByteBuffer value2 = ByteBuffer.wrap(that.ipPrefix.address().toOctets());
124 return value1.compareTo(value2);
125 }
126
127 if (this.ipPrefix().prefixLength() > that.ipPrefix().prefixLength()) {
128 return 1;
129 } else if (this.ipPrefix().prefixLength() < that.ipPrefix().prefixLength()) {
130 return -1;
131 }
132 }
133 return 1;
134 }
135
136 @Override
137 public String toString() {
138 return MoreObjects.toStringHelper(getClass())
139 .add("FLOW_SPEC_TYPE", FLOW_SPEC_TYPE).add("length", length)
140 .add("ipPrefix", ipPrefix).toString();
141 }
142}