blob: 88799f46e25d20afedce317b5ee79e0c3b4f1771 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Pier Ventre917127a2016-10-31 16:49:19 -070019import org.onosproject.net.DeviceId;
Saurav Dasc88d4662017-05-15 15:34:25 -070020import org.slf4j.Logger;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080021
22import java.util.HashSet;
23import java.util.Objects;
24import java.util.Set;
25
Pier Ventre917127a2016-10-31 16:49:19 -070026import static com.google.common.base.MoreObjects.toStringHelper;
27import static com.google.common.base.Preconditions.checkNotNull;
Saurav Dasc88d4662017-05-15 15:34:25 -070028import static org.slf4j.LoggerFactory.getLogger;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080029
30/**
31 * Representation of a set of neighbor switch dpids along with edge node
Saurav Dasc88d4662017-05-15 15:34:25 -070032 * label and a destination switch. Meant to be used as a lookup-key in a hash-map
33 * to retrieve an ECMP-group that hashes packets to a set of ports connecting to
34 * the neighbors in this set towards a specific destination switch.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080035 */
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070036public class NeighborSet {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080037 private final Set<DeviceId> neighbors;
38 private final int edgeLabel;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070039 public static final int NO_EDGE_LABEL = -1;
Pier Ventre917127a2016-10-31 16:49:19 -070040 private boolean mplsSet;
Saurav Dasc88d4662017-05-15 15:34:25 -070041 // the destination switch towards which the neighbors are the next-hops.
42 private final DeviceId dstSw;
43 protected static final Logger log = getLogger(NeighborSet.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080044
45 /**
46 * Constructor with set of neighbors. Edge label is
47 * default to -1.
48 *
Saurav Dasc88d4662017-05-15 15:34:25 -070049 * @param neighbors set of neighbors representing the next-hops
Pier Ventre917127a2016-10-31 16:49:19 -070050 * @param isMplsSet indicates if it is a mpls neighbor set
Saurav Dasc88d4662017-05-15 15:34:25 -070051 * @param dstSw the destination switch
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080052 */
Saurav Dasc88d4662017-05-15 15:34:25 -070053 public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet, DeviceId dstSw) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080054 checkNotNull(neighbors);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070055 this.edgeLabel = NO_EDGE_LABEL;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070056 this.neighbors = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080057 this.neighbors.addAll(neighbors);
Pier Ventre917127a2016-10-31 16:49:19 -070058 this.mplsSet = isMplsSet;
Saurav Dasc88d4662017-05-15 15:34:25 -070059 this.dstSw = dstSw;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080060 }
61
62 /**
63 * Constructor with set of neighbors and edge label.
64 *
Saurav Dasc88d4662017-05-15 15:34:25 -070065 * @param neighbors set of neighbors representing the next-hops
Pier Ventre917127a2016-10-31 16:49:19 -070066 * @param isMplsSet indicates if it is a mpls neighbor set
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080067 * @param edgeLabel label to be pushed as part of group operation
Saurav Dasc88d4662017-05-15 15:34:25 -070068 * @param dstSw the destination switch
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080069 */
Saurav Dasc88d4662017-05-15 15:34:25 -070070 public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet,
71 int edgeLabel, DeviceId dstSw) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080072 checkNotNull(neighbors);
73 this.edgeLabel = edgeLabel;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070074 this.neighbors = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080075 this.neighbors.addAll(neighbors);
Pier Ventre917127a2016-10-31 16:49:19 -070076 this.mplsSet = isMplsSet;
Saurav Dasc88d4662017-05-15 15:34:25 -070077 this.dstSw = dstSw;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080078 }
79
80 /**
81 * Default constructor for kryo serialization.
82 */
83 public NeighborSet() {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070084 this.edgeLabel = NO_EDGE_LABEL;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070085 this.neighbors = new HashSet<>();
Pier Ventre917127a2016-10-31 16:49:19 -070086 this.mplsSet = true;
Saurav Dasc88d4662017-05-15 15:34:25 -070087 this.dstSw = DeviceId.NONE;
Pier Ventre917127a2016-10-31 16:49:19 -070088 }
89
90 /**
91 * Factory method for NeighborSet hierarchy.
92 *
93 * @param random the expected behavior.
Saurav Dasc88d4662017-05-15 15:34:25 -070094 * @param neighbors the set of neighbors representing the next-hops
Pier Ventre917127a2016-10-31 16:49:19 -070095 * @param isMplsSet indicates if it is a mpls neighbor set
Saurav Dasc88d4662017-05-15 15:34:25 -070096 * @param dstSw the destination switch
Pier Ventre917127a2016-10-31 16:49:19 -070097 * @return the neighbor set object.
98 */
Saurav Dasc88d4662017-05-15 15:34:25 -070099 public static NeighborSet neighborSet(boolean random, Set<DeviceId> neighbors,
100 boolean isMplsSet, DeviceId dstSw) {
101 return random ? new RandomNeighborSet(neighbors, dstSw)
102 : new NeighborSet(neighbors, isMplsSet, dstSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700103 }
104
105 /**
106 * Factory method for NeighborSet hierarchy.
107 *
108 * @param random the expected behavior.
Saurav Dasc88d4662017-05-15 15:34:25 -0700109 * @param neighbors the set of neighbors representing the next-hops
Pier Ventre917127a2016-10-31 16:49:19 -0700110 * @param isMplsSet indicates if it is a mpls neighbor set
111 * @param edgeLabel label to be pushed as part of group operation
Saurav Dasc88d4662017-05-15 15:34:25 -0700112 * @param dstSw the destination switch
Pier Ventre917127a2016-10-31 16:49:19 -0700113 * @return the neighbor set object
114 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700115 public static NeighborSet neighborSet(boolean random, Set<DeviceId> neighbors,
116 boolean isMplsSet, int edgeLabel,
117 DeviceId dstSw) {
118 return random ? new RandomNeighborSet(neighbors, edgeLabel, dstSw)
119 : new NeighborSet(neighbors, isMplsSet, edgeLabel, dstSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700120 }
121
122 /**
123 * Factory method for NeighborSet hierarchy.
124 *
125 * @param random the expected behavior.
126 * @return the neighbor set object
127 */
128 public static NeighborSet neighborSet(boolean random) {
129 return random ? new RandomNeighborSet() : new NeighborSet();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800130 }
131
132 /**
133 * Gets the neighbors part of neighbor set.
134 *
135 * @return set of neighbor identifiers
136 */
137 public Set<DeviceId> getDeviceIds() {
138 return neighbors;
139 }
140
141 /**
142 * Gets the label associated with neighbor set.
143 *
144 * @return integer
145 */
146 public int getEdgeLabel() {
147 return edgeLabel;
148 }
149
Pier Ventre917127a2016-10-31 16:49:19 -0700150 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700151 * Gets the destination switch for this neighbor set.
152 *
153 * @return the destination switch id
154 */
155 public DeviceId getDestinationSw() {
156 return dstSw;
157 }
158
159 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700160 * Gets the first neighbor of the set. The default
161 * implementation assure the first neighbor is the
162 * first of the set. Subclasses can modify this.
163 *
164 * @return the first neighbor of the set
165 */
166 public DeviceId getFirstNeighbor() {
167 return neighbors.isEmpty() ? DeviceId.NONE : neighbors.iterator().next();
168 }
169
170 /**
171 * Gets the value of mplsSet.
172 *
173 * @return the value of mplsSet
174 */
175 public boolean mplsSet() {
176 return mplsSet;
177 }
178
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800179 // The list of neighbor ids and label are used for comparison.
180 @Override
181 public boolean equals(Object o) {
182 if (this == o) {
183 return true;
184 }
185 if (!(o instanceof NeighborSet)) {
186 return false;
187 }
188 NeighborSet that = (NeighborSet) o;
189 return (this.neighbors.containsAll(that.neighbors) &&
190 that.neighbors.containsAll(this.neighbors) &&
Saurav Dasc88d4662017-05-15 15:34:25 -0700191 this.edgeLabel == that.edgeLabel &&
192 this.mplsSet == that.mplsSet &&
193 this.dstSw.equals(that.dstSw));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800194 }
195
196 // The list of neighbor ids and label are used for comparison.
197 @Override
198 public int hashCode() {
Saurav Dasc88d4662017-05-15 15:34:25 -0700199 return Objects.hash(neighbors, edgeLabel, mplsSet, dstSw);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800200 }
201
202 @Override
203 public String toString() {
Pier Ventre917127a2016-10-31 16:49:19 -0700204 return toStringHelper(this)
Saurav Dasc88d4662017-05-15 15:34:25 -0700205 .add("Neighbors", neighbors)
Pier Ventre917127a2016-10-31 16:49:19 -0700206 .add("Label", edgeLabel)
207 .add("MplsSet", mplsSet)
Saurav Dasc88d4662017-05-15 15:34:25 -0700208 .add("DstSw", dstSw)
Pier Ventre917127a2016-10-31 16:49:19 -0700209 .toString();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800210 }
211}