Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 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 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 17 | package org.onosproject.segmentrouting.grouphandler; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 18 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 20 | import org.slf4j.Logger; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 21 | |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Objects; |
| 24 | import java.util.Set; |
| 25 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 28 | import static org.slf4j.LoggerFactory.getLogger; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Representation of a set of neighbor switch dpids along with edge node |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 32 | * 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 35 | */ |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 36 | public class NeighborSet { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 37 | private final Set<DeviceId> neighbors; |
| 38 | private final int edgeLabel; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 39 | public static final int NO_EDGE_LABEL = -1; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 40 | private boolean mplsSet; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 41 | // 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Constructor with set of neighbors. Edge label is |
| 47 | * default to -1. |
| 48 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 49 | * @param neighbors set of neighbors representing the next-hops |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 50 | * @param isMplsSet indicates if it is a mpls neighbor set |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 51 | * @param dstSw the destination switch |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 52 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 53 | public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet, DeviceId dstSw) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 54 | checkNotNull(neighbors); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 55 | this.edgeLabel = NO_EDGE_LABEL; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 56 | this.neighbors = new HashSet<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 57 | this.neighbors.addAll(neighbors); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 58 | this.mplsSet = isMplsSet; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 59 | this.dstSw = dstSw; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructor with set of neighbors and edge label. |
| 64 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 65 | * @param neighbors set of neighbors representing the next-hops |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 66 | * @param isMplsSet indicates if it is a mpls neighbor set |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 67 | * @param edgeLabel label to be pushed as part of group operation |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 68 | * @param dstSw the destination switch |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 69 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 70 | public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet, |
| 71 | int edgeLabel, DeviceId dstSw) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 72 | checkNotNull(neighbors); |
| 73 | this.edgeLabel = edgeLabel; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 74 | this.neighbors = new HashSet<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 75 | this.neighbors.addAll(neighbors); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 76 | this.mplsSet = isMplsSet; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 77 | this.dstSw = dstSw; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Default constructor for kryo serialization. |
| 82 | */ |
| 83 | public NeighborSet() { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 84 | this.edgeLabel = NO_EDGE_LABEL; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 85 | this.neighbors = new HashSet<>(); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 86 | this.mplsSet = true; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 87 | this.dstSw = DeviceId.NONE; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Factory method for NeighborSet hierarchy. |
| 92 | * |
| 93 | * @param random the expected behavior. |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 94 | * @param neighbors the set of neighbors representing the next-hops |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 95 | * @param isMplsSet indicates if it is a mpls neighbor set |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 96 | * @param dstSw the destination switch |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 97 | * @return the neighbor set object. |
| 98 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 99 | 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 Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Factory method for NeighborSet hierarchy. |
| 107 | * |
| 108 | * @param random the expected behavior. |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 109 | * @param neighbors the set of neighbors representing the next-hops |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 110 | * @param isMplsSet indicates if it is a mpls neighbor set |
| 111 | * @param edgeLabel label to be pushed as part of group operation |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 112 | * @param dstSw the destination switch |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 113 | * @return the neighbor set object |
| 114 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 115 | 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 Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 120 | } |
| 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 130 | } |
| 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 Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 150 | /** |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 151 | * 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 Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 160 | * 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 179 | // 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 191 | this.edgeLabel == that.edgeLabel && |
| 192 | this.mplsSet == that.mplsSet && |
| 193 | this.dstSw.equals(that.dstSw)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // The list of neighbor ids and label are used for comparison. |
| 197 | @Override |
| 198 | public int hashCode() { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 199 | return Objects.hash(neighbors, edgeLabel, mplsSet, dstSw); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public String toString() { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 204 | return toStringHelper(this) |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 205 | .add("Neighbors", neighbors) |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 206 | .add("Label", edgeLabel) |
| 207 | .add("MplsSet", mplsSet) |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 208 | .add("DstSw", dstSw) |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 209 | .toString(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 210 | } |
| 211 | } |