blob: 9e7fd04acb1b5aee21db4472044500969d735c41 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08003 *
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 Vavilapalli4db76e32015-04-07 15:12:32 -070016package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080017
Charles Chan59cc16d2017-02-02 16:20:42 -080018
Pier Ventre917127a2016-10-31 16:49:19 -070019import com.google.common.collect.Iterables;
20import org.apache.commons.lang3.RandomUtils;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080021import org.onlab.packet.MacAddress;
sangho32a59322015-02-17 12:07:41 -080022import org.onlab.packet.MplsLabel;
Saurav Das423fe2b2015-12-04 10:52:59 -080023import org.onlab.packet.VlanId;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070024import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080025import org.onosproject.core.ApplicationId;
Charles Chan59cc16d2017-02-02 16:20:42 -080026import org.onosproject.incubator.net.intf.Interface;
27import org.onosproject.net.ConnectPoint;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080028import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
30import org.onosproject.net.PortNumber;
Saurav Das423fe2b2015-12-04 10:52:59 -080031import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080032import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das8a0732e2015-11-20 15:27:53 -080033import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080034import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070035import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan216e3c82016-04-23 14:48:16 -070036import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070037import org.onosproject.net.flowobjective.FlowObjectiveService;
38import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070039import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080040import org.onosproject.net.link.LinkService;
Saurav Das423fe2b2015-12-04 10:52:59 -080041import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan0b4e6182015-11-03 10:42:14 -080042import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
43import org.onosproject.segmentrouting.config.DeviceProperties;
Charles Chand2990362016-04-18 13:44:03 -070044import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
45import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080046import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070047import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080048import org.slf4j.Logger;
49
Pier Ventre917127a2016-10-31 16:49:19 -070050import java.net.URI;
51import java.util.ArrayList;
Charles Chan7ffd81f2017-02-08 15:52:08 -080052import java.util.Collection;
Pier Ventre917127a2016-10-31 16:49:19 -070053import java.util.Collections;
54import java.util.HashSet;
55import java.util.List;
56import java.util.Map;
57import java.util.Set;
58import java.util.concurrent.ConcurrentHashMap;
59import java.util.stream.Collectors;
60
61import static com.google.common.base.Preconditions.checkNotNull;
Charles Chan59cc16d2017-02-02 16:20:42 -080062import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
Pier Ventre917127a2016-10-31 16:49:19 -070063import static org.slf4j.LoggerFactory.getLogger;
64
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080065/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070066 * Default ECMP group handler creation module. This component creates a set of
67 * ECMP groups for every neighbor that this device is connected to based on
68 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080069 */
70public class DefaultGroupHandler {
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070071 protected static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080072
73 protected final DeviceId deviceId;
74 protected final ApplicationId appId;
75 protected final DeviceProperties deviceConfig;
76 protected final List<Integer> allSegmentIds;
Pier Ventree0ae7a32016-11-23 09:57:42 -080077 protected int ipv4NodeSegmentId = -1;
78 protected int ipv6NodeSegmentId = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -080079 protected boolean isEdgeRouter = false;
80 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080081 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070082 protected FlowObjectiveService flowObjectiveService;
Saurav Das423fe2b2015-12-04 10:52:59 -080083 // local store for neighbor-device-ids and the set of ports on this device
84 // that connect to the same neighbor
Saurav Das8a0732e2015-11-20 15:27:53 -080085 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
86 new ConcurrentHashMap<>();
Saurav Das1a129a02016-11-18 15:21:57 -080087 // local store for ports on this device connected to neighbor-device-id
Saurav Das8a0732e2015-11-20 15:27:53 -080088 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
89 new ConcurrentHashMap<>();
Saurav Das1a129a02016-11-18 15:21:57 -080090 // distributed store for (device+neighborset) mapped to next-id
Charles Chane849c192016-01-11 18:28:54 -080091 protected EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
92 nsNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -080093 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan59cc16d2017-02-02 16:20:42 -080094 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
95 vlanNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -080096 // distributed store for (device+port+treatment) mapped to next-id
Charles Chane849c192016-01-11 18:28:54 -080097 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
98 portNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -080099 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800100
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700101 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700102 .register(URI.class).register(HashSet.class)
103 .register(DeviceId.class).register(PortNumber.class)
104 .register(NeighborSet.class).register(PolicyGroupIdentifier.class)
105 .register(PolicyGroupParams.class)
106 .register(GroupBucketIdentifier.class)
107 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800108
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700109 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
110 DeviceProperties config,
111 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700112 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800113 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800114 this.deviceId = checkNotNull(deviceId);
115 this.appId = checkNotNull(appId);
116 this.deviceConfig = checkNotNull(config);
117 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800118 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
119 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800120 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
121 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800122 this.isEdgeRouter = config.isEdgeDevice(deviceId);
123 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
124 } catch (DeviceConfigNotFoundException e) {
125 log.warn(e.getMessage()
126 + " Skipping value assignment in DefaultGroupHandler");
127 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700128 this.flowObjectiveService = flowObjService;
Charles Chane849c192016-01-11 18:28:54 -0800129 this.nsNextObjStore = srManager.nsNextObjStore;
Charles Chan59cc16d2017-02-02 16:20:42 -0800130 this.vlanNextObjStore = srManager.vlanNextObjStore;
Charles Chane849c192016-01-11 18:28:54 -0800131 this.portNextObjStore = srManager.portNextObjStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800132 this.srManager = srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800133
134 populateNeighborMaps();
135 }
136
137 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700138 * Creates a group handler object based on the type of device. If device is
139 * of edge type it returns edge group handler, else it returns transit group
140 * handler.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800141 *
142 * @param deviceId device identifier
143 * @param appId application identifier
144 * @param config interface to retrieve the device properties
145 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700146 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800147 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800148 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800149 * @return default group handler type
150 */
Saurav Das4ce45962015-11-24 23:21:05 -0800151 public static DefaultGroupHandler createGroupHandler(
152 DeviceId deviceId,
153 ApplicationId appId,
154 DeviceProperties config,
155 LinkService linkService,
156 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800157 SegmentRoutingManager srManager)
Saurav Das4ce45962015-11-24 23:21:05 -0800158 throws DeviceConfigNotFoundException {
Charles Chan0b4e6182015-11-03 10:42:14 -0800159 // handle possible exception in the caller
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800160 if (config.isEdgeDevice(deviceId)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700161 return new DefaultEdgeGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700162 linkService,
163 flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800164 srManager
165 );
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800166 } else {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700167 return new DefaultTransitGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700168 linkService,
169 flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800170 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800171 }
172 }
173
174 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700175 * Creates the auto created groups for this device based on the current
176 * snapshot of the topology.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800177 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700178 // Empty implementations to be overridden by derived classes
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800179 public void createGroups() {
180 }
181
182 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700183 * Performs group creation or update procedures when a new link is
184 * discovered on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800185 *
186 * @param newLink new neighbor link
Charles Chane849c192016-01-11 18:28:54 -0800187 * @param isMaster true if local instance is the master
188 *
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800189 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800190 public void linkUp(Link newLink, boolean isMaster) {
sanghob35a6192015-04-01 13:05:26 -0700191
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800192 if (newLink.type() != Link.Type.DIRECT) {
193 log.warn("linkUp: unknown link type");
194 return;
195 }
196
197 if (!newLink.src().deviceId().equals(deviceId)) {
198 log.warn("linkUp: deviceId{} doesn't match with link src{}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700199 deviceId, newLink.src().deviceId());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800200 return;
201 }
202
Saurav Das8a0732e2015-11-20 15:27:53 -0800203 log.info("* LinkUP: Device {} linkUp at local port {} to neighbor {}", deviceId,
204 newLink.src().port(), newLink.dst().deviceId());
Saurav Das59232cf2016-04-27 18:35:50 -0700205 // ensure local state is updated even if linkup is aborted later on
206 addNeighborAtPort(newLink.dst().deviceId(),
207 newLink.src().port());
208
Charles Chan0b4e6182015-11-03 10:42:14 -0800209 MacAddress dstMac;
210 try {
211 dstMac = deviceConfig.getDeviceMac(newLink.dst().deviceId());
212 } catch (DeviceConfigNotFoundException e) {
213 log.warn(e.getMessage() + " Aborting linkUp.");
214 return;
215 }
216
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700217 /*if (devicePortMap.get(newLink.dst().deviceId()) == null) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800218 // New Neighbor
219 newNeighbor(newLink);
220 } else {
221 // Old Neighbor
222 newPortToExistingNeighbor(newLink);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700223 }*/
224 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
225 .stream()
226 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
227 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
228 .filter((ns) -> (ns.getDeviceIds()
229 .contains(newLink.dst().deviceId())))
230 .collect(Collectors.toSet());
231 log.trace("linkUp: nsNextObjStore contents for device {}:",
sangho0b2b6d12015-05-20 22:16:38 -0700232 deviceId,
233 nsSet);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700234 for (NeighborSet ns : nsSet) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700235 Integer nextId = nsNextObjStore.
236 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Saurav Das4ce45962015-11-24 23:21:05 -0800237 if (nextId != null && isMaster) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800238 // Create the new bucket to be updated
239 TrafficTreatment.Builder tBuilder =
240 DefaultTrafficTreatment.builder();
241 tBuilder.setOutput(newLink.src().port())
242 .setEthDst(dstMac)
243 .setEthSrc(nodeMacAddr);
244 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
245 tBuilder.pushMpls()
246 .copyTtlOut()
247 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
248 }
249 // setup metadata to pass to nextObjective - indicate the vlan on egress
250 // if needed by the switch pipeline. Since hashed next-hops are always to
251 // other neighboring routers, there is no subnet assigned on those ports.
252 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800253 metabuilder.matchVlanId(INTERNAL_VLAN);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700254
Saurav Das423fe2b2015-12-04 10:52:59 -0800255 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
256 .withId(nextId)
257 .withType(NextObjective.Type.HASHED)
258 .addTreatment(tBuilder.build())
259 .withMeta(metabuilder.build())
260 .fromApp(appId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800261 log.info("**linkUp in device {}: Adding Bucket "
Saurav Das4ce45962015-11-24 23:21:05 -0800262 + "with Port {} to next object id {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700263 deviceId,
264 newLink.src().port(),
Saurav Das4ce45962015-11-24 23:21:05 -0800265 nextId);
Charles Chan216e3c82016-04-23 14:48:16 -0700266
267 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700268 (objective) -> log.debug("LinkUp addedTo NextObj {} on {}",
Charles Chan216e3c82016-04-23 14:48:16 -0700269 nextId, deviceId),
270 (objective, error) ->
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700271 log.warn("LinkUp failed to addTo NextObj {} on {}: {}",
Charles Chan216e3c82016-04-23 14:48:16 -0700272 nextId, deviceId, error));
273 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
Saurav Das4ce45962015-11-24 23:21:05 -0800274 flowObjectiveService.next(deviceId, nextObjective);
275 } else if (isMaster) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800276 log.warn("linkUp in device {}, but global store has no record "
277 + "for neighbor-set {}", deviceId, ns);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700278 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800279 }
280 }
281
282 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800283 * Performs hash group recovery procedures when a switch-to-switch
284 * port goes down on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800285 *
286 * @param port port number that has gone down
Charles Chane849c192016-01-11 18:28:54 -0800287 * @param isMaster true if local instance is the master
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800288 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800289 public void portDown(PortNumber port, boolean isMaster) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800290 if (portDeviceMap.get(port) == null) {
291 log.warn("portDown: unknown port");
292 return;
293 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800294
295 MacAddress dstMac;
296 try {
297 dstMac = deviceConfig.getDeviceMac(portDeviceMap.get(port));
298 } catch (DeviceConfigNotFoundException e) {
299 log.warn(e.getMessage() + " Aborting portDown.");
300 return;
301 }
302
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700303 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
304 portDeviceMap.get(port));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700305 /*Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(portDeviceMap
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700306 .get(port),
307 devicePortMap
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700308 .keySet());*/
309 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
310 .stream()
311 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
312 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
313 .filter((ns) -> (ns.getDeviceIds()
314 .contains(portDeviceMap.get(port))))
315 .collect(Collectors.toSet());
Saurav Das423fe2b2015-12-04 10:52:59 -0800316 log.debug("portDown: nsNextObjStore contents for device {}:{}",
317 deviceId, nsSet);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800318 for (NeighborSet ns : nsSet) {
Saurav Das80980c72016-03-23 11:22:49 -0700319 NeighborSetNextObjectiveStoreKey nsStoreKey =
320 new NeighborSetNextObjectiveStoreKey(deviceId, ns);
321 Integer nextId = nsNextObjStore.get(nsStoreKey);
Saurav Das423fe2b2015-12-04 10:52:59 -0800322 if (nextId != null && isMaster) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800323 log.info("**portDown in device {}: Removing Bucket "
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700324 + "with Port {} to next object id {}",
325 deviceId,
326 port,
327 nextId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800328 // Create the bucket to be removed
329 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
330 .builder();
331 tBuilder.setOutput(port)
332 .setEthDst(dstMac)
333 .setEthSrc(nodeMacAddr);
334 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
335 tBuilder.pushMpls()
336 .copyTtlOut()
337 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
338 }
339 NextObjective.Builder nextObjBuilder = DefaultNextObjective
340 .builder()
341 .withType(NextObjective.Type.HASHED) //same as original
342 .withId(nextId)
343 .fromApp(appId)
344 .addTreatment(tBuilder.build());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700345 ObjectiveContext context = new DefaultObjectiveContext(
346 (objective) -> log.debug("portDown removedFrom NextObj {} on {}",
347 nextId, deviceId),
348 (objective, error) ->
349 log.warn("portDown failed to removeFrom NextObj {} on {}: {}",
350 nextId, deviceId, error));
Saurav Das423fe2b2015-12-04 10:52:59 -0800351 NextObjective nextObjective = nextObjBuilder.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700352 removeFromExisting(context);
sangho834e4b02015-05-01 09:38:25 -0700353
Saurav Das423fe2b2015-12-04 10:52:59 -0800354 flowObjectiveService.next(deviceId, nextObjective);
sangho834e4b02015-05-01 09:38:25 -0700355 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800356 }
357
358 devicePortMap.get(portDeviceMap.get(port)).remove(port);
359 portDeviceMap.remove(port);
360 }
361
362 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800363 * Adds or removes a port that has been configured with a subnet to a broadcast group
364 * for bridging. Note that this does not create the broadcast group itself.
Saurav Das018605f2017-02-18 14:05:44 -0800365 * Should only be called by the master instance for this device/port.
Saurav Das1a129a02016-11-18 15:21:57 -0800366 *
367 * @param port the port on this device that needs to be added/removed to a bcast group
Charles Chan59cc16d2017-02-02 16:20:42 -0800368 * @param vlanId the vlan id corresponding to the broadcast group
Saurav Das1a129a02016-11-18 15:21:57 -0800369 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800370 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800371 public void processEdgePort(PortNumber port, VlanId vlanId, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800372 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800373 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800374 if (nextId == -1) {
375 if (portUp) {
376 log.debug("**Creating flooding group for first port enabled in"
Charles Chan59cc16d2017-02-02 16:20:42 -0800377 + " subnet {} on dev {} port {}", vlanId, deviceId, port);
378 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800379 } else {
380 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800381 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800382 }
383 return;
384 }
385
386 log.info("**port{} in device {}: {} Bucket with Port {} to"
387 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
388 (portUp) ? "Adding" : "Removing",
389 port, nextId);
390 // Create the bucket to be added or removed
391 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
392 tBuilder.popVlan();
393 tBuilder.setOutput(port);
394
Charles Chan59cc16d2017-02-02 16:20:42 -0800395 VlanId untaggedVlan = srManager.getUntaggedVlanId(new ConnectPoint(deviceId, port));
396 VlanId assignedVlanId = (untaggedVlan != null) ? untaggedVlan : INTERNAL_VLAN;
397
Saurav Das1a129a02016-11-18 15:21:57 -0800398 TrafficSelector metadata =
399 DefaultTrafficSelector.builder().matchVlanId(assignedVlanId).build();
400
401 NextObjective.Builder nextObjBuilder = DefaultNextObjective
402 .builder().withId(nextId)
403 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
404 .addTreatment(tBuilder.build())
405 .withMeta(metadata);
406
407 ObjectiveContext context = new DefaultObjectiveContext(
408 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
409 port, (portUp) ? "addedTo" : "removedFrom",
410 nextId, deviceId),
411 (objective, error) ->
412 log.warn("port {} failed to {} NextObj {} on {}: {}",
413 port, (portUp) ? "addTo" : "removeFrom",
414 nextId, deviceId, error));
415
416 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
417 : nextObjBuilder.removeFromExisting(context);
418 log.debug("edgePort processed: Submited next objective {} in device {}",
419 nextId, deviceId);
420 flowObjectiveService.next(deviceId, nextObj);
421 }
422
423 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800424 * Returns the next objective of type hashed associated with the neighborset.
425 * If there is no next objective for this neighborset, this method
Saurav Das8a0732e2015-11-20 15:27:53 -0800426 * would create a next objective and return. Optionally metadata can be
427 * passed in for the creation of the next objective.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800428 *
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700429 * @param ns neighborset
Saurav Das8a0732e2015-11-20 15:27:53 -0800430 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700431 * @param isBos if Bos is set
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 * @return int if found or -1 if there are errors in the creation of the
433 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800434 */
Pier Ventre917127a2016-10-31 16:49:19 -0700435 public int getNextObjectiveId(NeighborSet ns, TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700436 Integer nextId = nsNextObjStore.
437 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700438 if (nextId == null) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700439 log.trace("getNextObjectiveId in device{}: Next objective id "
440 + "not found for {} and creating", deviceId, ns);
441 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
442 deviceId,
443 nsNextObjStore.entrySet()
444 .stream()
445 .filter((nsStoreEntry) ->
446 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
447 .collect(Collectors.toList()));
Pier Ventre917127a2016-10-31 16:49:19 -0700448 createGroupsFromNeighborsets(Collections.singleton(ns), meta, isBos);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700449 nextId = nsNextObjStore.
450 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700451 if (nextId == null) {
452 log.warn("getNextObjectiveId: unable to create next objective");
453 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700454 } else {
455 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700456 + "created for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700457 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700458 } else {
459 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700460 + "found for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700461 }
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700462 return nextId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800463 }
464
sangho0b2b6d12015-05-20 22:16:38 -0700465 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800466 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800467 * or -1 if no such objective exists. Note that this method does NOT create
468 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800469 * created at startup from network configuration. Typically this is used
470 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700471 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800472 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700473 * @return int if found or -1
474 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800475 public int getVlanNextObjectiveId(VlanId vlanId) {
476 Integer nextId = vlanNextObjStore.
477 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700478
479 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700480 }
481
482 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800483 * Returns the next objective of type simple associated with the port on the
484 * device, given the treatment. Different treatments to the same port result
485 * in different next objectives. If no such objective exists, this method
486 * creates one and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800487 * the creation of the objective. Typically this is used for L2 and L3 forwarding
488 * to compute nodes and containers/VMs on the compute nodes directly attached
489 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800490 *
491 * @param portNum the port number for the simple next objective
492 * @param treatment the actions to apply on the packets (should include outport)
493 * @param meta optional metadata passed into the creation of the next objective
494 * @return int if found or created, -1 if there are errors during the
495 * creation of the next objective.
496 */
497 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
498 TrafficSelector meta) {
Charles Chane849c192016-01-11 18:28:54 -0800499 Integer nextId = portNextObjStore
500 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
Saurav Das4ce45962015-11-24 23:21:05 -0800501 if (nextId == null) {
502 log.trace("getPortNextObjectiveId in device{}: Next objective id "
503 + "not found for {} and {} creating", deviceId, portNum);
504 createGroupFromPort(portNum, treatment, meta);
505 nextId = portNextObjStore.get(
506 new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
507 if (nextId == null) {
508 log.warn("getPortNextObjectiveId: unable to create next obj"
Charles Chane849c192016-01-11 18:28:54 -0800509 + "for dev:{} port:{}", deviceId, portNum);
510 return -1;
511 }
512 }
513 return nextId;
514 }
515
516 /**
sangho0b2b6d12015-05-20 22:16:38 -0700517 * Checks if the next objective ID (group) for the neighbor set exists or not.
518 *
519 * @param ns neighbor set to check
520 * @return true if it exists, false otherwise
521 */
522 public boolean hasNextObjectiveId(NeighborSet ns) {
523 Integer nextId = nsNextObjStore.
524 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
525 if (nextId == null) {
526 return false;
527 }
528
529 return true;
530 }
531
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700532 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800533 protected void newNeighbor(Link newLink) {
534 }
535
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700536 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800537 protected void newPortToExistingNeighbor(Link newLink) {
538 }
539
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700540 // Empty implementation
541 protected Set<NeighborSet>
542 computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor,
543 Set<DeviceId> updatedNeighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800544 return null;
545 }
546
547 private void populateNeighborMaps() {
548 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700549 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800550 if (link.type() != Link.Type.DIRECT) {
551 continue;
552 }
553 addNeighborAtPort(link.dst().deviceId(), link.src().port());
554 }
555 }
556
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700557 protected void addNeighborAtPort(DeviceId neighborId,
558 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800559 // Update DeviceToPort database
560 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
561 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800562 Set<PortNumber> ports = Collections
563 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
564 ports.add(portToNeighbor);
565 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
566 if (portnums != null) {
567 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800568 }
569
570 // Update portToDevice database
Saurav Das8a0732e2015-11-20 15:27:53 -0800571 DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId);
572 if (prev != null) {
573 log.warn("Device: {} port: {} has neighbor: {}. NOT updating "
574 + "to neighbor: {}", deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800575 }
576 }
577
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700578 protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700579 List<DeviceId> list = new ArrayList<>(neighbors);
580 Set<Set<DeviceId>> sets = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800581 // get the number of elements in the neighbors
582 int elements = list.size();
583 // the number of members of a power set is 2^n
584 // including the empty set
585 int powerElements = (1 << elements);
586
587 // run a binary counter for the number of power elements
588 // NOTE: Exclude empty set
589 for (long i = 1; i < powerElements; i++) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700590 Set<DeviceId> neighborSubSet = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800591 for (int j = 0; j < elements; j++) {
592 if ((i >> j) % 2 == 1) {
593 neighborSubSet.add(list.get(j));
594 }
595 }
596 sets.add(neighborSubSet);
597 }
598 return sets;
599 }
600
601 private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800602 int segmentId;
603 try {
Pier Ventre917127a2016-10-31 16:49:19 -0700604 // IPv6 sid is not inserted. this part of the code is not used for now.
Pier Ventree0ae7a32016-11-23 09:57:42 -0800605 segmentId = deviceConfig.getIPv4SegmentId(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800606 } catch (DeviceConfigNotFoundException e) {
607 log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId.");
608 return false;
609 }
610
611 return segmentId == sId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800612 }
613
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700614 protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800615
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700616 List<Integer> nsSegmentIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800617
sanghob35a6192015-04-01 13:05:26 -0700618 // Always pair up with no edge label
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700619 // If (neighbors.size() == 1) {
sanghob35a6192015-04-01 13:05:26 -0700620 nsSegmentIds.add(-1);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700621 // }
sanghob35a6192015-04-01 13:05:26 -0700622
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800623 // Filter out SegmentIds matching with the
624 // nodes in the combo
625 for (Integer sId : allSegmentIds) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800626 if (sId.equals(this.ipv4NodeSegmentId)) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800627 continue;
628 }
629 boolean filterOut = false;
630 // Check if the edge label being set is of
631 // any node in the Neighbor set
632 for (DeviceId deviceId : neighbors) {
633 if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) {
634 filterOut = true;
635 break;
636 }
637 }
638 if (!filterOut) {
639 nsSegmentIds.add(sId);
640 }
641 }
642 return nsSegmentIds;
643 }
644
sangho1e575652015-05-14 00:39:53 -0700645 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800646 * Creates hash groups from a set of NeighborSet given.
sangho1e575652015-05-14 00:39:53 -0700647 *
648 * @param nsSet a set of NeighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800649 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700650 * @param isBos if BoS is set
sangho1e575652015-05-14 00:39:53 -0700651 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800652 public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet,
Pier Ventre917127a2016-10-31 16:49:19 -0700653 TrafficSelector meta,
654 boolean isBos) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800655 for (NeighborSet ns : nsSet) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700656 int nextId = flowObjectiveService.allocateNextId();
Pier Ventre917127a2016-10-31 16:49:19 -0700657 NextObjective.Type type = NextObjective.Type.HASHED;
658 Set<DeviceId> neighbors = ns.getDeviceIds();
659 // If Bos == False and MPLS-ECMP == false, we have
660 // to use simple group and we will pick a single neighbor.
661 if (!isBos && !srManager.getMplsEcmp()) {
662 type = NextObjective.Type.SIMPLE;
663 neighbors = Collections.singleton(ns.getFirstNeighbor());
664 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700665 NextObjective.Builder nextObjBuilder = DefaultNextObjective
Pier Ventre917127a2016-10-31 16:49:19 -0700666 .builder()
667 .withId(nextId)
668 .withType(type)
669 .fromApp(appId);
670 // For each neighbor, we have to update the sent actions
671 for (DeviceId neighborId : neighbors) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800672 if (devicePortMap.get(neighborId) == null) {
673 log.warn("Neighbor {} is not in the port map yet for dev:{}",
674 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700675 return;
Jon Hallcbd1b392017-01-18 20:15:44 -0800676 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700677 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800678 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700679 return;
sangho834e4b02015-05-01 09:38:25 -0700680 }
681
Saurav Das8a0732e2015-11-20 15:27:53 -0800682 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800683 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800684 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800685 } catch (DeviceConfigNotFoundException e) {
686 log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets.");
687 return;
688 }
Pier Ventre917127a2016-10-31 16:49:19 -0700689 // For each port, we have to create a new treatment
690 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
691 // In this case we are using a SIMPLE group. We randomly pick a port
692 if (!isBos && !srManager.getMplsEcmp()) {
693 int size = devicePortMap.get(neighborId).size();
694 int index = RandomUtils.nextInt(0, size);
695 neighborPorts = Collections.singleton(
696 Iterables.get(devicePortMap.get(neighborId), index)
697 );
698 }
699 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700700 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
701 .builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800702 tBuilder.setEthDst(neighborMac)
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700703 .setEthSrc(nodeMacAddr);
704 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800705 tBuilder.pushMpls()
706 .copyTtlOut()
707 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700708 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800709 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700710 nextObjBuilder.addTreatment(tBuilder.build());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800711 }
712 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800713 if (meta != null) {
Saurav Das4ce45962015-11-24 23:21:05 -0800714 nextObjBuilder.withMeta(meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800715 }
Charles Chan216e3c82016-04-23 14:48:16 -0700716
717 ObjectiveContext context = new DefaultObjectiveContext(
Pier Ventre917127a2016-10-31 16:49:19 -0700718 (objective) ->
719 log.debug("createGroupsFromNeighborsets installed NextObj {} on {}",
Charles Chan216e3c82016-04-23 14:48:16 -0700720 nextId, deviceId),
721 (objective, error) ->
722 log.warn("createGroupsFromNeighborsets failed to install NextObj {} on {}: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700723 nextId, deviceId, error)
724 );
Charles Chan216e3c82016-04-23 14:48:16 -0700725 NextObjective nextObj = nextObjBuilder.add(context);
726 log.debug("**createGroupsFromNeighborsets: Submited "
Saurav Das8a0732e2015-11-20 15:27:53 -0800727 + "next objective {} in device {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700728 nextId, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800729 flowObjectiveService.next(deviceId, nextObj);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700730 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns),
731 nextId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800732 }
733 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700734
Saurav Das4ce45962015-11-24 23:21:05 -0800735 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800736 * Creates broadcast groups for all ports in the same subnet for
737 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -0800738 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800739 public void createGroupsFromVlanConfig() {
740 Set<Interface> interfaces = srManager.interfaceService.getInterfaces();
Charles Chan59cc16d2017-02-02 16:20:42 -0800741
Charles Chan7ffd81f2017-02-08 15:52:08 -0800742 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -0800743 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800744 });
Saurav Das1a129a02016-11-18 15:21:57 -0800745 }
Charles Chan9f676b62015-10-29 14:58:10 -0700746
Saurav Das1a129a02016-11-18 15:21:57 -0800747 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800748 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -0800749 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800750 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -0800751 * @param ports list of ports in the subnet
752 */
Charles Chan7ffd81f2017-02-08 15:52:08 -0800753 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800754 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -0700755
Charles Chan59cc16d2017-02-02 16:20:42 -0800756 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -0800757 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -0800758 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800759 return;
760 }
Charles Chan188ebf52015-12-23 00:15:11 -0800761
Saurav Das1a129a02016-11-18 15:21:57 -0800762 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -0800763 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -0700764
Saurav Das1a129a02016-11-18 15:21:57 -0800765 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -0700766
Saurav Das1a129a02016-11-18 15:21:57 -0800767 NextObjective.Builder nextObjBuilder = DefaultNextObjective
768 .builder().withId(nextId)
769 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
770 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -0700771
Saurav Das1a129a02016-11-18 15:21:57 -0800772 ports.forEach(port -> {
773 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -0800774 if (toPopVlan(port, vlanId)) {
775 tBuilder.popVlan();
776 }
Saurav Das1a129a02016-11-18 15:21:57 -0800777 tBuilder.setOutput(port);
778 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -0700779 });
Saurav Das1a129a02016-11-18 15:21:57 -0800780
781 NextObjective nextObj = nextObjBuilder.add();
782 flowObjectiveService.next(deviceId, nextObj);
Charles Chan59cc16d2017-02-02 16:20:42 -0800783 log.debug("createBcastGroupFromVlan: Submited next objective {} in device {}",
Saurav Das1a129a02016-11-18 15:21:57 -0800784 nextId, deviceId);
785
Charles Chan59cc16d2017-02-02 16:20:42 -0800786 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700787 }
788
Charles Chane849c192016-01-11 18:28:54 -0800789 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800790 * Determine if we should pop given vlan before sending packets to the given port.
791 *
792 * @param portNumber port number
793 * @param vlanId vlan id
794 * @return true if the vlan id is not contained in any vlanTagged config
795 */
796 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
797 return srManager.interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
798 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
799 }
800
801 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800802 * Create simple next objective for a single port. The treatments can include
803 * all outgoing actions that need to happen on the packet.
804 *
805 * @param portNum the outgoing port on the device
806 * @param treatment the actions to apply on the packets (should include outport)
807 * @param meta optional data to pass to the driver
808 */
809 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
810 TrafficSelector meta) {
811 int nextId = flowObjectiveService.allocateNextId();
812 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
813 deviceId, portNum, treatment);
814
815 NextObjective.Builder nextObjBuilder = DefaultNextObjective
816 .builder().withId(nextId)
817 .withType(NextObjective.Type.SIMPLE)
818 .addTreatment(treatment)
819 .fromApp(appId)
820 .withMeta(meta);
821
822 NextObjective nextObj = nextObjBuilder.add();
823 flowObjectiveService.next(deviceId, nextObj);
824 log.debug("createGroupFromPort: Submited next objective {} in device {} "
825 + "for port {}", nextId, deviceId, portNum);
826
827 portNextObjStore.put(key, nextId);
828 }
829
sangho1e575652015-05-14 00:39:53 -0700830 /**
831 * Removes groups for the next objective ID given.
832 *
833 * @param objectiveId next objective ID to remove
834 * @return true if succeeds, false otherwise
835 */
836 public boolean removeGroup(int objectiveId) {
837
838 if (nsNextObjStore.containsValue(objectiveId)) {
839 NextObjective.Builder nextObjBuilder = DefaultNextObjective
840 .builder().withId(objectiveId)
841 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -0700842 ObjectiveContext context = new DefaultObjectiveContext(
843 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
844 objectiveId, deviceId),
845 (objective, error) ->
846 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
847 objectiveId, deviceId, error));
848 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -0800849 log.info("**removeGroup: Submited "
850 + "next objective {} in device {}",
851 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -0700852 flowObjectiveService.next(deviceId, nextObjective);
853
854 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) {
855 if (entry.getValue().equals(objectiveId)) {
856 nsNextObjStore.remove(entry.getKey());
857 break;
858 }
859 }
sangho0b2b6d12015-05-20 22:16:38 -0700860 return true;
sangho1e575652015-05-14 00:39:53 -0700861 }
862
863 return false;
864 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700865
Charles Chane849c192016-01-11 18:28:54 -0800866 /**
867 * Removes all groups from all next objective stores.
868 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800869 public void removeAllGroups() {
870 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry:
871 nsNextObjStore.entrySet()) {
872 removeGroup(entry.getValue());
873 }
874 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
875 portNextObjStore.entrySet()) {
876 removeGroup(entry.getValue());
877 }
Charles Chan59cc16d2017-02-02 16:20:42 -0800878 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
879 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800880 removeGroup(entry.getValue());
881 }
882 // should probably clean local stores port-neighbor
883 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800884}