7ada3224c8ee73e027bb0083654a3bb64d6c80cd
[onosfw.git] /
1 /*
2  * Copyright 2015 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
17 package org.onosproject.segmentrouting.grouphandler;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import java.util.HashSet;
22 import java.util.Objects;
23 import java.util.Set;
24
25 import org.onosproject.net.DeviceId;
26
27 /**
28  * Representation of a set of neighbor switch dpids along with edge node
29  * label. Meant to be used as a lookup-key in a hash-map to retrieve an
30  * ECMP-group that hashes packets to a set of ports connecting to the
31  * neighbors in this set.
32  */
33 public class NeighborSet {
34     private final Set<DeviceId> neighbors;
35     private final int edgeLabel;
36     public static final int NO_EDGE_LABEL = -1;
37
38     /**
39      * Constructor with set of neighbors. Edge label is
40      * default to -1.
41      *
42      * @param neighbors set of neighbors to be part of neighbor set
43      */
44     public NeighborSet(Set<DeviceId> neighbors) {
45         checkNotNull(neighbors);
46         this.edgeLabel = NO_EDGE_LABEL;
47         this.neighbors = new HashSet<DeviceId>();
48         this.neighbors.addAll(neighbors);
49     }
50
51     /**
52      * Constructor with set of neighbors and edge label.
53      *
54      * @param neighbors set of neighbors to be part of neighbor set
55      * @param edgeLabel label to be pushed as part of group operation
56      */
57     public NeighborSet(Set<DeviceId> neighbors, int edgeLabel) {
58         checkNotNull(neighbors);
59         this.edgeLabel = edgeLabel;
60         this.neighbors = new HashSet<DeviceId>();
61         this.neighbors.addAll(neighbors);
62     }
63
64     /**
65      * Default constructor for kryo serialization.
66      */
67     public NeighborSet() {
68         this.edgeLabel = NO_EDGE_LABEL;
69         this.neighbors = new HashSet<DeviceId>();
70     }
71
72     /**
73      * Gets the neighbors part of neighbor set.
74      *
75      * @return set of neighbor identifiers
76      */
77     public Set<DeviceId> getDeviceIds() {
78         return neighbors;
79     }
80
81     /**
82      * Gets the label associated with neighbor set.
83      *
84      * @return integer
85      */
86     public int getEdgeLabel() {
87         return edgeLabel;
88     }
89
90     // The list of neighbor ids and label are used for comparison.
91     @Override
92     public boolean equals(Object o) {
93         if (this == o) {
94             return true;
95         }
96         if (!(o instanceof NeighborSet)) {
97             return false;
98         }
99         NeighborSet that = (NeighborSet) o;
100         return (this.neighbors.containsAll(that.neighbors) &&
101                 that.neighbors.containsAll(this.neighbors) &&
102                 (this.edgeLabel == that.edgeLabel));
103     }
104
105     // The list of neighbor ids and label are used for comparison.
106     @Override
107     public int hashCode() {
108         int result = 17;
109         int combinedHash = 0;
110         for (DeviceId d : neighbors) {
111             combinedHash = combinedHash + Objects.hash(d);
112         }
113         result = 31 * result + combinedHash + Objects.hash(edgeLabel);
114
115         return result;
116     }
117
118     @Override
119     public String toString() {
120         return " Neighborset Sw: " + neighbors
121                 + " and Label: " + edgeLabel;
122     }
123 }