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