blob: cbf4269ab8400f72551bdf2ac2bc5fe60959c373 [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
62 if (this.destinationPrefix.equals(other.destinationPrefix)) {
63 return this.sourcePrefix.equals(other.sourcePrefix);
64 }
65 return false;
66 }
67 return false;
68 }
69
70 /**
71 * Returns destination prefix.
72 *
73 * @return destination prefix
74 */
75 public IpPrefix destinationPrefix() {
76 return this.destinationPrefix;
77 }
78
79 /**
80 * Returns source prefix.
81 *
82 * @return source prefix
83 */
84 public IpPrefix sourcePrefix() {
85 return this.sourcePrefix;
86 }
87
88 @Override
89 public String toString() {
90 return MoreObjects.toStringHelper(getClass()).omitNullValues()
91 .add("destinationPrefix", destinationPrefix)
92 .add("sourcePrefix", destinationPrefix)
93 .toString();
94 }
95
96 /**
97 * Compares this and o object.
98 *
99 * @param o object to be compared with this object
100 * @return which object is greater
101 */
102 public int compareTo(Object o) {
103 if (this.equals(o)) {
104 return 0;
105 }
106
107 if (o instanceof BgpFlowSpecPrefix) {
108 BgpFlowSpecPrefix that = (BgpFlowSpecPrefix) o;
109
110 if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) {
111 ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets());
112 ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets());
113 int cmpVal = value1.compareTo(value2);
114 if (cmpVal != 0) {
115 return cmpVal;
116 }
117 } else {
118 if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) {
119 return 1;
120 } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) {
121 return -1;
122 }
123 }
124
125 if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) {
126 ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets());
127 ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets());
128 return value1.compareTo(value2);
129 }
130
131 if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) {
132 return 1;
133 } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) {
134 return -1;
135 }
136 }
137 return 1;
138 }
139}