blob: 06f7861be780522f4e838fd7b3a6dbf03a020f2c [file] [log] [blame]
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07003 *
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.segmentrouting.grouphandler;
18
Pier Ventre229fd0b2016-10-31 16:49:19 -070019import org.onosproject.net.DeviceId;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070020
21import java.util.HashSet;
22import java.util.Objects;
23import java.util.Set;
24
Pier Ventre229fd0b2016-10-31 16:49:19 -070025import static com.google.common.base.MoreObjects.toStringHelper;
26import static com.google.common.base.Preconditions.checkNotNull;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070027
28/**
29 * Representation of a set of neighbor switch dpids along with edge node
30 * label. Meant to be used as a lookup-key in a hash-map to retrieve an
31 * ECMP-group that hashes packets to a set of ports connecting to the
32 * neighbors in this set.
33 */
34public class NeighborSet {
35 private final Set<DeviceId> neighbors;
36 private final int edgeLabel;
37 public static final int NO_EDGE_LABEL = -1;
Pier Ventre229fd0b2016-10-31 16:49:19 -070038 private boolean mplsSet;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070039
40 /**
41 * Constructor with set of neighbors. Edge label is
42 * default to -1.
43 *
44 * @param neighbors set of neighbors to be part of neighbor set
Pier Ventre229fd0b2016-10-31 16:49:19 -070045 * @param isMplsSet indicates if it is a mpls neighbor set
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070046 */
Pier Ventre229fd0b2016-10-31 16:49:19 -070047 public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070048 checkNotNull(neighbors);
49 this.edgeLabel = NO_EDGE_LABEL;
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -070050 this.neighbors = new HashSet<>();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070051 this.neighbors.addAll(neighbors);
Pier Ventre229fd0b2016-10-31 16:49:19 -070052 this.mplsSet = isMplsSet;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070053 }
54
55 /**
56 * Constructor with set of neighbors and edge label.
57 *
58 * @param neighbors set of neighbors to be part of neighbor set
Pier Ventre229fd0b2016-10-31 16:49:19 -070059 * @param isMplsSet indicates if it is a mpls neighbor set
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070060 * @param edgeLabel label to be pushed as part of group operation
61 */
Pier Ventre229fd0b2016-10-31 16:49:19 -070062 public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet, int edgeLabel) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070063 checkNotNull(neighbors);
64 this.edgeLabel = edgeLabel;
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -070065 this.neighbors = new HashSet<>();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070066 this.neighbors.addAll(neighbors);
Pier Ventre229fd0b2016-10-31 16:49:19 -070067 this.mplsSet = isMplsSet;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070068 }
69
70 /**
71 * Default constructor for kryo serialization.
72 */
73 public NeighborSet() {
74 this.edgeLabel = NO_EDGE_LABEL;
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -070075 this.neighbors = new HashSet<>();
Pier Ventre229fd0b2016-10-31 16:49:19 -070076 this.mplsSet = true;
77 }
78
79 /**
80 * Factory method for NeighborSet hierarchy.
81 *
82 * @param random the expected behavior.
83 * @param neighbors the set of neighbors to be part of neighbor set
84 * @param isMplsSet indicates if it is a mpls neighbor set
85 * @return the neighbor set object.
86 */
87 public static NeighborSet neighborSet(boolean random, Set<DeviceId> neighbors, boolean isMplsSet) {
88 return random ?
89 new RandomNeighborSet(neighbors) :
90 new NeighborSet(neighbors, isMplsSet);
91 }
92
93 /**
94 * Factory method for NeighborSet hierarchy.
95 *
96 * @param random the expected behavior.
97 * @param neighbors the set of neighbors to be part of neighbor set
98 * @param isMplsSet indicates if it is a mpls neighbor set
99 * @param edgeLabel label to be pushed as part of group operation
100 * @return the neighbor set object
101 */
102 public static NeighborSet neighborSet(boolean random, Set<DeviceId> neighbors, boolean isMplsSet, int edgeLabel) {
103 return random ?
104 new RandomNeighborSet(neighbors, edgeLabel) :
105 new NeighborSet(neighbors, isMplsSet, edgeLabel);
106 }
107
108 /**
109 * Factory method for NeighborSet hierarchy.
110 *
111 * @param random the expected behavior.
112 * @return the neighbor set object
113 */
114 public static NeighborSet neighborSet(boolean random) {
115 return random ? new RandomNeighborSet() : new NeighborSet();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700116 }
117
118 /**
119 * Gets the neighbors part of neighbor set.
120 *
121 * @return set of neighbor identifiers
122 */
123 public Set<DeviceId> getDeviceIds() {
124 return neighbors;
125 }
126
127 /**
128 * Gets the label associated with neighbor set.
129 *
130 * @return integer
131 */
132 public int getEdgeLabel() {
133 return edgeLabel;
134 }
135
Pier Ventre229fd0b2016-10-31 16:49:19 -0700136 /**
137 * Gets the first neighbor of the set. The default
138 * implementation assure the first neighbor is the
139 * first of the set. Subclasses can modify this.
140 *
141 * @return the first neighbor of the set
142 */
143 public DeviceId getFirstNeighbor() {
144 return neighbors.isEmpty() ? DeviceId.NONE : neighbors.iterator().next();
145 }
146
147 /**
148 * Gets the value of mplsSet.
149 *
150 * @return the value of mplsSet
151 */
152 public boolean mplsSet() {
153 return mplsSet;
154 }
155
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700156 // The list of neighbor ids and label are used for comparison.
157 @Override
158 public boolean equals(Object o) {
159 if (this == o) {
160 return true;
161 }
162 if (!(o instanceof NeighborSet)) {
163 return false;
164 }
165 NeighborSet that = (NeighborSet) o;
166 return (this.neighbors.containsAll(that.neighbors) &&
167 that.neighbors.containsAll(this.neighbors) &&
Pier Ventre229fd0b2016-10-31 16:49:19 -0700168 (this.edgeLabel == that.edgeLabel) &&
169 (this.mplsSet == that.mplsSet));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700170 }
171
172 // The list of neighbor ids and label are used for comparison.
173 @Override
174 public int hashCode() {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700175 return Objects.hash(neighbors, edgeLabel, mplsSet);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700176 }
177
178 @Override
179 public String toString() {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700180 return toStringHelper(this)
181 .add("Neighborset Sw", neighbors)
182 .add("Label", edgeLabel)
183 .add("MplsSet", mplsSet)
184 .toString();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700185 }
186}