blob: b2d52e91751e47cffcbfce3584e4e4f768973187 [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
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070021import java.net.URI;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080022import java.util.ArrayList;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070023import java.util.Collections;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import java.util.HashSet;
25import java.util.List;
sangho1e575652015-05-14 00:39:53 -070026import java.util.Map;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080027import java.util.Set;
Saurav Das8a0732e2015-11-20 15:27:53 -080028import java.util.concurrent.ConcurrentHashMap;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070029import java.util.stream.Collectors;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030
Charles Chanc42e84e2015-10-20 16:24:19 -070031import org.onlab.packet.Ip4Prefix;
32import org.onlab.packet.IpPrefix;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080033import org.onlab.packet.MacAddress;
sangho32a59322015-02-17 12:07:41 -080034import org.onlab.packet.MplsLabel;
Saurav Das423fe2b2015-12-04 10:52:59 -080035import org.onlab.packet.VlanId;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070036import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080037import org.onosproject.core.ApplicationId;
Charles Chane849c192016-01-11 18:28:54 -080038import org.onosproject.net.ConnectPoint;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080039import org.onosproject.net.DeviceId;
40import org.onosproject.net.Link;
41import org.onosproject.net.PortNumber;
Saurav Das423fe2b2015-12-04 10:52:59 -080042import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080043import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das8a0732e2015-11-20 15:27:53 -080044import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080045import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070046import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan216e3c82016-04-23 14:48:16 -070047import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070048import org.onosproject.net.flowobjective.FlowObjectiveService;
49import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070050import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080051import org.onosproject.net.link.LinkService;
Saurav Das423fe2b2015-12-04 10:52:59 -080052import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan0b4e6182015-11-03 10:42:14 -080053import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
54import org.onosproject.segmentrouting.config.DeviceProperties;
Charles Chand2990362016-04-18 13:44:03 -070055import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
56import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
57import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
58import org.onosproject.segmentrouting.storekey.XConnectNextObjectiveStoreKey;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070059import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080060import org.slf4j.Logger;
61
62/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070063 * Default ECMP group handler creation module. This component creates a set of
64 * ECMP groups for every neighbor that this device is connected to based on
65 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080066 */
67public class DefaultGroupHandler {
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070068 protected static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080069
70 protected final DeviceId deviceId;
71 protected final ApplicationId appId;
72 protected final DeviceProperties deviceConfig;
73 protected final List<Integer> allSegmentIds;
Charles Chan0b4e6182015-11-03 10:42:14 -080074 protected int nodeSegmentId = -1;
75 protected boolean isEdgeRouter = false;
76 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080077 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070078 protected FlowObjectiveService flowObjectiveService;
Saurav Das423fe2b2015-12-04 10:52:59 -080079 // local store for neighbor-device-ids and the set of ports on this device
80 // that connect to the same neighbor
Saurav Das8a0732e2015-11-20 15:27:53 -080081 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
82 new ConcurrentHashMap<>();
Saurav Das423fe2b2015-12-04 10:52:59 -080083 //local store for ports on this device connected to neighbor-device-id
Saurav Das8a0732e2015-11-20 15:27:53 -080084 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
85 new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -080086 protected EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
87 nsNextObjStore = null;
88 protected EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
89 subnetNextObjStore = null;
90 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
91 portNextObjStore = null;
92 protected EventuallyConsistentMap<XConnectNextObjectiveStoreKey, Integer>
93 xConnectNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -080094 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080095
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070096 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070097 .register(URI.class).register(HashSet.class)
98 .register(DeviceId.class).register(PortNumber.class)
99 .register(NeighborSet.class).register(PolicyGroupIdentifier.class)
100 .register(PolicyGroupParams.class)
101 .register(GroupBucketIdentifier.class)
102 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800103
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700104 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
105 DeviceProperties config,
106 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700107 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800108 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800109 this.deviceId = checkNotNull(deviceId);
110 this.appId = checkNotNull(appId);
111 this.deviceConfig = checkNotNull(config);
112 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800113 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
114 try {
115 this.nodeSegmentId = config.getSegmentId(deviceId);
116 this.isEdgeRouter = config.isEdgeDevice(deviceId);
117 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
118 } catch (DeviceConfigNotFoundException e) {
119 log.warn(e.getMessage()
120 + " Skipping value assignment in DefaultGroupHandler");
121 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700122 this.flowObjectiveService = flowObjService;
Charles Chane849c192016-01-11 18:28:54 -0800123 this.nsNextObjStore = srManager.nsNextObjStore;
124 this.subnetNextObjStore = srManager.subnetNextObjStore;
125 this.portNextObjStore = srManager.portNextObjStore;
126 this.xConnectNextObjStore = srManager.xConnectNextObjStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800127 this.srManager = srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800128
129 populateNeighborMaps();
130 }
131
132 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700133 * Creates a group handler object based on the type of device. If device is
134 * of edge type it returns edge group handler, else it returns transit group
135 * handler.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800136 *
137 * @param deviceId device identifier
138 * @param appId application identifier
139 * @param config interface to retrieve the device properties
140 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700141 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800142 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800143 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800144 * @return default group handler type
145 */
Saurav Das4ce45962015-11-24 23:21:05 -0800146 public static DefaultGroupHandler createGroupHandler(
147 DeviceId deviceId,
148 ApplicationId appId,
149 DeviceProperties config,
150 LinkService linkService,
151 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800152 SegmentRoutingManager srManager)
Saurav Das4ce45962015-11-24 23:21:05 -0800153 throws DeviceConfigNotFoundException {
Charles Chan0b4e6182015-11-03 10:42:14 -0800154 // handle possible exception in the caller
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800155 if (config.isEdgeDevice(deviceId)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700156 return new DefaultEdgeGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700157 linkService,
158 flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800159 srManager
160 );
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800161 } else {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700162 return new DefaultTransitGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700163 linkService,
164 flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800165 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800166 }
167 }
168
169 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700170 * Creates the auto created groups for this device based on the current
171 * snapshot of the topology.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800172 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700173 // Empty implementations to be overridden by derived classes
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800174 public void createGroups() {
175 }
176
177 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700178 * Performs group creation or update procedures when a new link is
179 * discovered on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800180 *
181 * @param newLink new neighbor link
Charles Chane849c192016-01-11 18:28:54 -0800182 * @param isMaster true if local instance is the master
183 *
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800184 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800185 public void linkUp(Link newLink, boolean isMaster) {
sanghob35a6192015-04-01 13:05:26 -0700186
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800187 if (newLink.type() != Link.Type.DIRECT) {
188 log.warn("linkUp: unknown link type");
189 return;
190 }
191
192 if (!newLink.src().deviceId().equals(deviceId)) {
193 log.warn("linkUp: deviceId{} doesn't match with link src{}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700194 deviceId, newLink.src().deviceId());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800195 return;
196 }
197
Saurav Das8a0732e2015-11-20 15:27:53 -0800198 log.info("* LinkUP: Device {} linkUp at local port {} to neighbor {}", deviceId,
199 newLink.src().port(), newLink.dst().deviceId());
Charles Chan0b4e6182015-11-03 10:42:14 -0800200 MacAddress dstMac;
201 try {
202 dstMac = deviceConfig.getDeviceMac(newLink.dst().deviceId());
203 } catch (DeviceConfigNotFoundException e) {
204 log.warn(e.getMessage() + " Aborting linkUp.");
205 return;
206 }
207
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700208 addNeighborAtPort(newLink.dst().deviceId(),
209 newLink.src().port());
210 /*if (devicePortMap.get(newLink.dst().deviceId()) == null) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800211 // New Neighbor
212 newNeighbor(newLink);
213 } else {
214 // Old Neighbor
215 newPortToExistingNeighbor(newLink);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700216 }*/
217 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
218 .stream()
219 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
220 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
221 .filter((ns) -> (ns.getDeviceIds()
222 .contains(newLink.dst().deviceId())))
223 .collect(Collectors.toSet());
224 log.trace("linkUp: nsNextObjStore contents for device {}:",
sangho0b2b6d12015-05-20 22:16:38 -0700225 deviceId,
226 nsSet);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700227 for (NeighborSet ns : nsSet) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700228 Integer nextId = nsNextObjStore.
229 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Saurav Das4ce45962015-11-24 23:21:05 -0800230 if (nextId != null && isMaster) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800231 // Create the new bucket to be updated
232 TrafficTreatment.Builder tBuilder =
233 DefaultTrafficTreatment.builder();
234 tBuilder.setOutput(newLink.src().port())
235 .setEthDst(dstMac)
236 .setEthSrc(nodeMacAddr);
237 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
238 tBuilder.pushMpls()
239 .copyTtlOut()
240 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
241 }
242 // setup metadata to pass to nextObjective - indicate the vlan on egress
243 // if needed by the switch pipeline. Since hashed next-hops are always to
244 // other neighboring routers, there is no subnet assigned on those ports.
245 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
246 metabuilder.matchVlanId(
247 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700248
Saurav Das423fe2b2015-12-04 10:52:59 -0800249 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
250 .withId(nextId)
251 .withType(NextObjective.Type.HASHED)
252 .addTreatment(tBuilder.build())
253 .withMeta(metabuilder.build())
254 .fromApp(appId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800255 log.info("**linkUp in device {}: Adding Bucket "
Saurav Das4ce45962015-11-24 23:21:05 -0800256 + "with Port {} to next object id {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700257 deviceId,
258 newLink.src().port(),
Saurav Das4ce45962015-11-24 23:21:05 -0800259 nextId);
Charles Chan216e3c82016-04-23 14:48:16 -0700260
261 ObjectiveContext context = new DefaultObjectiveContext(
262 (objective) -> log.debug("LinkUp installed NextObj {} on {}",
263 nextId, deviceId),
264 (objective, error) ->
265 log.warn("LinkUp failed to install NextObj {} on {}: {}",
266 nextId, deviceId, error));
267 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
Saurav Das4ce45962015-11-24 23:21:05 -0800268 flowObjectiveService.next(deviceId, nextObjective);
Saurav Das423fe2b2015-12-04 10:52:59 -0800269
270 // the addition of a bucket may actually change the neighborset
271 // update the global store
272 /*
273 Set<DeviceId> neighbors = new HashSet<DeviceId>(ns.getDeviceIds());
274 boolean newadd = neighbors.add(newLink.dst().deviceId());
275 if (newadd) {
276 NeighborSet nsnew = new NeighborSet(neighbors, ns.getEdgeLabel());
277 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, nsnew),
278 nextId);
279 nsNextObjStore.remove(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
280 }*/
Saurav Das4ce45962015-11-24 23:21:05 -0800281 } else if (isMaster) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800282 log.warn("linkUp in device {}, but global store has no record "
283 + "for neighbor-set {}", deviceId, ns);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700284 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800285 }
286 }
287
288 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700289 * Performs group recovery procedures when a port goes down on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800290 *
291 * @param port port number that has gone down
Charles Chane849c192016-01-11 18:28:54 -0800292 * @param isMaster true if local instance is the master
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800293 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800294 public void portDown(PortNumber port, boolean isMaster) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800295 if (portDeviceMap.get(port) == null) {
296 log.warn("portDown: unknown port");
297 return;
298 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800299
Saurav Das80980c72016-03-23 11:22:49 -0700300 @SuppressWarnings("unused")
Charles Chan0b4e6182015-11-03 10:42:14 -0800301 MacAddress dstMac;
302 try {
303 dstMac = deviceConfig.getDeviceMac(portDeviceMap.get(port));
304 } catch (DeviceConfigNotFoundException e) {
305 log.warn(e.getMessage() + " Aborting portDown.");
306 return;
307 }
308
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700309 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
310 portDeviceMap.get(port));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700311 /*Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(portDeviceMap
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700312 .get(port),
313 devicePortMap
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700314 .keySet());*/
315 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
316 .stream()
317 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
318 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
319 .filter((ns) -> (ns.getDeviceIds()
320 .contains(portDeviceMap.get(port))))
321 .collect(Collectors.toSet());
Saurav Das423fe2b2015-12-04 10:52:59 -0800322 log.debug("portDown: nsNextObjStore contents for device {}:{}",
323 deviceId, nsSet);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800324 for (NeighborSet ns : nsSet) {
Saurav Das80980c72016-03-23 11:22:49 -0700325 NeighborSetNextObjectiveStoreKey nsStoreKey =
326 new NeighborSetNextObjectiveStoreKey(deviceId, ns);
327 Integer nextId = nsNextObjStore.get(nsStoreKey);
Saurav Das423fe2b2015-12-04 10:52:59 -0800328 if (nextId != null && isMaster) {
Saurav Das80980c72016-03-23 11:22:49 -0700329 // XXX This is a workaround for BUG (CORD-611) in current switches.
330 // Should be temporary because this workaround prevents correct
331 // functionality in LAG recovery.
332 log.info("**portDown port:{} in device {}: Invalidating nextId {}",
333 port, deviceId, nextId);
334 nsNextObjStore.remove(nsStoreKey);
335 /*
Saurav Das8a0732e2015-11-20 15:27:53 -0800336 log.info("**portDown in device {}: Removing Bucket "
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700337 + "with Port {} to next object id {}",
338 deviceId,
339 port,
340 nextId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800341 // Create the bucket to be removed
342 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
343 .builder();
344 tBuilder.setOutput(port)
345 .setEthDst(dstMac)
346 .setEthSrc(nodeMacAddr);
347 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
348 tBuilder.pushMpls()
349 .copyTtlOut()
350 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
351 }
352 NextObjective.Builder nextObjBuilder = DefaultNextObjective
353 .builder()
354 .withType(NextObjective.Type.HASHED) //same as original
355 .withId(nextId)
356 .fromApp(appId)
357 .addTreatment(tBuilder.build());
358 NextObjective nextObjective = nextObjBuilder.
359 removeFromExisting(new SRNextObjectiveContext(deviceId));
sangho834e4b02015-05-01 09:38:25 -0700360
Saurav Das423fe2b2015-12-04 10:52:59 -0800361 flowObjectiveService.next(deviceId, nextObjective);
Saurav Das80980c72016-03-23 11:22:49 -0700362 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800363 // the removal of a bucket may actually change the neighborset
364 // update the global store
365 /*
366 Set<DeviceId> neighbors = new HashSet<DeviceId>(ns.getDeviceIds());
367 boolean removed = neighbors.remove(portDeviceMap.get(port));
368 if (removed) {
369 NeighborSet nsnew = new NeighborSet(neighbors, ns.getEdgeLabel());
370 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, nsnew),
371 nextId);
372 nsNextObjStore.remove(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
373 }*/
sangho834e4b02015-05-01 09:38:25 -0700374 }
375
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800376 }
377
378 devicePortMap.get(portDeviceMap.get(port)).remove(port);
379 portDeviceMap.remove(port);
380 }
381
382 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800383 * Returns the next objective of type hashed associated with the neighborset.
384 * If there is no next objective for this neighborset, this method
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 * would create a next objective and return. Optionally metadata can be
386 * passed in for the creation of the next objective.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800387 *
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700388 * @param ns neighborset
Saurav Das8a0732e2015-11-20 15:27:53 -0800389 * @param meta metadata passed into the creation of a Next Objective
390 * @return int if found or -1 if there are errors in the creation of the
391 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800392 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 public int getNextObjectiveId(NeighborSet ns, TrafficSelector meta) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700394 Integer nextId = nsNextObjStore.
395 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700396 if (nextId == null) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700397 log.trace("getNextObjectiveId in device{}: Next objective id "
398 + "not found for {} and creating", deviceId, ns);
399 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
400 deviceId,
401 nsNextObjStore.entrySet()
402 .stream()
403 .filter((nsStoreEntry) ->
404 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
405 .collect(Collectors.toList()));
Saurav Das8a0732e2015-11-20 15:27:53 -0800406 createGroupsFromNeighborsets(Collections.singleton(ns), meta);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700407 nextId = nsNextObjStore.
408 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700409 if (nextId == null) {
410 log.warn("getNextObjectiveId: unable to create next objective");
411 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700412 } else {
413 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700414 + "created for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700415 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700416 } else {
417 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700418 + "found for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700419 }
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700420 return nextId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800421 }
422
sangho0b2b6d12015-05-20 22:16:38 -0700423 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800424 * Returns the next objective of type broadcast associated with the subnet,
425 * or -1 if no such objective exists. Note that this method does NOT create
426 * the next objective as a side-effect. It is expected that is objective is
427 * created at startup from network configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700428 *
429 * @param prefix subnet information
430 * @return int if found or -1
431 */
432 public int getSubnetNextObjectiveId(IpPrefix prefix) {
433 Integer nextId = subnetNextObjStore.
434 get(new SubnetNextObjectiveStoreKey(deviceId, prefix));
Charles Chan9f676b62015-10-29 14:58:10 -0700435
436 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700437 }
438
439 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800440 * Returns the next objective of type simple associated with the port on the
441 * device, given the treatment. Different treatments to the same port result
442 * in different next objectives. If no such objective exists, this method
443 * creates one and returns the id. Optionally metadata can be passed in for
444 * the creation of the objective.
445 *
446 * @param portNum the port number for the simple next objective
447 * @param treatment the actions to apply on the packets (should include outport)
448 * @param meta optional metadata passed into the creation of the next objective
449 * @return int if found or created, -1 if there are errors during the
450 * creation of the next objective.
451 */
452 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
453 TrafficSelector meta) {
Charles Chane849c192016-01-11 18:28:54 -0800454 Integer nextId = portNextObjStore
455 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
Saurav Das4ce45962015-11-24 23:21:05 -0800456 if (nextId == null) {
457 log.trace("getPortNextObjectiveId in device{}: Next objective id "
458 + "not found for {} and {} creating", deviceId, portNum);
459 createGroupFromPort(portNum, treatment, meta);
460 nextId = portNextObjStore.get(
461 new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
462 if (nextId == null) {
463 log.warn("getPortNextObjectiveId: unable to create next obj"
Charles Chane849c192016-01-11 18:28:54 -0800464 + "for dev:{} port:{}", deviceId, portNum);
465 return -1;
466 }
467 }
468 return nextId;
469 }
470
471 /**
472 * Returns the next objective ID of type broadcast associated with the VLAN
473 * cross-connection.
474 *
475 * @param vlanId VLAN ID for the cross-connection
476 * @return int if found or created, -1 if there are errors during the
477 * creation of the next objective
478 */
479 public int getXConnectNextObjectiveId(VlanId vlanId) {
480 Integer nextId = xConnectNextObjStore
481 .get(new XConnectNextObjectiveStoreKey(deviceId, vlanId));
482 if (nextId == null) {
483 log.trace("getXConnectNextObjectiveId: Next objective id "
484 + "not found for device {} and vlan {}. Creating", deviceId, vlanId);
485 createGroupsForXConnect(deviceId);
486 nextId = xConnectNextObjStore.get(
487 new XConnectNextObjectiveStoreKey(deviceId, vlanId));
488 if (nextId == null) {
489 log.warn("getXConnectNextObjectiveId: Next objective id "
490 + "not found for device {} and vlan {}.", deviceId, vlanId);
Saurav Das4ce45962015-11-24 23:21:05 -0800491 return -1;
492 }
493 }
494 return nextId;
495 }
496
497 /**
sangho0b2b6d12015-05-20 22:16:38 -0700498 * Checks if the next objective ID (group) for the neighbor set exists or not.
499 *
500 * @param ns neighbor set to check
501 * @return true if it exists, false otherwise
502 */
503 public boolean hasNextObjectiveId(NeighborSet ns) {
504 Integer nextId = nsNextObjStore.
505 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
506 if (nextId == null) {
507 return false;
508 }
509
510 return true;
511 }
512
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700513 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800514 protected void newNeighbor(Link newLink) {
515 }
516
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700517 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800518 protected void newPortToExistingNeighbor(Link newLink) {
519 }
520
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700521 // Empty implementation
522 protected Set<NeighborSet>
523 computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor,
524 Set<DeviceId> updatedNeighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800525 return null;
526 }
527
528 private void populateNeighborMaps() {
529 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700530 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800531 if (link.type() != Link.Type.DIRECT) {
532 continue;
533 }
534 addNeighborAtPort(link.dst().deviceId(), link.src().port());
535 }
536 }
537
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700538 protected void addNeighborAtPort(DeviceId neighborId,
539 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800540 // Update DeviceToPort database
541 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
542 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800543 Set<PortNumber> ports = Collections
544 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
545 ports.add(portToNeighbor);
546 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
547 if (portnums != null) {
548 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800549 }
550
551 // Update portToDevice database
Saurav Das8a0732e2015-11-20 15:27:53 -0800552 DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId);
553 if (prev != null) {
554 log.warn("Device: {} port: {} has neighbor: {}. NOT updating "
555 + "to neighbor: {}", deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800556 }
557 }
558
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700559 protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700560 List<DeviceId> list = new ArrayList<>(neighbors);
561 Set<Set<DeviceId>> sets = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800562 // get the number of elements in the neighbors
563 int elements = list.size();
564 // the number of members of a power set is 2^n
565 // including the empty set
566 int powerElements = (1 << elements);
567
568 // run a binary counter for the number of power elements
569 // NOTE: Exclude empty set
570 for (long i = 1; i < powerElements; i++) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700571 Set<DeviceId> neighborSubSet = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800572 for (int j = 0; j < elements; j++) {
573 if ((i >> j) % 2 == 1) {
574 neighborSubSet.add(list.get(j));
575 }
576 }
577 sets.add(neighborSubSet);
578 }
579 return sets;
580 }
581
582 private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800583 int segmentId;
584 try {
585 segmentId = deviceConfig.getSegmentId(deviceId);
586 } catch (DeviceConfigNotFoundException e) {
587 log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId.");
588 return false;
589 }
590
591 return segmentId == sId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800592 }
593
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700594 protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800595
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700596 List<Integer> nsSegmentIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800597
sanghob35a6192015-04-01 13:05:26 -0700598 // Always pair up with no edge label
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700599 // If (neighbors.size() == 1) {
sanghob35a6192015-04-01 13:05:26 -0700600 nsSegmentIds.add(-1);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700601 // }
sanghob35a6192015-04-01 13:05:26 -0700602
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800603 // Filter out SegmentIds matching with the
604 // nodes in the combo
605 for (Integer sId : allSegmentIds) {
606 if (sId.equals(nodeSegmentId)) {
607 continue;
608 }
609 boolean filterOut = false;
610 // Check if the edge label being set is of
611 // any node in the Neighbor set
612 for (DeviceId deviceId : neighbors) {
613 if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) {
614 filterOut = true;
615 break;
616 }
617 }
618 if (!filterOut) {
619 nsSegmentIds.add(sId);
620 }
621 }
622 return nsSegmentIds;
623 }
624
sangho1e575652015-05-14 00:39:53 -0700625 /**
626 * Creates Groups from a set of NeighborSet given.
627 *
628 * @param nsSet a set of NeighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800629 * @param meta metadata passed into the creation of a Next Objective
sangho1e575652015-05-14 00:39:53 -0700630 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800631 public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet,
632 TrafficSelector meta) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800633 for (NeighborSet ns : nsSet) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700634 int nextId = flowObjectiveService.allocateNextId();
635 NextObjective.Builder nextObjBuilder = DefaultNextObjective
636 .builder().withId(nextId)
637 .withType(NextObjective.Type.HASHED).fromApp(appId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800638 for (DeviceId neighborId : ns.getDeviceIds()) {
639 if (devicePortMap.get(neighborId) == null) {
640 log.warn("Neighbor {} is not in the port map yet for dev:{}",
641 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700642 return;
Saurav Das8a0732e2015-11-20 15:27:53 -0800643 } else if (devicePortMap.get(neighborId).size() == 0) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700644 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800645 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700646 return;
sangho834e4b02015-05-01 09:38:25 -0700647 }
648
Saurav Das8a0732e2015-11-20 15:27:53 -0800649 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800650 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800651 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800652 } catch (DeviceConfigNotFoundException e) {
653 log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets.");
654 return;
655 }
656
Saurav Das8a0732e2015-11-20 15:27:53 -0800657 for (PortNumber sp : devicePortMap.get(neighborId)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700658 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
659 .builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800660 tBuilder.setEthDst(neighborMac)
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700661 .setEthSrc(nodeMacAddr);
662 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800663 tBuilder.pushMpls()
664 .copyTtlOut()
665 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700666 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800667 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700668 nextObjBuilder.addTreatment(tBuilder.build());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800669 }
670 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800671 if (meta != null) {
Saurav Das4ce45962015-11-24 23:21:05 -0800672 nextObjBuilder.withMeta(meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800673 }
Charles Chan216e3c82016-04-23 14:48:16 -0700674
675 ObjectiveContext context = new DefaultObjectiveContext(
676 (objective) -> log.debug("createGroupsFromNeighborsets installed NextObj {} on {}",
677 nextId, deviceId),
678 (objective, error) ->
679 log.warn("createGroupsFromNeighborsets failed to install NextObj {} on {}: {}",
680 nextId, deviceId, error));
681 NextObjective nextObj = nextObjBuilder.add(context);
682 log.debug("**createGroupsFromNeighborsets: Submited "
Saurav Das8a0732e2015-11-20 15:27:53 -0800683 + "next objective {} in device {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700684 nextId, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800685 flowObjectiveService.next(deviceId, nextObj);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700686 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns),
687 nextId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800688 }
689 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700690
Saurav Das4ce45962015-11-24 23:21:05 -0800691 /**
692 * Creates broadcast groups for all ports in the same configured subnet.
Saurav Das4ce45962015-11-24 23:21:05 -0800693 */
Charles Chanc42e84e2015-10-20 16:24:19 -0700694 public void createGroupsFromSubnetConfig() {
Saurav Das7a1ffca2016-03-28 19:00:18 -0700695 Map<Ip4Prefix, List<PortNumber>> subnetPortMap;
696 try {
697 subnetPortMap = this.deviceConfig.getSubnetPortsMap(this.deviceId);
698 } catch (DeviceConfigNotFoundException e) {
699 log.warn(e.getMessage()
700 + " Not creating broadcast groups for device: " + deviceId);
701 return;
702 }
Charles Chanc42e84e2015-10-20 16:24:19 -0700703 // Construct a broadcast group for each subnet
704 subnetPortMap.forEach((subnet, ports) -> {
Charles Chan9f676b62015-10-29 14:58:10 -0700705 SubnetNextObjectiveStoreKey key =
706 new SubnetNextObjectiveStoreKey(deviceId, subnet);
707
708 if (subnetNextObjStore.containsKey(key)) {
709 log.debug("Broadcast group for device {} and subnet {} exists",
710 deviceId, subnet);
711 return;
712 }
713
Charles Chan188ebf52015-12-23 00:15:11 -0800714 VlanId assignedVlanId =
715 srManager.getSubnetAssignedVlanId(this.deviceId, subnet);
716 TrafficSelector metadata =
717 DefaultTrafficSelector.builder().matchVlanId(assignedVlanId).build();
718
Charles Chanc42e84e2015-10-20 16:24:19 -0700719 int nextId = flowObjectiveService.allocateNextId();
720
721 NextObjective.Builder nextObjBuilder = DefaultNextObjective
722 .builder().withId(nextId)
Charles Chan188ebf52015-12-23 00:15:11 -0800723 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
724 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -0700725
726 ports.forEach(port -> {
727 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Das4f980082015-11-05 13:39:15 -0800728 tBuilder.popVlan();
Charles Chanc42e84e2015-10-20 16:24:19 -0700729 tBuilder.setOutput(port);
730 nextObjBuilder.addTreatment(tBuilder.build());
731 });
732
733 NextObjective nextObj = nextObjBuilder.add();
734 flowObjectiveService.next(deviceId, nextObj);
735 log.debug("createGroupFromSubnetConfig: Submited "
736 + "next objective {} in device {}",
737 nextId, deviceId);
Charles Chan9f676b62015-10-29 14:58:10 -0700738
Charles Chanc42e84e2015-10-20 16:24:19 -0700739 subnetNextObjStore.put(key, nextId);
740 });
741 }
742
Charles Chane849c192016-01-11 18:28:54 -0800743 /**
744 * Creates broadcast groups for VLAN cross-connect ports.
745 *
746 * @param deviceId the DPID of the switch
747 */
748 public void createGroupsForXConnect(DeviceId deviceId) {
749 Map<VlanId, List<ConnectPoint>> xConnectsForDevice = deviceConfig.getXConnects();
750
751 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
752 // Only proceed the xConnect for given device
753 for (ConnectPoint connectPoint : connectPoints) {
754 if (!connectPoint.deviceId().equals(deviceId)) {
755 return;
756 }
757 }
758
759 // Check if the next obj is already in the store
760 XConnectNextObjectiveStoreKey key =
761 new XConnectNextObjectiveStoreKey(deviceId, vlanId);
762 if (xConnectNextObjStore.containsKey(key)) {
763 log.debug("Cross-connect Broadcast group for device {} and vlanId {} exists",
764 deviceId, vlanId);
765 return;
766 }
767
768 TrafficSelector metadata =
769 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
770 int nextId = flowObjectiveService.allocateNextId();
771
772 NextObjective.Builder nextObjBuilder = DefaultNextObjective
773 .builder().withId(nextId)
774 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
775 .withMeta(metadata);
776
777 connectPoints.forEach(connectPoint -> {
778 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
779 tBuilder.setOutput(connectPoint.port());
780 nextObjBuilder.addTreatment(tBuilder.build());
781 });
782
783 NextObjective nextObj = nextObjBuilder.add();
784 flowObjectiveService.next(deviceId, nextObj);
785 log.debug("createGroupsForXConnect: Submited next objective {} in device {}",
786 nextId, deviceId);
787 xConnectNextObjStore.put(key, nextId);
788 });
789 }
790
Saurav Das4ce45962015-11-24 23:21:05 -0800791
792 /**
793 * Create simple next objective for a single port. The treatments can include
794 * all outgoing actions that need to happen on the packet.
795 *
796 * @param portNum the outgoing port on the device
797 * @param treatment the actions to apply on the packets (should include outport)
798 * @param meta optional data to pass to the driver
799 */
800 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
801 TrafficSelector meta) {
802 int nextId = flowObjectiveService.allocateNextId();
803 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
804 deviceId, portNum, treatment);
805
806 NextObjective.Builder nextObjBuilder = DefaultNextObjective
807 .builder().withId(nextId)
808 .withType(NextObjective.Type.SIMPLE)
809 .addTreatment(treatment)
810 .fromApp(appId)
811 .withMeta(meta);
812
813 NextObjective nextObj = nextObjBuilder.add();
814 flowObjectiveService.next(deviceId, nextObj);
815 log.debug("createGroupFromPort: Submited next objective {} in device {} "
816 + "for port {}", nextId, deviceId, portNum);
817
818 portNextObjStore.put(key, nextId);
819 }
820
sangho1e575652015-05-14 00:39:53 -0700821 /**
822 * Removes groups for the next objective ID given.
823 *
824 * @param objectiveId next objective ID to remove
825 * @return true if succeeds, false otherwise
826 */
827 public boolean removeGroup(int objectiveId) {
828
829 if (nsNextObjStore.containsValue(objectiveId)) {
830 NextObjective.Builder nextObjBuilder = DefaultNextObjective
831 .builder().withId(objectiveId)
832 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -0700833 ObjectiveContext context = new DefaultObjectiveContext(
834 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
835 objectiveId, deviceId),
836 (objective, error) ->
837 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
838 objectiveId, deviceId, error));
839 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -0800840 log.info("**removeGroup: Submited "
841 + "next objective {} in device {}",
842 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -0700843 flowObjectiveService.next(deviceId, nextObjective);
844
845 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) {
846 if (entry.getValue().equals(objectiveId)) {
847 nsNextObjStore.remove(entry.getKey());
848 break;
849 }
850 }
sangho0b2b6d12015-05-20 22:16:38 -0700851 return true;
sangho1e575652015-05-14 00:39:53 -0700852 }
853
854 return false;
855 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700856
Charles Chane849c192016-01-11 18:28:54 -0800857 /**
858 * Removes all groups from all next objective stores.
859 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800860 public void removeAllGroups() {
861 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry:
862 nsNextObjStore.entrySet()) {
863 removeGroup(entry.getValue());
864 }
865 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
866 portNextObjStore.entrySet()) {
867 removeGroup(entry.getValue());
868 }
869 for (Map.Entry<SubnetNextObjectiveStoreKey, Integer> entry:
870 subnetNextObjStore.entrySet()) {
871 removeGroup(entry.getValue());
872 }
873 // should probably clean local stores port-neighbor
874 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800875}