blob: aefcd21b73ad99e8a0e0eda861103ff90f58b1a5 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08003 *
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
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070017package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080018
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.HashSet;
22import java.util.Objects;
23import java.util.Set;
24
25import org.onosproject.net.DeviceId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080026
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 */
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070033public class NeighborSet {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080034 private final Set<DeviceId> neighbors;
35 private final int edgeLabel;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070036 public static final int NO_EDGE_LABEL = -1;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080037
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);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070046 this.edgeLabel = NO_EDGE_LABEL;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070047 this.neighbors = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080048 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;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070060 this.neighbors = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080061 this.neighbors.addAll(neighbors);
62 }
63
64 /**
65 * Default constructor for kryo serialization.
66 */
67 public NeighborSet() {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070068 this.edgeLabel = NO_EDGE_LABEL;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070069 this.neighbors = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080070 }
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}