blob: 4e64c4b77d94b0cdbd5c7aa7284461e19aa7bec4 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Srikanth 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;
47import org.onosproject.net.flowobjective.FlowObjectiveService;
48import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070049import org.onosproject.net.flowobjective.Objective;
50import org.onosproject.net.flowobjective.ObjectiveContext;
51import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080052import org.onosproject.net.link.LinkService;
Saurav Das423fe2b2015-12-04 10:52:59 -080053import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan0b4e6182015-11-03 10:42:14 -080054import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
55import org.onosproject.segmentrouting.config.DeviceProperties;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070056import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080057import org.slf4j.Logger;
58
59/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070060 * Default ECMP group handler creation module. This component creates a set of
61 * ECMP groups for every neighbor that this device is connected to based on
62 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080063 */
64public class DefaultGroupHandler {
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070065 protected static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080066
67 protected final DeviceId deviceId;
68 protected final ApplicationId appId;
69 protected final DeviceProperties deviceConfig;
70 protected final List<Integer> allSegmentIds;
Charles Chan0b4e6182015-11-03 10:42:14 -080071 protected int nodeSegmentId = -1;
72 protected boolean isEdgeRouter = false;
73 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080074 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070075 protected FlowObjectiveService flowObjectiveService;
Saurav Das423fe2b2015-12-04 10:52:59 -080076 // local store for neighbor-device-ids and the set of ports on this device
77 // that connect to the same neighbor
Saurav Das8a0732e2015-11-20 15:27:53 -080078 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
79 new ConcurrentHashMap<>();
Saurav Das423fe2b2015-12-04 10:52:59 -080080 //local store for ports on this device connected to neighbor-device-id
Saurav Das8a0732e2015-11-20 15:27:53 -080081 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
82 new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -080083 protected EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
84 nsNextObjStore = null;
85 protected EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
86 subnetNextObjStore = null;
87 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
88 portNextObjStore = null;
89 protected EventuallyConsistentMap<XConnectNextObjectiveStoreKey, Integer>
90 xConnectNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -080091 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080092
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070093 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070094 .register(URI.class).register(HashSet.class)
95 .register(DeviceId.class).register(PortNumber.class)
96 .register(NeighborSet.class).register(PolicyGroupIdentifier.class)
97 .register(PolicyGroupParams.class)
98 .register(GroupBucketIdentifier.class)
99 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800100
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700101 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
102 DeviceProperties config,
103 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700104 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800105 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800106 this.deviceId = checkNotNull(deviceId);
107 this.appId = checkNotNull(appId);
108 this.deviceConfig = checkNotNull(config);
109 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800110 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
111 try {
112 this.nodeSegmentId = config.getSegmentId(deviceId);
113 this.isEdgeRouter = config.isEdgeDevice(deviceId);
114 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
115 } catch (DeviceConfigNotFoundException e) {
116 log.warn(e.getMessage()
117 + " Skipping value assignment in DefaultGroupHandler");
118 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700119 this.flowObjectiveService = flowObjService;
Charles Chane849c192016-01-11 18:28:54 -0800120 this.nsNextObjStore = srManager.nsNextObjStore;
121 this.subnetNextObjStore = srManager.subnetNextObjStore;
122 this.portNextObjStore = srManager.portNextObjStore;
123 this.xConnectNextObjStore = srManager.xConnectNextObjStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800124 this.srManager = srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800125
126 populateNeighborMaps();
127 }
128
129 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700130 * Creates a group handler object based on the type of device. If device is
131 * of edge type it returns edge group handler, else it returns transit group
132 * handler.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800133 *
134 * @param deviceId device identifier
135 * @param appId application identifier
136 * @param config interface to retrieve the device properties
137 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700138 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800139 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800140 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800141 * @return default group handler type
142 */
Saurav Das4ce45962015-11-24 23:21:05 -0800143 public static DefaultGroupHandler createGroupHandler(
144 DeviceId deviceId,
145 ApplicationId appId,
146 DeviceProperties config,
147 LinkService linkService,
148 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800149 SegmentRoutingManager srManager)
Saurav Das4ce45962015-11-24 23:21:05 -0800150 throws DeviceConfigNotFoundException {
Charles Chan0b4e6182015-11-03 10:42:14 -0800151 // handle possible exception in the caller
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800152 if (config.isEdgeDevice(deviceId)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700153 return new DefaultEdgeGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700154 linkService,
155 flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800156 srManager
157 );
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800158 } else {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700159 return new DefaultTransitGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700160 linkService,
161 flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800162 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800163 }
164 }
165
166 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700167 * Creates the auto created groups for this device based on the current
168 * snapshot of the topology.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800169 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700170 // Empty implementations to be overridden by derived classes
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800171 public void createGroups() {
172 }
173
174 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700175 * Performs group creation or update procedures when a new link is
176 * discovered on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800177 *
178 * @param newLink new neighbor link
Charles Chane849c192016-01-11 18:28:54 -0800179 * @param isMaster true if local instance is the master
180 *
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800181 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800182 public void linkUp(Link newLink, boolean isMaster) {
sanghob35a6192015-04-01 13:05:26 -0700183
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800184 if (newLink.type() != Link.Type.DIRECT) {
185 log.warn("linkUp: unknown link type");
186 return;
187 }
188
189 if (!newLink.src().deviceId().equals(deviceId)) {
190 log.warn("linkUp: deviceId{} doesn't match with link src{}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700191 deviceId, newLink.src().deviceId());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800192 return;
193 }
194
Saurav Das8a0732e2015-11-20 15:27:53 -0800195 log.info("* LinkUP: Device {} linkUp at local port {} to neighbor {}", deviceId,
196 newLink.src().port(), newLink.dst().deviceId());
Charles Chan0b4e6182015-11-03 10:42:14 -0800197 MacAddress dstMac;
198 try {
199 dstMac = deviceConfig.getDeviceMac(newLink.dst().deviceId());
200 } catch (DeviceConfigNotFoundException e) {
201 log.warn(e.getMessage() + " Aborting linkUp.");
202 return;
203 }
204
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700205 addNeighborAtPort(newLink.dst().deviceId(),
206 newLink.src().port());
207 /*if (devicePortMap.get(newLink.dst().deviceId()) == null) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800208 // New Neighbor
209 newNeighbor(newLink);
210 } else {
211 // Old Neighbor
212 newPortToExistingNeighbor(newLink);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700213 }*/
214 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
215 .stream()
216 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
217 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
218 .filter((ns) -> (ns.getDeviceIds()
219 .contains(newLink.dst().deviceId())))
220 .collect(Collectors.toSet());
221 log.trace("linkUp: nsNextObjStore contents for device {}:",
sangho0b2b6d12015-05-20 22:16:38 -0700222 deviceId,
223 nsSet);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700224 for (NeighborSet ns : nsSet) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700225 Integer nextId = nsNextObjStore.
226 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Saurav Das4ce45962015-11-24 23:21:05 -0800227 if (nextId != null && isMaster) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800228 // Create the new bucket to be updated
229 TrafficTreatment.Builder tBuilder =
230 DefaultTrafficTreatment.builder();
231 tBuilder.setOutput(newLink.src().port())
232 .setEthDst(dstMac)
233 .setEthSrc(nodeMacAddr);
234 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
235 tBuilder.pushMpls()
236 .copyTtlOut()
237 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
238 }
239 // setup metadata to pass to nextObjective - indicate the vlan on egress
240 // if needed by the switch pipeline. Since hashed next-hops are always to
241 // other neighboring routers, there is no subnet assigned on those ports.
242 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
243 metabuilder.matchVlanId(
244 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700245
Saurav Das423fe2b2015-12-04 10:52:59 -0800246 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
247 .withId(nextId)
248 .withType(NextObjective.Type.HASHED)
249 .addTreatment(tBuilder.build())
250 .withMeta(metabuilder.build())
251 .fromApp(appId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800252 log.info("**linkUp in device {}: Adding Bucket "
Saurav Das4ce45962015-11-24 23:21:05 -0800253 + "with Port {} to next object id {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700254 deviceId,
255 newLink.src().port(),
Saurav Das4ce45962015-11-24 23:21:05 -0800256 nextId);
257 NextObjective nextObjective = nextObjBuilder.
258 addToExisting(new SRNextObjectiveContext(deviceId));
259 flowObjectiveService.next(deviceId, nextObjective);
Saurav Das423fe2b2015-12-04 10:52:59 -0800260
261 // the addition of a bucket may actually change the neighborset
262 // update the global store
263 /*
264 Set<DeviceId> neighbors = new HashSet<DeviceId>(ns.getDeviceIds());
265 boolean newadd = neighbors.add(newLink.dst().deviceId());
266 if (newadd) {
267 NeighborSet nsnew = new NeighborSet(neighbors, ns.getEdgeLabel());
268 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, nsnew),
269 nextId);
270 nsNextObjStore.remove(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
271 }*/
Saurav Das4ce45962015-11-24 23:21:05 -0800272 } else if (isMaster) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800273 log.warn("linkUp in device {}, but global store has no record "
274 + "for neighbor-set {}", deviceId, ns);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700275 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800276 }
277 }
278
279 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700280 * Performs group recovery procedures when a port goes down on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800281 *
282 * @param port port number that has gone down
Charles Chane849c192016-01-11 18:28:54 -0800283 * @param isMaster true if local instance is the master
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800284 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800285 public void portDown(PortNumber port, boolean isMaster) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800286 if (portDeviceMap.get(port) == null) {
287 log.warn("portDown: unknown port");
288 return;
289 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800290
291 MacAddress dstMac;
292 try {
293 dstMac = deviceConfig.getDeviceMac(portDeviceMap.get(port));
294 } catch (DeviceConfigNotFoundException e) {
295 log.warn(e.getMessage() + " Aborting portDown.");
296 return;
297 }
298
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700299 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
300 portDeviceMap.get(port));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700301 /*Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(portDeviceMap
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700302 .get(port),
303 devicePortMap
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700304 .keySet());*/
305 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
306 .stream()
307 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
308 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
309 .filter((ns) -> (ns.getDeviceIds()
310 .contains(portDeviceMap.get(port))))
311 .collect(Collectors.toSet());
Saurav Das423fe2b2015-12-04 10:52:59 -0800312 log.debug("portDown: nsNextObjStore contents for device {}:{}",
313 deviceId, nsSet);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800314 for (NeighborSet ns : nsSet) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700315 Integer nextId = nsNextObjStore.
316 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Saurav Das423fe2b2015-12-04 10:52:59 -0800317 if (nextId != null && isMaster) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800318 log.info("**portDown in device {}: Removing Bucket "
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700319 + "with Port {} to next object id {}",
320 deviceId,
321 port,
322 nextId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800323 // Create the bucket to be removed
324 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
325 .builder();
326 tBuilder.setOutput(port)
327 .setEthDst(dstMac)
328 .setEthSrc(nodeMacAddr);
329 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
330 tBuilder.pushMpls()
331 .copyTtlOut()
332 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
333 }
334 NextObjective.Builder nextObjBuilder = DefaultNextObjective
335 .builder()
336 .withType(NextObjective.Type.HASHED) //same as original
337 .withId(nextId)
338 .fromApp(appId)
339 .addTreatment(tBuilder.build());
340 NextObjective nextObjective = nextObjBuilder.
341 removeFromExisting(new SRNextObjectiveContext(deviceId));
sangho834e4b02015-05-01 09:38:25 -0700342
Saurav Das423fe2b2015-12-04 10:52:59 -0800343 flowObjectiveService.next(deviceId, nextObjective);
344
345 // the removal of a bucket may actually change the neighborset
346 // update the global store
347 /*
348 Set<DeviceId> neighbors = new HashSet<DeviceId>(ns.getDeviceIds());
349 boolean removed = neighbors.remove(portDeviceMap.get(port));
350 if (removed) {
351 NeighborSet nsnew = new NeighborSet(neighbors, ns.getEdgeLabel());
352 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, nsnew),
353 nextId);
354 nsNextObjStore.remove(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
355 }*/
sangho834e4b02015-05-01 09:38:25 -0700356 }
357
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800358 }
359
360 devicePortMap.get(portDeviceMap.get(port)).remove(port);
361 portDeviceMap.remove(port);
362 }
363
364 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800365 * Returns the next objective of type hashed associated with the neighborset.
366 * If there is no next objective for this neighborset, this method
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 * would create a next objective and return. Optionally metadata can be
368 * passed in for the creation of the next objective.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800369 *
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700370 * @param ns neighborset
Saurav Das8a0732e2015-11-20 15:27:53 -0800371 * @param meta metadata passed into the creation of a Next Objective
372 * @return int if found or -1 if there are errors in the creation of the
373 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800374 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800375 public int getNextObjectiveId(NeighborSet ns, TrafficSelector meta) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700376 Integer nextId = nsNextObjStore.
377 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700378 if (nextId == null) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700379 log.trace("getNextObjectiveId in device{}: Next objective id "
380 + "not found for {} and creating", deviceId, ns);
381 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
382 deviceId,
383 nsNextObjStore.entrySet()
384 .stream()
385 .filter((nsStoreEntry) ->
386 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
387 .collect(Collectors.toList()));
Saurav Das8a0732e2015-11-20 15:27:53 -0800388 createGroupsFromNeighborsets(Collections.singleton(ns), meta);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700389 nextId = nsNextObjStore.
390 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700391 if (nextId == null) {
392 log.warn("getNextObjectiveId: unable to create next objective");
393 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700394 } else {
395 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700396 + "created for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700397 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700398 } else {
399 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700400 + "found for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700401 }
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700402 return nextId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800403 }
404
sangho0b2b6d12015-05-20 22:16:38 -0700405 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800406 * Returns the next objective of type broadcast associated with the subnet,
407 * or -1 if no such objective exists. Note that this method does NOT create
408 * the next objective as a side-effect. It is expected that is objective is
409 * created at startup from network configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700410 *
411 * @param prefix subnet information
412 * @return int if found or -1
413 */
414 public int getSubnetNextObjectiveId(IpPrefix prefix) {
415 Integer nextId = subnetNextObjStore.
416 get(new SubnetNextObjectiveStoreKey(deviceId, prefix));
Charles Chan9f676b62015-10-29 14:58:10 -0700417
418 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700419 }
420
421 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800422 * Returns the next objective of type simple associated with the port on the
423 * device, given the treatment. Different treatments to the same port result
424 * in different next objectives. If no such objective exists, this method
425 * creates one and returns the id. Optionally metadata can be passed in for
426 * the creation of the objective.
427 *
428 * @param portNum the port number for the simple next objective
429 * @param treatment the actions to apply on the packets (should include outport)
430 * @param meta optional metadata passed into the creation of the next objective
431 * @return int if found or created, -1 if there are errors during the
432 * creation of the next objective.
433 */
434 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
435 TrafficSelector meta) {
Charles Chane849c192016-01-11 18:28:54 -0800436 Integer nextId = portNextObjStore
437 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
Saurav Das4ce45962015-11-24 23:21:05 -0800438 if (nextId == null) {
439 log.trace("getPortNextObjectiveId in device{}: Next objective id "
440 + "not found for {} and {} creating", deviceId, portNum);
441 createGroupFromPort(portNum, treatment, meta);
442 nextId = portNextObjStore.get(
443 new PortNextObjectiveStoreKey(deviceId, portNum, treatment));
444 if (nextId == null) {
445 log.warn("getPortNextObjectiveId: unable to create next obj"
Charles Chane849c192016-01-11 18:28:54 -0800446 + "for dev:{} port:{}", deviceId, portNum);
447 return -1;
448 }
449 }
450 return nextId;
451 }
452
453 /**
454 * Returns the next objective ID of type broadcast associated with the VLAN
455 * cross-connection.
456 *
457 * @param vlanId VLAN ID for the cross-connection
458 * @return int if found or created, -1 if there are errors during the
459 * creation of the next objective
460 */
461 public int getXConnectNextObjectiveId(VlanId vlanId) {
462 Integer nextId = xConnectNextObjStore
463 .get(new XConnectNextObjectiveStoreKey(deviceId, vlanId));
464 if (nextId == null) {
465 log.trace("getXConnectNextObjectiveId: Next objective id "
466 + "not found for device {} and vlan {}. Creating", deviceId, vlanId);
467 createGroupsForXConnect(deviceId);
468 nextId = xConnectNextObjStore.get(
469 new XConnectNextObjectiveStoreKey(deviceId, vlanId));
470 if (nextId == null) {
471 log.warn("getXConnectNextObjectiveId: Next objective id "
472 + "not found for device {} and vlan {}.", deviceId, vlanId);
Saurav Das4ce45962015-11-24 23:21:05 -0800473 return -1;
474 }
475 }
476 return nextId;
477 }
478
479 /**
sangho0b2b6d12015-05-20 22:16:38 -0700480 * Checks if the next objective ID (group) for the neighbor set exists or not.
481 *
482 * @param ns neighbor set to check
483 * @return true if it exists, false otherwise
484 */
485 public boolean hasNextObjectiveId(NeighborSet ns) {
486 Integer nextId = nsNextObjStore.
487 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
488 if (nextId == null) {
489 return false;
490 }
491
492 return true;
493 }
494
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700495 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800496 protected void newNeighbor(Link newLink) {
497 }
498
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700499 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800500 protected void newPortToExistingNeighbor(Link newLink) {
501 }
502
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700503 // Empty implementation
504 protected Set<NeighborSet>
505 computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor,
506 Set<DeviceId> updatedNeighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800507 return null;
508 }
509
510 private void populateNeighborMaps() {
511 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700512 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800513 if (link.type() != Link.Type.DIRECT) {
514 continue;
515 }
516 addNeighborAtPort(link.dst().deviceId(), link.src().port());
517 }
518 }
519
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700520 protected void addNeighborAtPort(DeviceId neighborId,
521 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800522 // Update DeviceToPort database
523 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
524 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800525 Set<PortNumber> ports = Collections
526 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
527 ports.add(portToNeighbor);
528 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
529 if (portnums != null) {
530 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800531 }
532
533 // Update portToDevice database
Saurav Das8a0732e2015-11-20 15:27:53 -0800534 DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId);
535 if (prev != null) {
536 log.warn("Device: {} port: {} has neighbor: {}. NOT updating "
537 + "to neighbor: {}", deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800538 }
539 }
540
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700541 protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700542 List<DeviceId> list = new ArrayList<>(neighbors);
543 Set<Set<DeviceId>> sets = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800544 // get the number of elements in the neighbors
545 int elements = list.size();
546 // the number of members of a power set is 2^n
547 // including the empty set
548 int powerElements = (1 << elements);
549
550 // run a binary counter for the number of power elements
551 // NOTE: Exclude empty set
552 for (long i = 1; i < powerElements; i++) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700553 Set<DeviceId> neighborSubSet = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800554 for (int j = 0; j < elements; j++) {
555 if ((i >> j) % 2 == 1) {
556 neighborSubSet.add(list.get(j));
557 }
558 }
559 sets.add(neighborSubSet);
560 }
561 return sets;
562 }
563
564 private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800565 int segmentId;
566 try {
567 segmentId = deviceConfig.getSegmentId(deviceId);
568 } catch (DeviceConfigNotFoundException e) {
569 log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId.");
570 return false;
571 }
572
573 return segmentId == sId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800574 }
575
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700576 protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800577
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700578 List<Integer> nsSegmentIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800579
sanghob35a6192015-04-01 13:05:26 -0700580 // Always pair up with no edge label
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700581 // If (neighbors.size() == 1) {
sanghob35a6192015-04-01 13:05:26 -0700582 nsSegmentIds.add(-1);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700583 // }
sanghob35a6192015-04-01 13:05:26 -0700584
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800585 // Filter out SegmentIds matching with the
586 // nodes in the combo
587 for (Integer sId : allSegmentIds) {
588 if (sId.equals(nodeSegmentId)) {
589 continue;
590 }
591 boolean filterOut = false;
592 // Check if the edge label being set is of
593 // any node in the Neighbor set
594 for (DeviceId deviceId : neighbors) {
595 if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) {
596 filterOut = true;
597 break;
598 }
599 }
600 if (!filterOut) {
601 nsSegmentIds.add(sId);
602 }
603 }
604 return nsSegmentIds;
605 }
606
sangho1e575652015-05-14 00:39:53 -0700607 /**
608 * Creates Groups from a set of NeighborSet given.
609 *
610 * @param nsSet a set of NeighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800611 * @param meta metadata passed into the creation of a Next Objective
sangho1e575652015-05-14 00:39:53 -0700612 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800613 public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet,
614 TrafficSelector meta) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800615 for (NeighborSet ns : nsSet) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700616 int nextId = flowObjectiveService.allocateNextId();
617 NextObjective.Builder nextObjBuilder = DefaultNextObjective
618 .builder().withId(nextId)
619 .withType(NextObjective.Type.HASHED).fromApp(appId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800620 for (DeviceId neighborId : ns.getDeviceIds()) {
621 if (devicePortMap.get(neighborId) == null) {
622 log.warn("Neighbor {} is not in the port map yet for dev:{}",
623 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700624 return;
Saurav Das8a0732e2015-11-20 15:27:53 -0800625 } else if (devicePortMap.get(neighborId).size() == 0) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700626 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800627 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700628 return;
sangho834e4b02015-05-01 09:38:25 -0700629 }
630
Saurav Das8a0732e2015-11-20 15:27:53 -0800631 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800632 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800633 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800634 } catch (DeviceConfigNotFoundException e) {
635 log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets.");
636 return;
637 }
638
Saurav Das8a0732e2015-11-20 15:27:53 -0800639 for (PortNumber sp : devicePortMap.get(neighborId)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700640 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
641 .builder();
Saurav Das8a0732e2015-11-20 15:27:53 -0800642 tBuilder.setEthDst(neighborMac)
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700643 .setEthSrc(nodeMacAddr);
644 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800645 tBuilder.pushMpls()
646 .copyTtlOut()
647 .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700648 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800649 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700650 nextObjBuilder.addTreatment(tBuilder.build());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800651 }
652 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800653 if (meta != null) {
Saurav Das4ce45962015-11-24 23:21:05 -0800654 nextObjBuilder.withMeta(meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800655 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700656 NextObjective nextObj = nextObjBuilder.
657 add(new SRNextObjectiveContext(deviceId));
Saurav Das8a0732e2015-11-20 15:27:53 -0800658 log.info("**createGroupsFromNeighborsets: Submited "
659 + "next objective {} in device {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700660 nextId, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800661 flowObjectiveService.next(deviceId, nextObj);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700662 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns),
663 nextId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800664 }
665 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700666
Saurav Das4ce45962015-11-24 23:21:05 -0800667 /**
668 * Creates broadcast groups for all ports in the same configured subnet.
Saurav Das4ce45962015-11-24 23:21:05 -0800669 */
Charles Chanc42e84e2015-10-20 16:24:19 -0700670 public void createGroupsFromSubnetConfig() {
671 Map<Ip4Prefix, List<PortNumber>> subnetPortMap =
672 this.deviceConfig.getSubnetPortsMap(this.deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700673 // Construct a broadcast group for each subnet
674 subnetPortMap.forEach((subnet, ports) -> {
Charles Chan9f676b62015-10-29 14:58:10 -0700675 SubnetNextObjectiveStoreKey key =
676 new SubnetNextObjectiveStoreKey(deviceId, subnet);
677
678 if (subnetNextObjStore.containsKey(key)) {
679 log.debug("Broadcast group for device {} and subnet {} exists",
680 deviceId, subnet);
681 return;
682 }
683
Charles Chan188ebf52015-12-23 00:15:11 -0800684 VlanId assignedVlanId =
685 srManager.getSubnetAssignedVlanId(this.deviceId, subnet);
686 TrafficSelector metadata =
687 DefaultTrafficSelector.builder().matchVlanId(assignedVlanId).build();
688
Charles Chanc42e84e2015-10-20 16:24:19 -0700689 int nextId = flowObjectiveService.allocateNextId();
690
691 NextObjective.Builder nextObjBuilder = DefaultNextObjective
692 .builder().withId(nextId)
Charles Chan188ebf52015-12-23 00:15:11 -0800693 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
694 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -0700695
696 ports.forEach(port -> {
697 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Das4f980082015-11-05 13:39:15 -0800698 tBuilder.popVlan();
Charles Chanc42e84e2015-10-20 16:24:19 -0700699 tBuilder.setOutput(port);
700 nextObjBuilder.addTreatment(tBuilder.build());
701 });
702
703 NextObjective nextObj = nextObjBuilder.add();
704 flowObjectiveService.next(deviceId, nextObj);
705 log.debug("createGroupFromSubnetConfig: Submited "
706 + "next objective {} in device {}",
707 nextId, deviceId);
Charles Chan9f676b62015-10-29 14:58:10 -0700708
Charles Chanc42e84e2015-10-20 16:24:19 -0700709 subnetNextObjStore.put(key, nextId);
710 });
711 }
712
Charles Chane849c192016-01-11 18:28:54 -0800713 /**
714 * Creates broadcast groups for VLAN cross-connect ports.
715 *
716 * @param deviceId the DPID of the switch
717 */
718 public void createGroupsForXConnect(DeviceId deviceId) {
719 Map<VlanId, List<ConnectPoint>> xConnectsForDevice = deviceConfig.getXConnects();
720
721 xConnectsForDevice.forEach((vlanId, connectPoints) -> {
722 // Only proceed the xConnect for given device
723 for (ConnectPoint connectPoint : connectPoints) {
724 if (!connectPoint.deviceId().equals(deviceId)) {
725 return;
726 }
727 }
728
729 // Check if the next obj is already in the store
730 XConnectNextObjectiveStoreKey key =
731 new XConnectNextObjectiveStoreKey(deviceId, vlanId);
732 if (xConnectNextObjStore.containsKey(key)) {
733 log.debug("Cross-connect Broadcast group for device {} and vlanId {} exists",
734 deviceId, vlanId);
735 return;
736 }
737
738 TrafficSelector metadata =
739 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
740 int nextId = flowObjectiveService.allocateNextId();
741
742 NextObjective.Builder nextObjBuilder = DefaultNextObjective
743 .builder().withId(nextId)
744 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
745 .withMeta(metadata);
746
747 connectPoints.forEach(connectPoint -> {
748 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
749 tBuilder.setOutput(connectPoint.port());
750 nextObjBuilder.addTreatment(tBuilder.build());
751 });
752
753 NextObjective nextObj = nextObjBuilder.add();
754 flowObjectiveService.next(deviceId, nextObj);
755 log.debug("createGroupsForXConnect: Submited next objective {} in device {}",
756 nextId, deviceId);
757 xConnectNextObjStore.put(key, nextId);
758 });
759 }
760
Saurav Das4ce45962015-11-24 23:21:05 -0800761
762 /**
763 * Create simple next objective for a single port. The treatments can include
764 * all outgoing actions that need to happen on the packet.
765 *
766 * @param portNum the outgoing port on the device
767 * @param treatment the actions to apply on the packets (should include outport)
768 * @param meta optional data to pass to the driver
769 */
770 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
771 TrafficSelector meta) {
772 int nextId = flowObjectiveService.allocateNextId();
773 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
774 deviceId, portNum, treatment);
775
776 NextObjective.Builder nextObjBuilder = DefaultNextObjective
777 .builder().withId(nextId)
778 .withType(NextObjective.Type.SIMPLE)
779 .addTreatment(treatment)
780 .fromApp(appId)
781 .withMeta(meta);
782
783 NextObjective nextObj = nextObjBuilder.add();
784 flowObjectiveService.next(deviceId, nextObj);
785 log.debug("createGroupFromPort: Submited next objective {} in device {} "
786 + "for port {}", nextId, deviceId, portNum);
787
788 portNextObjStore.put(key, nextId);
789 }
790
sangho1e575652015-05-14 00:39:53 -0700791 /**
792 * Removes groups for the next objective ID given.
793 *
794 * @param objectiveId next objective ID to remove
795 * @return true if succeeds, false otherwise
796 */
797 public boolean removeGroup(int objectiveId) {
798
799 if (nsNextObjStore.containsValue(objectiveId)) {
800 NextObjective.Builder nextObjBuilder = DefaultNextObjective
801 .builder().withId(objectiveId)
802 .withType(NextObjective.Type.HASHED).fromApp(appId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700803 NextObjective nextObjective = nextObjBuilder.
804 remove(new SRNextObjectiveContext(deviceId));
Saurav Das8a0732e2015-11-20 15:27:53 -0800805 log.info("**removeGroup: Submited "
806 + "next objective {} in device {}",
807 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -0700808 flowObjectiveService.next(deviceId, nextObjective);
809
810 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) {
811 if (entry.getValue().equals(objectiveId)) {
812 nsNextObjStore.remove(entry.getKey());
813 break;
814 }
815 }
sangho0b2b6d12015-05-20 22:16:38 -0700816 return true;
sangho1e575652015-05-14 00:39:53 -0700817 }
818
819 return false;
820 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700821
Charles Chane849c192016-01-11 18:28:54 -0800822 /**
823 * Removes all groups from all next objective stores.
824 */
Saurav Das423fe2b2015-12-04 10:52:59 -0800825 public void removeAllGroups() {
826 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry:
827 nsNextObjStore.entrySet()) {
828 removeGroup(entry.getValue());
829 }
830 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
831 portNextObjStore.entrySet()) {
832 removeGroup(entry.getValue());
833 }
834 for (Map.Entry<SubnetNextObjectiveStoreKey, Integer> entry:
835 subnetNextObjStore.entrySet()) {
836 removeGroup(entry.getValue());
837 }
838 // should probably clean local stores port-neighbor
839 }
840
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700841 protected static class SRNextObjectiveContext implements ObjectiveContext {
842 final DeviceId deviceId;
843
844 SRNextObjectiveContext(DeviceId deviceId) {
845 this.deviceId = deviceId;
846 }
847 @Override
848 public void onSuccess(Objective objective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800849 log.info("Next objective {} operation successful in device {}",
850 objective.id(), deviceId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700851 }
852
853 @Override
854 public void onError(Objective objective, ObjectiveError error) {
855 log.warn("Next objective {} operation failed with error: {} in device {}",
Saurav Das8a0732e2015-11-20 15:27:53 -0800856 objective.id(), error, deviceId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700857 }
858 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800859}