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