blob: 8373ccfcdf2227065138b1f2d27fc94512bb760f [file] [log] [blame]
Shashikanth VHfe2fb202016-02-05 18:45:07 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Shashikanth VHfe2fb202016-02-05 18:45:07 +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 */
16
17package org.onosproject.bgpio.protocol.flowspec;
18
19import java.util.Objects;
20import org.onlab.packet.IpPrefix;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23import java.nio.ByteBuffer;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides BGP flow specification rule index.
29 */
30public class BgpFlowSpecPrefix implements Comparable<Object> {
31
32 private static final Logger log = LoggerFactory.getLogger(BgpFlowSpecPrefix.class);
33
34 private final IpPrefix destinationPrefix;
35 private final IpPrefix sourcePrefix;
36
37 /**
38 * Constructor to initialize parameters.
39 *
40 * @param destinationPrefix destination prefix
41 * @param sourcePrefix source prefix
42 */
43 public BgpFlowSpecPrefix(IpPrefix destinationPrefix, IpPrefix sourcePrefix) {
Shashikanth VHe3a73bc2016-02-22 20:11:31 +053044 if (destinationPrefix == null) {
45 destinationPrefix = IpPrefix.valueOf(0, 0);
46 }
47
48 if (sourcePrefix == null) {
49 sourcePrefix = IpPrefix.valueOf(0, 0);
50 }
51
Shashikanth VHfe2fb202016-02-05 18:45:07 +053052 this.destinationPrefix = destinationPrefix;
53 this.sourcePrefix = sourcePrefix;
54 }
55
56 @Override
57 public int hashCode() {
58 return Objects.hash(destinationPrefix, sourcePrefix);
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66
67 if (obj instanceof BgpFlowSpecPrefix) {
68 BgpFlowSpecPrefix other = (BgpFlowSpecPrefix) obj;
69
Shashikanth VH03205d22016-02-19 22:48:08 +053070 if ((this.destinationPrefix != null) && (this.sourcePrefix != null)
71 && (this.destinationPrefix.equals(other.destinationPrefix))) {
72 return this.sourcePrefix.equals(other.sourcePrefix);
73 } else if (this.destinationPrefix != null) {
74 return this.destinationPrefix.equals(other.destinationPrefix);
75 } else if (this.sourcePrefix != null) {
Shashikanth VHfe2fb202016-02-05 18:45:07 +053076 return this.sourcePrefix.equals(other.sourcePrefix);
77 }
78 return false;
79 }
80 return false;
81 }
82
83 /**
84 * Returns destination prefix.
85 *
86 * @return destination prefix
87 */
88 public IpPrefix destinationPrefix() {
89 return this.destinationPrefix;
90 }
91
92 /**
93 * Returns source prefix.
94 *
95 * @return source prefix
96 */
97 public IpPrefix sourcePrefix() {
98 return this.sourcePrefix;
99 }
100
101 @Override
102 public String toString() {
103 return MoreObjects.toStringHelper(getClass()).omitNullValues()
104 .add("destinationPrefix", destinationPrefix)
105 .add("sourcePrefix", destinationPrefix)
106 .toString();
107 }
108
109 /**
110 * Compares this and o object.
111 *
112 * @param o object to be compared with this object
113 * @return which object is greater
114 */
115 public int compareTo(Object o) {
116 if (this.equals(o)) {
117 return 0;
118 }
119
120 if (o instanceof BgpFlowSpecPrefix) {
121 BgpFlowSpecPrefix that = (BgpFlowSpecPrefix) o;
Shashikanth VH03205d22016-02-19 22:48:08 +0530122 if (this.destinationPrefix() != null) {
123 if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) {
124 ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets());
125 ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets());
126 int cmpVal = value1.compareTo(value2);
127 if (cmpVal != 0) {
128 return cmpVal;
129 }
130 } else {
131 if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) {
132 return 1;
133 } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) {
134 return -1;
135 }
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530136 }
Shashikanth VH03205d22016-02-19 22:48:08 +0530137 }
138 if (this.sourcePrefix() != null) {
139 if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) {
140 ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets());
141 ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets());
142 return value1.compareTo(value2);
143 }
144
145 if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) {
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530146 return 1;
Shashikanth VH03205d22016-02-19 22:48:08 +0530147 } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) {
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530148 return -1;
149 }
150 }
Shashikanth VH03205d22016-02-19 22:48:08 +0530151 return 0;
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530152 }
153 return 1;
154 }
155}