blob: 93dfd0c74bbd0dfe4cf8c92d7f899dad89d97c0d [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 Dasb0ae6ee2017-03-04 16:08:47 -0800363 * Adds or removes a port that has been configured with a vlan to a broadcast group
364 * for bridging. Should only be called by the master instance for this device.
Saurav Das1a129a02016-11-18 15:21:57 -0800365 *
366 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800367 * @param vlanId the vlan id corresponding to the broadcast domain/group
368 * @param popVlan indicates if packets should be sent out untagged or not out
369 * of the port. If true, indicates an access (untagged) or native vlan
370 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Das1a129a02016-11-18 15:21:57 -0800371 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800372 */
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800373 public void processEdgePort(PortNumber port, VlanId vlanId,
374 boolean popVlan, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800375 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800376 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800377 if (nextId == -1) {
378 if (portUp) {
379 log.debug("**Creating flooding group for first port enabled in"
Charles Chan59cc16d2017-02-02 16:20:42 -0800380 + " subnet {} on dev {} port {}", vlanId, deviceId, port);
381 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800382 } else {
383 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800384 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800385 }
386 return;
387 }
388
389 log.info("**port{} in device {}: {} Bucket with Port {} to"
390 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
391 (portUp) ? "Adding" : "Removing",
392 port, nextId);
393 // Create the bucket to be added or removed
394 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800395 if (popVlan) {
396 tBuilder.popVlan();
397 }
Saurav Das1a129a02016-11-18 15:21:57 -0800398 tBuilder.setOutput(port);
399
Saurav Das1a129a02016-11-18 15:21:57 -0800400 TrafficSelector metadata =
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800401 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Das1a129a02016-11-18 15:21:57 -0800402
403 NextObjective.Builder nextObjBuilder = DefaultNextObjective
404 .builder().withId(nextId)
405 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
406 .addTreatment(tBuilder.build())
407 .withMeta(metadata);
408
409 ObjectiveContext context = new DefaultObjectiveContext(
410 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
411 port, (portUp) ? "addedTo" : "removedFrom",
412 nextId, deviceId),
413 (objective, error) ->
414 log.warn("port {} failed to {} NextObj {} on {}: {}",
415 port, (portUp) ? "addTo" : "removeFrom",
416 nextId, deviceId, error));
417
418 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
419 : nextObjBuilder.removeFromExisting(context);
420 log.debug("edgePort processed: Submited next objective {} in device {}",
421 nextId, deviceId);
422 flowObjectiveService.next(deviceId, nextObj);
423 }
424
425 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800426 * Returns the next objective of type hashed associated with the neighborset.
427 * If there is no next objective for this neighborset, this method
Saurav Das8a0732e2015-11-20 15:27:53 -0800428 * would create a next objective and return. Optionally metadata can be
429 * passed in for the creation of the next objective.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800430 *
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700431 * @param ns neighborset
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700433 * @param isBos if Bos is set
Saurav Das8a0732e2015-11-20 15:27:53 -0800434 * @return int if found or -1 if there are errors in the creation of the
435 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800436 */
Pier Ventre917127a2016-10-31 16:49:19 -0700437 public int getNextObjectiveId(NeighborSet ns, TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700438 Integer nextId = nsNextObjStore.
439 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700440 if (nextId == null) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700441 log.trace("getNextObjectiveId in device{}: Next objective id "
442 + "not found for {} and creating", deviceId, ns);
443 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
444 deviceId,
445 nsNextObjStore.entrySet()
446 .stream()
447 .filter((nsStoreEntry) ->
448 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
449 .collect(Collectors.toList()));
Pier Ventre917127a2016-10-31 16:49:19 -0700450 createGroupsFromNeighborsets(Collections.singleton(ns), meta, isBos);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700451 nextId = nsNextObjStore.
452 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700453 if (nextId == null) {
454 log.warn("getNextObjectiveId: unable to create next objective");
455 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700456 } else {
457 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700458 + "created for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700459 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700460 } else {
461 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700462 + "found for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700463 }
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700464 return nextId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800465 }
466
sangho0b2b6d12015-05-20 22:16:38 -0700467 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800468 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800469 * or -1 if no such objective exists. Note that this method does NOT create
470 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800471 * created at startup from network configuration. Typically this is used
472 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700473 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800474 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700475 * @return int if found or -1
476 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800477 public int getVlanNextObjectiveId(VlanId vlanId) {
478 Integer nextId = vlanNextObjStore.
479 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700480
481 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700482 }
483
484 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800485 * Returns the next objective of type simple associated with the port on the
486 * device, given the treatment. Different treatments to the same port result
487 * in different next objectives. If no such objective exists, this method
488 * creates one and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800489 * the creation of the objective. Typically this is used for L2 and L3 forwarding
490 * to compute nodes and containers/VMs on the compute nodes directly attached
491 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800492 *
493 * @param portNum the port number for the simple next objective
494 * @param treatment the actions to apply on the packets (should include outport)
495 * @param meta optional metadata passed into the creation of the next objective
496 * @return int if found or created, -1 if there are errors during the
497 * creation of the next objective.
498 */
499 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
500 TrafficSelector meta) {
Charles Chane849c192016-01-11 18:28:54 -0800501 Integer nextId = portNextObjStore
502 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
Saurav Das4ce45962015-11-24 23:21:05 -0800503 if (nextId == null) {
504 log.trace("getPortNextObjectiveId in device{}: Next objective id "
505 + "not found for {} and {} creating", deviceId, portNum);
506 createGroupFromPort(portNum, treatment, meta);
507 nextId = portNextObjStore.get(
508 new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
509 if (nextId == null) {
510 log.warn("getPortNextObjectiveId: unable to create next obj"
Charles Chane849c192016-01-11 18:28:54 -0800511 + "for dev:{} port:{}", deviceId, portNum);
512 return -1;
513 }
514 }
515 return nextId;
516 }
517
518 /**
sangho0b2b6d12015-05-20 22:16:38 -0700519 * Checks if the next objective ID (group) for the neighbor set exists or not.
520 *
521 * @param ns neighbor set to check
522 * @return true if it exists, false otherwise
523 */
524 public boolean hasNextObjectiveId(NeighborSet ns) {
525 Integer nextId = nsNextObjStore.
526 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
527 if (nextId == null) {
528 return false;
529 }
530
531 return true;
532 }
533
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700534 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800535 protected void newNeighbor(Link newLink) {
536 }
537
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700538 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800539 protected void newPortToExistingNeighbor(Link newLink) {
540 }
541
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700542 // Empty implementation
543 protected Set<NeighborSet>
544 computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor,
545 Set<DeviceId> updatedNeighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800546 return null;
547 }
548
549 private void populateNeighborMaps() {
550 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700551 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800552 if (link.type() != Link.Type.DIRECT) {
553 continue;
554 }
555 addNeighborAtPort(link.dst().deviceId(), link.src().port());
556 }
557 }
558
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700559 protected void addNeighborAtPort(DeviceId neighborId,
560 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800561 // Update DeviceToPort database
562 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
563 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800564 Set<PortNumber> ports = Collections
565 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
566 ports.add(portToNeighbor);
567 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
568 if (portnums != null) {
569 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800570 }
571
572 // Update portToDevice database
Saurav Das8a0732e2015-11-20 15:27:53 -0800573 DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId);
574 if (prev != null) {
575 log.warn("Device: {} port: {} has neighbor: {}. NOT updating "
576 + "to neighbor: {}", deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800577 }
578 }
579
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700580 protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700581 List<DeviceId> list = new ArrayList<>(neighbors);
582 Set<Set<DeviceId>> sets = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800583 // get the number of elements in the neighbors
584 int elements = list.size();
585 // the number of members of a power set is 2^n
586 // including the empty set
587 int powerElements = (1 << elements);
588
589 // run a binary counter for the number of power elements
590 // NOTE: Exclude empty set
591 for (long i = 1; i < powerElements; i++) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700592 Set<DeviceId> neighborSubSet = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800593 for (int j = 0; j < elements; j++) {
594 if ((i >> j) % 2 == 1) {
595 neighborSubSet.add(list.get(j));
596 }
597 }
598 sets.add(neighborSubSet);
599 }
600 return sets;
601 }
602
603 private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800604 int segmentId;
605 try {
Pier Ventre917127a2016-10-31 16:49:19 -0700606 // IPv6 sid is not inserted. this part of the code is not used for now.
Pier Ventree0ae7a32016-11-23 09:57:42 -0800607 segmentId = deviceConfig.getIPv4SegmentId(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800608 } catch (DeviceConfigNotFoundException e) {
609 log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId.");
610 return false;
611 }
612
613 return segmentId == sId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800614 }
615
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700616 protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800617
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700618 List<Integer> nsSegmentIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800619
sanghob35a6192015-04-01 13:05:26 -0700620 // Always pair up with no edge label
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700621 // If (neighbors.size() == 1) {
sanghob35a6192015-04-01 13:05:26 -0700622 nsSegmentIds.add(-1);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700623 // }
sanghob35a6192015-04-01 13:05:26 -0700624
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800625 // Filter out SegmentIds matching with the
626 // nodes in the combo
627 for (Integer sId : allSegmentIds) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800628 if (sId.equals(this.ipv4NodeSegmentId)) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800629 continue;
630 }
631 boolean filterOut = false;
632 // Check if the edge label being set is of
633 // any node in the Neighbor set
634 for (DeviceId deviceId : neighbors) {
635 if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) {
636 filterOut = true;
637 break;
638 }
639 }
640 if (!filterOut) {
641 nsSegmentIds.add(sId);
642 }
643 }
644 return nsSegmentIds;
645 }
646
sangho1e575652015-05-14 00:39:53 -0700647 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800648 * Creates hash groups from a set of NeighborSet given.
sangho1e575652015-05-14 00:39:53 -0700649 *
650 * @param nsSet a set of NeighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800651 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700652 * @param isBos if BoS is set
sangho1e575652015-05-14 00:39:53 -0700653 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800654 public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet,
Pier Ventre917127a2016-10-31 16:49:19 -0700655 TrafficSelector meta,
656 boolean isBos) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800657 for (NeighborSet ns : nsSet) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700658 int nextId = flowObjectiveService.allocateNextId();
Pier Ventre917127a2016-10-31 16:49:19 -0700659 NextObjective.Type type = NextObjective.Type.HASHED;
660 Set<DeviceId> neighbors = ns.getDeviceIds();
661 // If Bos == False and MPLS-ECMP == false, we have
662 // to use simple group and we will pick a single neighbor.
663 if (!isBos && !srManager.getMplsEcmp()) {
664 type = NextObjective.Type.SIMPLE;
665 neighbors = Collections.singleton(ns.getFirstNeighbor());
666 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700667 NextObjective.Builder nextObjBuilder = DefaultNextObjective
Pier Ventre917127a2016-10-31 16:49:19 -0700668 .builder()
669 .withId(nextId)
670 .withType(type)
671 .fromApp(appId);
672 // For each neighbor, we have to update the sent actions
673 for (DeviceId neighborId : neighbors) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800674 if (devicePortMap.get(neighborId) == null) {
675 log.warn("Neighbor {} is not in the port map yet for dev:{}",
676 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700677 return;
Jon Hallcbd1b392017-01-18 20:15:44 -0800678 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700679 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800680 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700681 return;
sangho834e4b02015-05-01 09:38:25 -0700682 }
683
Saurav Das8a0732e2015-11-20 15:27:53 -0800684 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800685 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800686 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800687 } catch (DeviceConfigNotFoundException e) {
688 log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets.");
689 return;
690 }
Pier Ventre917127a2016-10-31 16:49:19 -0700691 // For each port, we have to create a new treatment
692 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
693 // In this case we are using a SIMPLE group. We randomly pick a port
694 if (!isBos && !srManager.getMplsEcmp()) {
695 int size = devicePortMap.get(neighborId).size();
696 int index = RandomUtils.nextInt(0, size);
697 neighborPorts = Collections.singleton(
698 Iterables.get(devicePortMap.get(neighborId), index)
699 );
700 }
701 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700702 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
703 .builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800704 tBuilder.setEthDst(neighborMac)
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700705 .setEthSrc(nodeMacAddr);
706 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800707 tBuilder.pushMpls()
708 .copyTtlOut()
709 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700710 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800711 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700712 nextObjBuilder.addTreatment(tBuilder.build());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800713 }
714 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800715 if (meta != null) {
Saurav Das4ce45962015-11-24 23:21:05 -0800716 nextObjBuilder.withMeta(meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800717 }
Charles Chan216e3c82016-04-23 14:48:16 -0700718
719 ObjectiveContext context = new DefaultObjectiveContext(
Pier Ventre917127a2016-10-31 16:49:19 -0700720 (objective) ->
721 log.debug("createGroupsFromNeighborsets installed NextObj {} on {}",
Charles Chan216e3c82016-04-23 14:48:16 -0700722 nextId, deviceId),
723 (objective, error) ->
724 log.warn("createGroupsFromNeighborsets failed to install NextObj {} on {}: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700725 nextId, deviceId, error)
726 );
Charles Chan216e3c82016-04-23 14:48:16 -0700727 NextObjective nextObj = nextObjBuilder.add(context);
728 log.debug("**createGroupsFromNeighborsets: Submited "
Saurav Das8a0732e2015-11-20 15:27:53 -0800729 + "next objective {} in device {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700730 nextId, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800731 flowObjectiveService.next(deviceId, nextObj);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700732 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns),
733 nextId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800734 }
735 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700736
Saurav Das4ce45962015-11-24 23:21:05 -0800737 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800738 * Creates broadcast groups for all ports in the same subnet for
739 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -0800740 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800741 public void createGroupsFromVlanConfig() {
742 Set<Interface> interfaces = srManager.interfaceService.getInterfaces();
Charles Chan59cc16d2017-02-02 16:20:42 -0800743
Charles Chan7ffd81f2017-02-08 15:52:08 -0800744 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -0800745 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800746 });
Saurav Das1a129a02016-11-18 15:21:57 -0800747 }
Charles Chan9f676b62015-10-29 14:58:10 -0700748
Saurav Das1a129a02016-11-18 15:21:57 -0800749 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800750 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -0800751 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800752 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -0800753 * @param ports list of ports in the subnet
754 */
Charles Chan7ffd81f2017-02-08 15:52:08 -0800755 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800756 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -0700757
Charles Chan59cc16d2017-02-02 16:20:42 -0800758 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -0800759 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -0800760 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800761 return;
762 }
Charles Chan188ebf52015-12-23 00:15:11 -0800763
Saurav Das1a129a02016-11-18 15:21:57 -0800764 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -0800765 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -0700766
Saurav Das1a129a02016-11-18 15:21:57 -0800767 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -0700768
Saurav Das1a129a02016-11-18 15:21:57 -0800769 NextObjective.Builder nextObjBuilder = DefaultNextObjective
770 .builder().withId(nextId)
771 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
772 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -0700773
Saurav Das1a129a02016-11-18 15:21:57 -0800774 ports.forEach(port -> {
775 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -0800776 if (toPopVlan(port, vlanId)) {
777 tBuilder.popVlan();
778 }
Saurav Das1a129a02016-11-18 15:21:57 -0800779 tBuilder.setOutput(port);
780 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -0700781 });
Saurav Das1a129a02016-11-18 15:21:57 -0800782
783 NextObjective nextObj = nextObjBuilder.add();
784 flowObjectiveService.next(deviceId, nextObj);
Charles Chan59cc16d2017-02-02 16:20:42 -0800785 log.debug("createBcastGroupFromVlan: Submited next objective {} in device {}",
Saurav Das1a129a02016-11-18 15:21:57 -0800786 nextId, deviceId);
787
Charles Chan59cc16d2017-02-02 16:20:42 -0800788 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700789 }
790
Charles Chane849c192016-01-11 18:28:54 -0800791 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800792 * Determine if we should pop given vlan before sending packets to the given port.
793 *
794 * @param portNumber port number
795 * @param vlanId vlan id
796 * @return true if the vlan id is not contained in any vlanTagged config
797 */
798 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
799 return srManager.interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
800 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
801 }
802
803 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800804 * Create simple next objective for a single port. The treatments can include
805 * all outgoing actions that need to happen on the packet.
806 *
807 * @param portNum the outgoing port on the device
808 * @param treatment the actions to apply on the packets (should include outport)
809 * @param meta optional data to pass to the driver
810 */
811 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
812 TrafficSelector meta) {
813 int nextId = flowObjectiveService.allocateNextId();
814 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
815 deviceId, portNum, treatment);
816
817 NextObjective.Builder nextObjBuilder = DefaultNextObjective
818 .builder().withId(nextId)
819 .withType(NextObjective.Type.SIMPLE)
820 .addTreatment(treatment)
821 .fromApp(appId)
822 .withMeta(meta);
823
824 NextObjective nextObj = nextObjBuilder.add();
825 flowObjectiveService.next(deviceId, nextObj);
826 log.debug("createGroupFromPort: Submited next objective {} in device {} "
827 + "for port {}", nextId, deviceId, portNum);
828
829 portNextObjStore.put(key, nextId);
830 }
831
sangho1e575652015-05-14 00:39:53 -0700832 /**
833 * Removes groups for the next objective ID given.
834 *
835 * @param objectiveId next objective ID to remove
836 * @return true if succeeds, false otherwise
837 */
838 public boolean removeGroup(int objectiveId) {
839
840 if (nsNextObjStore.containsValue(objectiveId)) {
841 NextObjective.Builder nextObjBuilder = DefaultNextObjective
842 .builder().withId(objectiveId)
843 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -0700844 ObjectiveContext context = new DefaultObjectiveContext(
845 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
846 objectiveId, deviceId),
847 (objective, error) ->
848 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
849 objectiveId, deviceId, error));
850 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -0800851 log.info("**removeGroup: Submited "
852 + "next objective {} in device {}",
853 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -0700854 flowObjectiveService.next(deviceId, nextObjective);
855
856 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) {
857 if (entry.getValue().equals(objectiveId)) {
858 nsNextObjStore.remove(entry.getKey());
859 break;
860 }
861 }
sangho0b2b6d12015-05-20 22:16:38 -0700862 return true;
sangho1e575652015-05-14 00:39:53 -0700863 }
864
865 return false;
866 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700867
Charles Chane849c192016-01-11 18:28:54 -0800868 /**
869 * Removes all groups from all next objective stores.
870 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800871 public void removeAllGroups() {
872 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry:
873 nsNextObjStore.entrySet()) {
874 removeGroup(entry.getValue());
875 }
876 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
877 portNextObjStore.entrySet()) {
878 removeGroup(entry.getValue());
879 }
Charles Chan59cc16d2017-02-02 16:20:42 -0800880 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
881 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800882 removeGroup(entry.getValue());
883 }
884 // should probably clean local stores port-neighbor
885 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800886}