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