Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.grouphandler; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 21 | import java.net.URI; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 22 | import java.util.ArrayList; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 23 | import java.util.Collections; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 24 | import java.util.HashMap; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.List; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 27 | import java.util.Random; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 28 | import java.util.Set; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 29 | import java.util.stream.Collectors; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 30 | |
| 31 | import org.onlab.packet.MacAddress; |
sangho | 32a5932 | 2015-02-17 12:07:41 -0800 | [diff] [blame] | 32 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 33 | import org.onlab.util.KryoNamespace; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Link; |
| 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 42 | import org.onosproject.net.flowobjective.NextObjective; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 43 | import org.onosproject.net.group.DefaultGroupKey; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 44 | import org.onosproject.net.group.GroupKey; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 45 | import org.onosproject.net.link.LinkService; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 46 | import org.onosproject.store.service.EventuallyConsistentMap; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 50 | * Default ECMP group handler creation module. This component creates a set of |
| 51 | * ECMP groups for every neighbor that this device is connected to based on |
| 52 | * whether the current device is an edge device or a transit device. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 53 | */ |
| 54 | public class DefaultGroupHandler { |
| 55 | protected final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | protected final DeviceId deviceId; |
| 58 | protected final ApplicationId appId; |
| 59 | protected final DeviceProperties deviceConfig; |
| 60 | protected final List<Integer> allSegmentIds; |
| 61 | protected final int nodeSegmentId; |
| 62 | protected final boolean isEdgeRouter; |
| 63 | protected final MacAddress nodeMacAddr; |
| 64 | protected LinkService linkService; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 65 | protected FlowObjectiveService flowObjectiveService; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 66 | |
| 67 | protected HashMap<DeviceId, Set<PortNumber>> devicePortMap = |
| 68 | new HashMap<DeviceId, Set<PortNumber>>(); |
| 69 | protected HashMap<PortNumber, DeviceId> portDeviceMap = |
| 70 | new HashMap<PortNumber, DeviceId>(); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 71 | //protected HashMap<NeighborSet, Integer> deviceNextObjectiveIds = |
| 72 | // new HashMap<NeighborSet, Integer>(); |
| 73 | protected EventuallyConsistentMap< |
| 74 | NeighborSetNextObjectiveStoreKey, Integer> nsNextObjStore = null; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 75 | protected Random rand = new Random(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 76 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 77 | protected KryoNamespace.Builder kryo = new KryoNamespace.Builder() |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 78 | .register(URI.class).register(HashSet.class) |
| 79 | .register(DeviceId.class).register(PortNumber.class) |
| 80 | .register(NeighborSet.class).register(PolicyGroupIdentifier.class) |
| 81 | .register(PolicyGroupParams.class) |
| 82 | .register(GroupBucketIdentifier.class) |
| 83 | .register(GroupBucketIdentifier.BucketOutputType.class); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 84 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 85 | protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId, |
| 86 | DeviceProperties config, |
| 87 | LinkService linkService, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 88 | FlowObjectiveService flowObjService, |
| 89 | EventuallyConsistentMap< |
| 90 | NeighborSetNextObjectiveStoreKey, |
| 91 | Integer> nsNextObjStore) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 92 | this.deviceId = checkNotNull(deviceId); |
| 93 | this.appId = checkNotNull(appId); |
| 94 | this.deviceConfig = checkNotNull(config); |
| 95 | this.linkService = checkNotNull(linkService); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 96 | allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds()); |
| 97 | nodeSegmentId = config.getSegmentId(deviceId); |
| 98 | isEdgeRouter = config.isEdgeDevice(deviceId); |
| 99 | nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId)); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 100 | this.flowObjectiveService = flowObjService; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 101 | this.nsNextObjStore = nsNextObjStore; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 102 | |
| 103 | populateNeighborMaps(); |
| 104 | } |
| 105 | |
| 106 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 107 | * Creates a group handler object based on the type of device. If device is |
| 108 | * of edge type it returns edge group handler, else it returns transit group |
| 109 | * handler. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 110 | * |
| 111 | * @param deviceId device identifier |
| 112 | * @param appId application identifier |
| 113 | * @param config interface to retrieve the device properties |
| 114 | * @param linkService link service object |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 115 | * @param flowObjService flow objective service object |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 116 | * @return default group handler type |
| 117 | */ |
| 118 | public static DefaultGroupHandler createGroupHandler(DeviceId deviceId, |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 119 | ApplicationId appId, |
| 120 | DeviceProperties config, |
| 121 | LinkService linkService, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 122 | FlowObjectiveService flowObjService, |
| 123 | EventuallyConsistentMap< |
| 124 | NeighborSetNextObjectiveStoreKey, |
| 125 | Integer> nsNextObjStore) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 126 | if (config.isEdgeDevice(deviceId)) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 127 | return new DefaultEdgeGroupHandler(deviceId, appId, config, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 128 | linkService, |
| 129 | flowObjService, |
| 130 | nsNextObjStore); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 131 | } else { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 132 | return new DefaultTransitGroupHandler(deviceId, appId, config, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 133 | linkService, |
| 134 | flowObjService, |
| 135 | nsNextObjStore); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 140 | * Creates the auto created groups for this device based on the current |
| 141 | * snapshot of the topology. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 142 | */ |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 143 | // Empty implementations to be overridden by derived classes |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 144 | public void createGroups() { |
| 145 | } |
| 146 | |
| 147 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 148 | * Performs group creation or update procedures when a new link is |
| 149 | * discovered on this device. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 150 | * |
| 151 | * @param newLink new neighbor link |
| 152 | */ |
| 153 | public void linkUp(Link newLink) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 155 | if (newLink.type() != Link.Type.DIRECT) { |
| 156 | log.warn("linkUp: unknown link type"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (!newLink.src().deviceId().equals(deviceId)) { |
| 161 | log.warn("linkUp: deviceId{} doesn't match with link src{}", |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 162 | deviceId, newLink.src().deviceId()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 166 | log.debug("Device {} linkUp at local port {} to neighbor {}", deviceId, |
| 167 | newLink.src().port(), newLink.dst().deviceId()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 168 | addNeighborAtPort(newLink.dst().deviceId(), |
| 169 | newLink.src().port()); |
| 170 | /*if (devicePortMap.get(newLink.dst().deviceId()) == null) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 171 | // New Neighbor |
| 172 | newNeighbor(newLink); |
| 173 | } else { |
| 174 | // Old Neighbor |
| 175 | newPortToExistingNeighbor(newLink); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 176 | }*/ |
| 177 | Set<NeighborSet> nsSet = nsNextObjStore.keySet() |
| 178 | .stream() |
| 179 | .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId))) |
| 180 | .map((nsStoreEntry) -> (nsStoreEntry.neighborSet())) |
| 181 | .filter((ns) -> (ns.getDeviceIds() |
| 182 | .contains(newLink.dst().deviceId()))) |
| 183 | .collect(Collectors.toSet()); |
| 184 | log.trace("linkUp: nsNextObjStore contents for device {}:", |
| 185 | deviceId, |
| 186 | nsSet); |
| 187 | for (NeighborSet ns : nsSet) { |
| 188 | // Create the new bucket to be updated |
| 189 | TrafficTreatment.Builder tBuilder = |
| 190 | DefaultTrafficTreatment.builder(); |
| 191 | tBuilder.setOutput(newLink.src().port()) |
| 192 | .setEthDst(deviceConfig.getDeviceMac( |
| 193 | newLink.dst().deviceId())) |
| 194 | .setEthSrc(nodeMacAddr); |
| 195 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
| 196 | tBuilder.pushMpls() |
| 197 | .setMpls(MplsLabel. |
| 198 | mplsLabel(ns.getEdgeLabel())); |
| 199 | } |
| 200 | |
| 201 | Integer nextId = nsNextObjStore. |
| 202 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
| 203 | if (nextId != null) { |
| 204 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 205 | .builder().withId(nextId) |
| 206 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
| 207 | |
| 208 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 209 | |
| 210 | log.debug("linkUp in device {}: Adding Bucket " |
| 211 | + "with Port {} to next object id {}", |
| 212 | deviceId, |
| 213 | newLink.src().port(), |
| 214 | nextId); |
| 215 | NextObjective nextObjective = nextObjBuilder.add(); |
| 216 | flowObjectiveService.next(deviceId, nextObjective); |
| 217 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 222 | * Performs group recovery procedures when a port goes down on this device. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 223 | * |
| 224 | * @param port port number that has gone down |
| 225 | */ |
| 226 | public void portDown(PortNumber port) { |
| 227 | if (portDeviceMap.get(port) == null) { |
| 228 | log.warn("portDown: unknown port"); |
| 229 | return; |
| 230 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 231 | log.debug("Device {} portDown {} to neighbor {}", deviceId, port, |
| 232 | portDeviceMap.get(port)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 233 | /*Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(portDeviceMap |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 234 | .get(port), |
| 235 | devicePortMap |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 236 | .keySet());*/ |
| 237 | Set<NeighborSet> nsSet = nsNextObjStore.keySet() |
| 238 | .stream() |
| 239 | .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId))) |
| 240 | .map((nsStoreEntry) -> (nsStoreEntry.neighborSet())) |
| 241 | .filter((ns) -> (ns.getDeviceIds() |
| 242 | .contains(portDeviceMap.get(port)))) |
| 243 | .collect(Collectors.toSet()); |
| 244 | log.trace("portDown: nsNextObjStore contents for device {}:", |
| 245 | deviceId, |
| 246 | nsSet); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 247 | for (NeighborSet ns : nsSet) { |
| 248 | // Create the bucket to be removed |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 249 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 250 | .builder(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 251 | tBuilder.setOutput(port) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 252 | .setEthDst(deviceConfig.getDeviceMac(portDeviceMap |
| 253 | .get(port))).setEthSrc(nodeMacAddr); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 254 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 255 | tBuilder.pushMpls().setMpls(MplsLabel.mplsLabel(ns |
| 256 | .getEdgeLabel())); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 257 | } |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 258 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 259 | Integer nextId = nsNextObjStore. |
| 260 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 261 | if (nextId != null) { |
| 262 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 263 | .builder().withType(NextObjective.Type.SIMPLE).withId(nextId).fromApp(appId); |
| 264 | |
| 265 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 266 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 267 | log.debug("portDown in device {}: Removing Bucket " |
| 268 | + "with Port {} to next object id {}", |
| 269 | deviceId, |
| 270 | port, |
| 271 | nextId); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 272 | NextObjective nextObjective = nextObjBuilder.remove(); |
| 273 | |
| 274 | flowObjectiveService.next(deviceId, nextObjective); |
| 275 | } |
| 276 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | devicePortMap.get(portDeviceMap.get(port)).remove(port); |
| 280 | portDeviceMap.remove(port); |
| 281 | } |
| 282 | |
| 283 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 284 | * Returns the next objective associated with the neighborset. |
| 285 | * If there is no next objective for this neighborset, this API |
| 286 | * would create a next objective and return. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 287 | * |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 288 | * @param ns neighborset |
| 289 | * @return int if found or -1 |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 290 | */ |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 291 | public int getNextObjectiveId(NeighborSet ns) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 292 | Integer nextId = nsNextObjStore. |
| 293 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 294 | if (nextId == null) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 295 | log.trace("getNextObjectiveId in device{}: Next objective id " |
| 296 | + "not found for {} and creating", deviceId, ns); |
| 297 | log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}", |
| 298 | deviceId, |
| 299 | nsNextObjStore.entrySet() |
| 300 | .stream() |
| 301 | .filter((nsStoreEntry) -> |
| 302 | (nsStoreEntry.getKey().deviceId().equals(deviceId))) |
| 303 | .collect(Collectors.toList())); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 304 | createGroupsFromNeighborsets(Collections.singleton(ns)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 305 | nextId = nsNextObjStore. |
| 306 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 307 | if (nextId == null) { |
| 308 | log.warn("getNextObjectiveId: unable to create next objective"); |
| 309 | return -1; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 310 | } else { |
| 311 | log.debug("getNextObjectiveId in device{}: Next objective id {} " |
| 312 | + "created for {}", deviceId, nextId.intValue(), ns); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 313 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 314 | } else { |
| 315 | log.trace("getNextObjectiveId in device{}: Next objective id {} " |
| 316 | + "found for {}", deviceId, nextId.intValue(), ns); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 317 | } |
| 318 | return nextId.intValue(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 321 | // Empty implementation |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 322 | protected void newNeighbor(Link newLink) { |
| 323 | } |
| 324 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 325 | // Empty implementation |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 326 | protected void newPortToExistingNeighbor(Link newLink) { |
| 327 | } |
| 328 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 329 | // Empty implementation |
| 330 | protected Set<NeighborSet> |
| 331 | computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor, |
| 332 | Set<DeviceId> updatedNeighbors) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 333 | return null; |
| 334 | } |
| 335 | |
| 336 | private void populateNeighborMaps() { |
| 337 | Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 338 | for (Link link : outgoingLinks) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 339 | if (link.type() != Link.Type.DIRECT) { |
| 340 | continue; |
| 341 | } |
| 342 | addNeighborAtPort(link.dst().deviceId(), link.src().port()); |
| 343 | } |
| 344 | } |
| 345 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 346 | protected void addNeighborAtPort(DeviceId neighborId, |
| 347 | PortNumber portToNeighbor) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 348 | // Update DeviceToPort database |
| 349 | log.debug("Device {} addNeighborAtPort: neighbor {} at port {}", |
| 350 | deviceId, neighborId, portToNeighbor); |
| 351 | if (devicePortMap.get(neighborId) != null) { |
| 352 | devicePortMap.get(neighborId).add(portToNeighbor); |
| 353 | } else { |
| 354 | Set<PortNumber> ports = new HashSet<PortNumber>(); |
| 355 | ports.add(portToNeighbor); |
| 356 | devicePortMap.put(neighborId, ports); |
| 357 | } |
| 358 | |
| 359 | // Update portToDevice database |
| 360 | if (portDeviceMap.get(portToNeighbor) == null) { |
| 361 | portDeviceMap.put(portToNeighbor, neighborId); |
| 362 | } |
| 363 | } |
| 364 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 365 | protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 366 | List<DeviceId> list = new ArrayList<DeviceId>(neighbors); |
| 367 | Set<Set<DeviceId>> sets = new HashSet<Set<DeviceId>>(); |
| 368 | // get the number of elements in the neighbors |
| 369 | int elements = list.size(); |
| 370 | // the number of members of a power set is 2^n |
| 371 | // including the empty set |
| 372 | int powerElements = (1 << elements); |
| 373 | |
| 374 | // run a binary counter for the number of power elements |
| 375 | // NOTE: Exclude empty set |
| 376 | for (long i = 1; i < powerElements; i++) { |
| 377 | Set<DeviceId> neighborSubSet = new HashSet<DeviceId>(); |
| 378 | for (int j = 0; j < elements; j++) { |
| 379 | if ((i >> j) % 2 == 1) { |
| 380 | neighborSubSet.add(list.get(j)); |
| 381 | } |
| 382 | } |
| 383 | sets.add(neighborSubSet); |
| 384 | } |
| 385 | return sets; |
| 386 | } |
| 387 | |
| 388 | private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) { |
| 389 | return (deviceConfig.getSegmentId(deviceId) == sId); |
| 390 | } |
| 391 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 392 | protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 393 | |
| 394 | List<Integer> nsSegmentIds = new ArrayList<Integer>(); |
| 395 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 396 | // Always pair up with no edge label |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 397 | // If (neighbors.size() == 1) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 398 | nsSegmentIds.add(-1); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 399 | // } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 400 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 401 | // Filter out SegmentIds matching with the |
| 402 | // nodes in the combo |
| 403 | for (Integer sId : allSegmentIds) { |
| 404 | if (sId.equals(nodeSegmentId)) { |
| 405 | continue; |
| 406 | } |
| 407 | boolean filterOut = false; |
| 408 | // Check if the edge label being set is of |
| 409 | // any node in the Neighbor set |
| 410 | for (DeviceId deviceId : neighbors) { |
| 411 | if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) { |
| 412 | filterOut = true; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | if (!filterOut) { |
| 417 | nsSegmentIds.add(sId); |
| 418 | } |
| 419 | } |
| 420 | return nsSegmentIds; |
| 421 | } |
| 422 | |
| 423 | protected void createGroupsFromNeighborsets(Set<NeighborSet> nsSet) { |
| 424 | for (NeighborSet ns : nsSet) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 425 | int nextId = flowObjectiveService.allocateNextId(); |
| 426 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 427 | .builder().withId(nextId) |
| 428 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 429 | for (DeviceId d : ns.getDeviceIds()) { |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 430 | if (devicePortMap.get(d) == null) { |
| 431 | log.warn("Device {} is not in the port map yet", d); |
| 432 | return; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 433 | } else if (devicePortMap.get(d).size() == 0) { |
| 434 | log.warn("There are no ports for " |
| 435 | + "the Device {} in the port map yet", d); |
| 436 | return; |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 439 | for (PortNumber sp : devicePortMap.get(d)) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 440 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 441 | .builder(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 442 | tBuilder.setOutput(sp) |
| 443 | .setEthDst(deviceConfig.getDeviceMac(d)) |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 444 | .setEthSrc(nodeMacAddr); |
| 445 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 446 | tBuilder.pushMpls().setMpls(MplsLabel.mplsLabel(ns |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 447 | .getEdgeLabel())); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 448 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 449 | nextObjBuilder.addTreatment(tBuilder.build()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 452 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 453 | NextObjective nextObj = nextObjBuilder.add(); |
| 454 | flowObjectiveService.next(deviceId, nextObj); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame^] | 455 | log.debug("createGroupsFromNeighborsets: Submited " |
| 456 | + "next objective {} in device {}", |
| 457 | nextId, deviceId); |
| 458 | nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns), |
| 459 | nextId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 462 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 463 | public GroupKey getGroupKey(Object obj) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 464 | return new DefaultGroupKey(kryo.build().serialize(obj)); |
| 465 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 466 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 467 | } |