blob: a394839231acc342008c4279c3a4551665ed1c83 [file] [log] [blame]
Shashikanth VHfe2fb202016-02-05 18:45:07 +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 */
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) {
44 this.destinationPrefix = destinationPrefix;
45 this.sourcePrefix = sourcePrefix;
46 }
47
48 @Override
49 public int hashCode() {
50 return Objects.hash(destinationPrefix, sourcePrefix);
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (this == obj) {
56 return true;
57 }
58
59 if (obj instanceof BgpFlowSpecPrefix) {
60 BgpFlowSpecPrefix other = (BgpFlowSpecPrefix) obj;
61
Shashikanth VH03205d22016-02-19 22:48:08 +053062 if ((this.destinationPrefix != null) && (this.sourcePrefix != null)
63 && (this.destinationPrefix.equals(other.destinationPrefix))) {
64 return this.sourcePrefix.equals(other.sourcePrefix);
65 } else if (this.destinationPrefix != null) {
66 return this.destinationPrefix.equals(other.destinationPrefix);
67 } else if (this.sourcePrefix != null) {
Shashikanth VHfe2fb202016-02-05 18:45:07 +053068 return this.sourcePrefix.equals(other.sourcePrefix);
69 }
70 return false;
71 }
72 return false;
73 }
74
75 /**
76 * Returns destination prefix.
77 *
78 * @return destination prefix
79 */
80 public IpPrefix destinationPrefix() {
81 return this.destinationPrefix;
82 }
83
84 /**
85 * Returns source prefix.
86 *
87 * @return source prefix
88 */
89 public IpPrefix sourcePrefix() {
90 return this.sourcePrefix;
91 }
92
93 @Override
94 public String toString() {
95 return MoreObjects.toStringHelper(getClass()).omitNullValues()
96 .add("destinationPrefix", destinationPrefix)
97 .add("sourcePrefix", destinationPrefix)
98 .toString();
99 }
100
101 /**
102 * Compares this and o object.
103 *
104 * @param o object to be compared with this object
105 * @return which object is greater
106 */
107 public int compareTo(Object o) {
108 if (this.equals(o)) {
109 return 0;
110 }
111
112 if (o instanceof BgpFlowSpecPrefix) {
113 BgpFlowSpecPrefix that = (BgpFlowSpecPrefix) o;
Shashikanth VH03205d22016-02-19 22:48:08 +0530114 if (this.destinationPrefix() != null) {
115 if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) {
116 ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets());
117 ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets());
118 int cmpVal = value1.compareTo(value2);
119 if (cmpVal != 0) {
120 return cmpVal;
121 }
122 } else {
123 if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) {
124 return 1;
125 } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) {
126 return -1;
127 }
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530128 }
Shashikanth VH03205d22016-02-19 22:48:08 +0530129 }
130 if (this.sourcePrefix() != null) {
131 if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) {
132 ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets());
133 ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets());
134 return value1.compareTo(value2);
135 }
136
137 if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) {
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530138 return 1;
Shashikanth VH03205d22016-02-19 22:48:08 +0530139 } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) {
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530140 return -1;
141 }
142 }
Shashikanth VH03205d22016-02-19 22:48:08 +0530143 return 0;
Shashikanth VHfe2fb202016-02-05 18:45:07 +0530144 }
145 return 1;
146 }
147}