2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.segmentrouting.grouphandler;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import java.util.HashSet;
22 import java.util.Objects;
25 import org.onosproject.net.DeviceId;
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.
33 public class NeighborSet {
34 private final Set<DeviceId> neighbors;
35 private final int edgeLabel;
36 public static final int NO_EDGE_LABEL = -1;
39 * Constructor with set of neighbors. Edge label is
42 * @param neighbors set of neighbors to be part of neighbor set
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);
52 * Constructor with set of neighbors and edge label.
54 * @param neighbors set of neighbors to be part of neighbor set
55 * @param edgeLabel label to be pushed as part of group operation
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);
65 * Default constructor for kryo serialization.
67 public NeighborSet() {
68 this.edgeLabel = NO_EDGE_LABEL;
69 this.neighbors = new HashSet<DeviceId>();
73 * Gets the neighbors part of neighbor set.
75 * @return set of neighbor identifiers
77 public Set<DeviceId> getDeviceIds() {
82 * Gets the label associated with neighbor set.
86 public int getEdgeLabel() {
90 // The list of neighbor ids and label are used for comparison.
92 public boolean equals(Object o) {
96 if (!(o instanceof NeighborSet)) {
99 NeighborSet that = (NeighborSet) o;
100 return (this.neighbors.containsAll(that.neighbors) &&
101 that.neighbors.containsAll(this.neighbors) &&
102 (this.edgeLabel == that.edgeLabel));
105 // The list of neighbor ids and label are used for comparison.
107 public int hashCode() {
109 int combinedHash = 0;
110 for (DeviceId d : neighbors) {
111 combinedHash = combinedHash + Objects.hash(d);
113 result = 31 * result + combinedHash + Objects.hash(edgeLabel);
119 public String toString() {
120 return " Neighborset Sw: " + neighbors
121 + " and Label: " + edgeLabel;