Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 19 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Objects; |
| 23 | import java.util.Set; |
| 24 | |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 27 | |
| 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 | */ |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 34 | public class NeighborSet { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 35 | private final Set<DeviceId> neighbors; |
| 36 | private final int edgeLabel; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 37 | public static final int NO_EDGE_LABEL = -1; |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 38 | private boolean mplsSet; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 39 | |
| 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 Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 45 | * @param isMplsSet indicates if it is a mpls neighbor set |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 46 | */ |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 47 | public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 48 | checkNotNull(neighbors); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 49 | this.edgeLabel = NO_EDGE_LABEL; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 50 | this.neighbors = new HashSet<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 51 | this.neighbors.addAll(neighbors); |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 52 | this.mplsSet = isMplsSet; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 53 | } |
| 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 Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 59 | * @param isMplsSet indicates if it is a mpls neighbor set |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 60 | * @param edgeLabel label to be pushed as part of group operation |
| 61 | */ |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 62 | public NeighborSet(Set<DeviceId> neighbors, boolean isMplsSet, int edgeLabel) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 63 | checkNotNull(neighbors); |
| 64 | this.edgeLabel = edgeLabel; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 65 | this.neighbors = new HashSet<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 66 | this.neighbors.addAll(neighbors); |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 67 | this.mplsSet = isMplsSet; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Default constructor for kryo serialization. |
| 72 | */ |
| 73 | public NeighborSet() { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 74 | this.edgeLabel = NO_EDGE_LABEL; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 75 | this.neighbors = new HashSet<>(); |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 76 | 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 116 | } |
| 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 Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 136 | /** |
| 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 156 | // 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 Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 168 | (this.edgeLabel == that.edgeLabel) && |
| 169 | (this.mplsSet == that.mplsSet)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // The list of neighbor ids and label are used for comparison. |
| 173 | @Override |
| 174 | public int hashCode() { |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 175 | return Objects.hash(neighbors, edgeLabel, mplsSet); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public String toString() { |
Pier Ventre | cd69375 | 2016-10-31 16:49:19 -0700 | [diff] [blame^] | 180 | return toStringHelper(this) |
| 181 | .add("Neighborset Sw", neighbors) |
| 182 | .add("Label", edgeLabel) |
| 183 | .add("MplsSet", mplsSet) |
| 184 | .toString(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 185 | } |
| 186 | } |