2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.forwardingrulesmanager.internal;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.ObjectInputStream;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.EnumSet;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import java.util.concurrent.BlockingQueue;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.LinkedBlockingQueue;
32 import org.eclipse.osgi.framework.console.CommandInterpreter;
33 import org.eclipse.osgi.framework.console.CommandProvider;
34 import org.opendaylight.controller.clustering.services.CacheConfigException;
35 import org.opendaylight.controller.clustering.services.CacheExistException;
36 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
37 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
38 import org.opendaylight.controller.clustering.services.IClusterServices;
39 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
40 import org.opendaylight.controller.connectionmanager.IConnectionManager;
41 import org.opendaylight.controller.containermanager.IContainerManager;
42 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
43 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
44 import org.opendaylight.controller.forwardingrulesmanager.FlowEntryInstall;
45 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
46 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManagerAware;
47 import org.opendaylight.controller.forwardingrulesmanager.PortGroup;
48 import org.opendaylight.controller.forwardingrulesmanager.PortGroupChangeListener;
49 import org.opendaylight.controller.forwardingrulesmanager.PortGroupConfig;
50 import org.opendaylight.controller.forwardingrulesmanager.PortGroupProvider;
51 import org.opendaylight.controller.forwardingrulesmanager.implementation.data.FlowEntryDistributionOrder;
52 import org.opendaylight.controller.sal.action.Action;
53 import org.opendaylight.controller.sal.action.ActionType;
54 import org.opendaylight.controller.sal.action.Output;
55 import org.opendaylight.controller.sal.connection.ConnectionLocality;
56 import org.opendaylight.controller.sal.core.Config;
57 import org.opendaylight.controller.sal.core.ContainerFlow;
58 import org.opendaylight.controller.sal.core.IContainer;
59 import org.opendaylight.controller.sal.core.IContainerLocalListener;
60 import org.opendaylight.controller.sal.core.Node;
61 import org.opendaylight.controller.sal.core.NodeConnector;
62 import org.opendaylight.controller.sal.core.Property;
63 import org.opendaylight.controller.sal.core.UpdateType;
64 import org.opendaylight.controller.sal.flowprogrammer.Flow;
65 import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerListener;
66 import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerService;
67 import org.opendaylight.controller.sal.match.Match;
68 import org.opendaylight.controller.sal.match.MatchType;
69 import org.opendaylight.controller.sal.utils.EtherTypes;
70 import org.opendaylight.controller.sal.utils.GlobalConstants;
71 import org.opendaylight.controller.sal.utils.IObjectReader;
72 import org.opendaylight.controller.sal.utils.NodeCreator;
73 import org.opendaylight.controller.sal.utils.ObjectReader;
74 import org.opendaylight.controller.sal.utils.ObjectWriter;
75 import org.opendaylight.controller.sal.utils.Status;
76 import org.opendaylight.controller.sal.utils.StatusCode;
77 import org.opendaylight.controller.switchmanager.IInventoryListener;
78 import org.opendaylight.controller.switchmanager.ISwitchManager;
79 import org.opendaylight.controller.switchmanager.ISwitchManagerAware;
80 import org.opendaylight.controller.switchmanager.Subnet;
81 import org.osgi.framework.BundleContext;
82 import org.osgi.framework.FrameworkUtil;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
87 * Class that manages forwarding rule installation and removal per container of
88 * the network. It also maintains the central repository of all the forwarding
89 * rules installed on the network nodes.
91 public class ForwardingRulesManager implements
92 IForwardingRulesManager,
93 PortGroupChangeListener,
94 IContainerLocalListener,
96 IConfigurationContainerAware,
99 ICacheUpdateAware<Object,Object>,
101 IFlowProgrammerListener {
103 private static final Logger log = LoggerFactory.getLogger(ForwardingRulesManager.class);
104 private static final Logger logsync = LoggerFactory.getLogger("FRMsync");
105 private static final String PORT_REMOVED = "Port removed";
106 private static final String NODE_DOWN = "Node is Down";
107 private static final String INVALID_FLOW_ENTRY = "Invalid FlowEntry";
108 private String frmFileName;
109 private String portGroupFileName;
110 private ConcurrentMap<Integer, FlowConfig> staticFlows;
111 private ConcurrentMap<Integer, Integer> staticFlowsOrdinal;
112 private ConcurrentMap<String, PortGroupConfig> portGroupConfigs;
113 private ConcurrentMap<PortGroupConfig, Map<Node, PortGroup>> portGroupData;
114 private ConcurrentMap<String, Object> TSPolicies;
115 private IContainerManager containerManager;
116 private boolean inContainerMode; // being used by global instance only
117 protected boolean stopping;
120 * Flow database. It's the software view of what was requested to install
121 * and what is installed on the switch. It is indexed by the entry itself.
122 * The entry's hashcode resumes the network node index, the flow's priority
123 * and the flow's match. The value element is a class which contains the
124 * flow entry pushed by the applications modules and the respective
125 * container flow merged version. In absence of container flows, the two
126 * flow entries are the same.
128 private ConcurrentMap<FlowEntry, FlowEntry> originalSwView;
129 private ConcurrentMap<FlowEntryInstall, FlowEntryInstall> installedSwView;
131 * Per node and per group indexing
133 private ConcurrentMap<Node, List<FlowEntryInstall>> nodeFlows;
134 private ConcurrentMap<String, List<FlowEntryInstall>> groupFlows;
137 * Inactive flow list. This is for the global instance of FRM It will
138 * contain all the flow entries which were installed on the global container
139 * when the first container is created.
141 private ConcurrentMap<FlowEntry, FlowEntry> inactiveFlows;
143 private IContainer container;
144 private Set<IForwardingRulesManagerAware> frmAware =
145 Collections.synchronizedSet(new HashSet<IForwardingRulesManagerAware>());
146 private PortGroupProvider portGroupProvider;
147 private IFlowProgrammerService programmer;
148 private IClusterContainerServices clusterContainerService = null;
149 private ISwitchManager switchManager;
150 private Thread frmEventHandler;
151 protected BlockingQueue<FRMEvent> pendingEvents;
153 // Distributes FRM programming in the cluster
154 private IConnectionManager connectionManager;
157 * Name clustered caches used to support FRM entry distribution these are by
158 * necessity non-transactional as long as need to be able to synchronize
159 * states also while a transaction is in progress
161 static final String WORK_ORDER_CACHE = "frm.workOrder";
162 static final String WORK_STATUS_CACHE = "frm.workStatus";
163 static final String ORIGINAL_SW_VIEW_CACHE = "frm.originalSwView";
164 static final String INSTALLED_SW_VIEW_CACHE = "frm.installedSwView";
167 * Data structure responsible for distributing the FlowEntryInstall requests
168 * in the cluster. The key value is entry that is being either Installed or
169 * Updated or Delete. The value field is the same of the key value in case
170 * of Installation or Deletion, it's the new entry in case of Modification,
171 * this because the clustering caches don't allow null values.
173 * The logic behind this data structure is that the controller that initiate
174 * the request will place the order here, someone will pick it and then will
175 * remove from this data structure because is being served.
177 * TODO: We need to have a way to cleanup this data structure if entries are
178 * not picked by anyone, which is always a case can happen especially on
179 * Node disconnect cases.
181 protected ConcurrentMap<FlowEntryDistributionOrder, FlowEntryInstall> workOrder;
184 * Data structure responsible for retrieving the results of the workOrder
185 * submitted to the cluster.
187 * The logic behind this data structure is that the controller that has
188 * executed the order will then place the result in workStatus signaling
189 * that there was a success or a failure.
191 * TODO: The workStatus entries need to have a lifetime associated in case
192 * of requestor controller leaving the cluster.
194 protected ConcurrentMap<FlowEntryDistributionOrder, Status> workStatus;
197 * Local Map used to hold the Future which a caller can use to monitor for
200 private ConcurrentMap<FlowEntryDistributionOrder, FlowEntryDistributionOrderFutureTask> workMonitor =
201 new ConcurrentHashMap<FlowEntryDistributionOrder, FlowEntryDistributionOrderFutureTask>();
204 * Max pool size for the executor
206 private static final int maxPoolSize = 10;
210 * Entry being installed/updated/removed
212 * New entry will be placed after the update operation. Valid
213 * only for UpdateType.CHANGED, null for all the other cases
216 * @return a Future object for monitoring the progress of the result, or
217 * null in case the processing should take place locally
219 private FlowEntryDistributionOrderFutureTask distributeWorkOrder(FlowEntryInstall e, FlowEntryInstall u,
221 // A null entry it's an unexpected condition, anyway it's safe to keep
222 // the handling local
227 Node n = e.getNode();
228 if (connectionManager.getLocalityStatus(n) == ConnectionLocality.NOT_LOCAL) {
229 // Create the work order and distribute it
230 FlowEntryDistributionOrder fe =
231 new FlowEntryDistributionOrder(e, t, clusterContainerService.getMyAddress());
232 // First create the monitor job
233 FlowEntryDistributionOrderFutureTask ret = new FlowEntryDistributionOrderFutureTask(fe);
234 logsync.trace("Node {} not local so sending fe {}", n, fe);
235 workMonitor.put(fe, ret);
236 if (t.equals(UpdateType.CHANGED)) {
237 // Then distribute the work
238 workOrder.put(fe, u);
240 // Then distribute the work
241 workOrder.put(fe, e);
243 logsync.trace("WorkOrder requested");
244 // Now create an Handle to monitor the execution of the operation
248 logsync.trace("Node {} could be local. so processing Entry:{} UpdateType:{}", n, e, t);
253 * Adds a flow entry onto the network node It runs various validity checks
254 * and derive the final container flows merged entries that will be
255 * attempted to be installed
258 * the original flow entry application requested to add
260 * the flag indicating if this is a asynchronous request
261 * @return the status of this request. In case of asynchronous call, it will
262 * contain the unique id assigned to this request
264 private Status addEntry(FlowEntry flowEntry, boolean async) {
267 if (flowEntry == null || flowEntry.getNode() == null || flowEntry.getFlow() == null) {
268 String logMsg = INVALID_FLOW_ENTRY + ": {}";
269 log.warn(logMsg, flowEntry);
270 return new Status(StatusCode.NOTACCEPTABLE, INVALID_FLOW_ENTRY);
274 * Redundant Check: Check if the request is a redundant one from the
275 * same application the flowEntry is equal to an existing one. Given we
276 * do not have an application signature in the requested FlowEntry yet,
277 * we are here detecting the above condition by comparing the flow
278 * names, if set. If they are equal to the installed flow, most likely
279 * this is a redundant installation request from the same application
280 * and we can silently return success
282 * TODO: in future a sort of application reference list mechanism will
283 * be added to the FlowEntry so that exact flow can be used by different
286 FlowEntry present = this.originalSwView.get(flowEntry);
287 if (present != null) {
288 boolean sameFlow = present.getFlow().equals(flowEntry.getFlow());
289 boolean sameApp = present.getFlowName() != null && present.getFlowName().equals(flowEntry.getFlowName());
290 if (sameFlow && sameApp) {
291 log.trace("Skipping redundant request for flow {} on node {}", flowEntry.getFlowName(),
292 flowEntry.getNode());
293 return new Status(StatusCode.SUCCESS, "Entry is already present");
298 * Derive the container flow merged entries to install In presence of N
299 * container flows, we may end up with N different entries to install...
301 List<FlowEntryInstall> toInstallList = deriveInstallEntries(flowEntry.clone(), container.getContainerFlows());
303 // Container Flow conflict Check
304 if (toInstallList.isEmpty()) {
305 String msg = "Flow Entry conflicts with all Container Flows";
306 String logMsg = msg + ": {}";
307 log.warn(logMsg, flowEntry);
308 return new Status(StatusCode.CONFLICT, msg);
311 // Derive the list of entries good to be installed
312 List<FlowEntryInstall> toInstallSafe = new ArrayList<FlowEntryInstall>();
313 for (FlowEntryInstall entry : toInstallList) {
314 // Conflict Check: Verify new entry would not overwrite existing
316 if (this.installedSwView.containsKey(entry)) {
317 log.warn("Operation Rejected: A flow with same match and priority exists on the target node");
318 log.trace("Aborting to install {}", entry);
321 toInstallSafe.add(entry);
324 // Declare failure if all the container flow merged entries clash with
326 if (toInstallSafe.size() == 0) {
327 String msg = "A flow with same match and priority exists on the target node";
328 String logMsg = msg + ": {}";
329 log.warn(logMsg, flowEntry);
330 return new Status(StatusCode.CONFLICT, msg);
333 // Try to install an entry at the time
334 Status error = new Status(null, null);
335 Status succeded = null;
336 boolean oneSucceded = false;
337 for (FlowEntryInstall installEntry : toInstallSafe) {
339 // Install and update database
340 Status ret = addEntriesInternal(installEntry, async);
342 if (ret.isSuccess()) {
345 * The first successful status response will be returned For the
346 * asynchronous call, we can discard the container flow
347 * complication for now and assume we will always deal with one
348 * flow only per request
353 log.trace("Failed to install the entry: {}. The failure is: {}", installEntry, ret.getDescription());
357 return (oneSucceded) ? succeded : error;
361 * Given a flow entry and the list of container flows, it returns the list
362 * of container flow merged flow entries good to be installed on this
363 * container. If the list of container flows is null or empty, the install
364 * entry list will contain only one entry, the original flow entry. If the
365 * flow entry is congruent with all the N container flows, then the output
366 * install entry list will contain N entries. If the output list is empty,
367 * it means the passed flow entry conflicts with all the container flows.
370 * The list of container flows
371 * @return the list of container flow merged entries good to be installed on
374 private List<FlowEntryInstall> deriveInstallEntries(FlowEntry request, List<ContainerFlow> cFlowList) {
375 List<FlowEntryInstall> toInstallList = new ArrayList<FlowEntryInstall>(1);
377 if (container.getContainerFlows() == null || container.getContainerFlows().isEmpty()) {
378 // No container flows => entry good to be installed unchanged
379 toInstallList.add(new FlowEntryInstall(request.clone(), null));
381 // Create the list of entries to be installed. If the flow entry is
382 // not congruent with any container flow, no install entries will be
384 for (ContainerFlow cFlow : container.getContainerFlows()) {
385 if (cFlow.allowsFlow(request.getFlow())) {
386 toInstallList.add(new FlowEntryInstall(request.clone(), cFlow));
390 return toInstallList;
394 * Modify a flow entry with a new one It runs various validity check and
395 * derive the final container flows merged flow entries to work with
397 * @param currentFlowEntry
398 * @param newFlowEntry
400 * the flag indicating if this is a asynchronous request
401 * @return the status of this request. In case of asynchronous call, it will
402 * contain the unique id assigned to this request
404 private Status modifyEntry(FlowEntry currentFlowEntry, FlowEntry newFlowEntry, boolean async) {
408 if (currentFlowEntry == null || currentFlowEntry.getNode() == null || newFlowEntry == null
409 || newFlowEntry.getNode() == null || newFlowEntry.getFlow() == null) {
410 String msg = "Modify: " + INVALID_FLOW_ENTRY;
411 String logMsg = msg + ": {} or {}";
412 log.warn(logMsg, currentFlowEntry, newFlowEntry);
413 return new Status(StatusCode.NOTACCEPTABLE, msg);
415 if (!currentFlowEntry.getNode().equals(newFlowEntry.getNode())
416 || !currentFlowEntry.getFlowName().equals(newFlowEntry.getFlowName())) {
417 String msg = "Modify: Incompatible Flow Entries";
418 String logMsg = msg + ": {} and {}";
419 log.warn(logMsg, currentFlowEntry, newFlowEntry);
420 return new Status(StatusCode.NOTACCEPTABLE, msg);
424 if (currentFlowEntry.getFlow().equals(newFlowEntry.getFlow())) {
425 String msg = "Modify skipped as flows are the same";
426 String logMsg = msg + ": {} and {}";
427 log.debug(logMsg, currentFlowEntry, newFlowEntry);
428 return new Status(StatusCode.SUCCESS, msg);
432 * Conflict Check: Verify the new entry would not conflict with an
433 * existing one. This is a loose check on the previous original flow
434 * entry requests. No check on the container flow merged flow entries
437 FlowEntry sameMatchOriginalEntry = originalSwView.get(newFlowEntry);
438 if (sameMatchOriginalEntry != null && !sameMatchOriginalEntry.equals(currentFlowEntry)) {
439 String msg = "Operation Rejected: Another flow with same match and priority exists on the target node";
440 String logMsg = msg + ": {}";
441 log.warn(logMsg, currentFlowEntry);
442 return new Status(StatusCode.CONFLICT, msg);
445 // Derive the installed and toInstall entries
446 List<FlowEntryInstall> installedList = deriveInstallEntries(currentFlowEntry.clone(),
447 container.getContainerFlows());
448 List<FlowEntryInstall> toInstallList = deriveInstallEntries(newFlowEntry.clone(), container.getContainerFlows());
450 if (toInstallList.isEmpty()) {
451 String msg = "Modify Operation Rejected: The new entry conflicts with all the container flows";
452 String logMsg = msg + ": {}";
453 log.warn(logMsg, newFlowEntry);
455 return new Status(StatusCode.CONFLICT, msg);
459 * If the two list sizes differ, it means the new flow entry does not
460 * satisfy the same number of container flows the current entry does.
461 * This is only possible when the new entry and current entry have
462 * different match. In this scenario the modification would ultimately
463 * be handled as a remove and add operations in the protocol plugin.
465 * Also, if any of the new flow entries would clash with an existing
466 * one, we cannot proceed with the modify operation, because it would
467 * fail for some entries and leave stale entries on the network node.
468 * Modify path can be taken only if it can be performed completely, for
471 * So, for the above two cases, to simplify, let's decouple the modify
472 * in: 1) remove current entries 2) install new entries
474 Status succeeded = null;
475 boolean decouple = false;
476 if (installedList.size() != toInstallList.size()) {
477 log.info("Modify: New flow entry does not satisfy the same "
478 + "number of container flows as the original entry does");
481 List<FlowEntryInstall> toInstallSafe = new ArrayList<FlowEntryInstall>();
482 for (FlowEntryInstall installEntry : toInstallList) {
484 * Conflict Check: Verify the new entry would not overwrite another
487 FlowEntryInstall sameMatchEntry = installedSwView.get(installEntry);
488 if (sameMatchEntry != null && !sameMatchEntry.getOriginal().equals(currentFlowEntry)) {
489 log.info("Modify: new container flow merged flow entry clashes with existing flow");
492 toInstallSafe.add(installEntry);
497 // Remove current entries
498 for (FlowEntryInstall currEntry : installedList) {
499 this.removeEntryInternal(currEntry, async);
501 // Install new entries
502 for (FlowEntryInstall newEntry : toInstallSafe) {
503 succeeded = this.addEntriesInternal(newEntry, async);
507 * The two list have the same size and the entries to install do not
508 * clash with any existing flow on the network node. We assume here
509 * (and might be wrong) that the same container flows that were
510 * satisfied by the current entries are the same that are satisfied
511 * by the new entries. Let's take the risk for now.
513 * Note: modification has to be complete. If any entry modification
514 * fails, we need to stop, restore the already modified entries, and
517 Status retModify = null;
519 int size = toInstallList.size();
521 // Modify and update database
522 retModify = modifyEntryInternal(installedList.get(i), toInstallList.get(i), async);
523 if (retModify.isSuccess()) {
529 // Check if uncompleted modify
531 log.warn("Unable to perform a complete modify for all the container flows merged entries");
532 // Restore original entries
535 log.info("Attempting to restore initial entries");
536 retExt = modifyEntryInternal(toInstallList.get(i), installedList.get(i), async);
537 if (retExt.isSuccess()) {
543 // Fatal error, recovery failed
545 String msg = "Flow recovery failed ! Unrecoverable Error";
547 return new Status(StatusCode.INTERNALERROR, msg);
550 succeeded = retModify;
553 * The first successful status response will be returned. For the
554 * asynchronous call, we can discard the container flow complication for
555 * now and assume we will always deal with one flow only per request
561 * This is the function that modifies the final container flows merged
562 * entries on the network node and update the database. It expects that all
563 * the validity checks are passed
565 * @param currentEntries
568 * the flag indicating if this is a asynchronous request
569 * @return the status of this request. In case of asynchronous call, it will
570 * contain the unique id assigned to this request
572 private Status modifyEntryInternal(FlowEntryInstall currentEntries, FlowEntryInstall newEntries, boolean async) {
573 FlowEntryDistributionOrderFutureTask futureStatus =
574 distributeWorkOrder(currentEntries, newEntries, UpdateType.CHANGED);
575 if (futureStatus != null) {
576 Status retStatus = new Status(StatusCode.UNDEFINED);
578 retStatus = futureStatus.get();
579 if (retStatus.getCode()
580 .equals(StatusCode.TIMEOUT)) {
581 // A timeout happened, lets cleanup the workMonitor
582 workMonitor.remove(futureStatus.getOrder());
584 } catch (InterruptedException e) {
586 } catch (ExecutionException e) {
591 // Modify the flow on the network node
592 Status status = async ? programmer.modifyFlowAsync(currentEntries.getNode(), currentEntries.getInstall()
593 .getFlow(), newEntries.getInstall()
594 .getFlow()) : programmer.modifyFlow(currentEntries.getNode(), currentEntries.getInstall()
595 .getFlow(), newEntries.getInstall()
598 if (!status.isSuccess()) {
599 log.trace("SDN Plugin failed to program the flow: {}. The failure is: {}", newEntries.getInstall(),
600 status.getDescription());
604 log.trace("Modified {} => {}", currentEntries.getInstall(), newEntries.getInstall());
607 newEntries.setRequestId(status.getRequestId());
608 updateSwViews(currentEntries, false);
609 updateSwViews(newEntries, true);
616 * Remove a flow entry. If the entry is not present in the software view
617 * (entry or node not present), it return successfully
620 * the flow entry to remove
622 * the flag indicating if this is a asynchronous request
623 * @return the status of this request. In case of asynchronous call, it will
624 * contain the unique id assigned to this request
626 private Status removeEntry(FlowEntry flowEntry, boolean async) {
627 Status error = new Status(null, null);
630 if (flowEntry == null || flowEntry.getNode() == null || flowEntry.getFlow() == null) {
631 String logMsg = INVALID_FLOW_ENTRY + ": {}";
632 log.warn(logMsg, flowEntry);
633 return new Status(StatusCode.NOTACCEPTABLE, INVALID_FLOW_ENTRY);
636 // Derive the container flows merged installed entries
637 List<FlowEntryInstall> installedList = deriveInstallEntries(flowEntry.clone(), container.getContainerFlows());
639 Status succeeded = null;
640 boolean atLeastOneRemoved = false;
641 for (FlowEntryInstall entry : installedList) {
642 if (!installedSwView.containsKey(entry)) {
643 String logMsg = "Removal skipped (not present in software view) for flow entry: {}";
644 log.debug(logMsg, flowEntry);
645 if (installedList.size() == 1) {
646 // If we had only one entry to remove, we are done
647 return new Status(StatusCode.SUCCESS);
653 // Remove and update DB
654 Status ret = removeEntryInternal(entry, async);
656 if (!ret.isSuccess()) {
658 log.trace("Failed to remove the entry: {}. The failure is: {}", entry.getInstall(), ret.getDescription());
659 if (installedList.size() == 1) {
660 // If we had only one entry to remove, this is fatal failure
665 atLeastOneRemoved = true;
670 * No worries if full removal failed. Consistency checker will take care
671 * of removing the stale entries later, or adjusting the software
672 * database if not in sync with hardware
674 return (atLeastOneRemoved) ? succeeded : error;
678 * This is the function that removes the final container flows merged entry
679 * from the network node and update the database. It expects that all the
680 * validity checks are passed
683 * the flow entry to remove
685 * the flag indicating if this is a asynchronous request
686 * @return the status of this request. In case of asynchronous call, it will
687 * contain the unique id assigned to this request
689 private Status removeEntryInternal(FlowEntryInstall entry, boolean async) {
690 FlowEntryDistributionOrderFutureTask futureStatus = distributeWorkOrder(entry, null, UpdateType.REMOVED);
691 if (futureStatus != null) {
692 Status retStatus = new Status(StatusCode.UNDEFINED);
694 retStatus = futureStatus.get();
695 if (retStatus.getCode()
696 .equals(StatusCode.TIMEOUT)) {
697 // A timeout happened, lets cleanup the workMonitor
698 workMonitor.remove(futureStatus.getOrder());
700 } catch (InterruptedException e) {
702 } catch (ExecutionException e) {
707 // Mark the entry to be deleted (for CC just in case we fail)
711 Status status = async ? programmer.removeFlowAsync(entry.getNode(), entry.getInstall()
712 .getFlow()) : programmer.removeFlow(entry.getNode(), entry.getInstall()
715 if (!status.isSuccess()) {
716 log.trace("SDN Plugin failed to remove the flow: {}. The failure is: {}", entry.getInstall(),
717 status.getDescription());
720 log.trace("Removed {}", entry.getInstall());
723 updateSwViews(entry, false);
730 * This is the function that installs the final container flow merged entry
731 * on the network node and updates the database. It expects that all the
732 * validity and conflict checks are passed. That means it does not check
733 * whether this flow would conflict or overwrite an existing one.
736 * the flow entry to install
738 * the flag indicating if this is a asynchronous request
739 * @return the status of this request. In case of asynchronous call, it will
740 * contain the unique id assigned to this request
742 private Status addEntriesInternal(FlowEntryInstall entry, boolean async) {
743 FlowEntryDistributionOrderFutureTask futureStatus = distributeWorkOrder(entry, null, UpdateType.ADDED);
744 if (futureStatus != null) {
745 Status retStatus = new Status(StatusCode.UNDEFINED);
747 retStatus = futureStatus.get();
748 if (retStatus.getCode()
749 .equals(StatusCode.TIMEOUT)) {
750 // A timeout happened, lets cleanup the workMonitor
751 workMonitor.remove(futureStatus.getOrder());
753 } catch (InterruptedException e) {
755 } catch (ExecutionException e) {
760 // Install the flow on the network node
761 Status status = async ? programmer.addFlowAsync(entry.getNode(), entry.getInstall()
762 .getFlow()) : programmer.addFlow(entry.getNode(), entry.getInstall()
765 if (!status.isSuccess()) {
766 log.trace("SDN Plugin failed to program the flow: {}. The failure is: {}", entry.getInstall(),
767 status.getDescription());
771 log.trace("Added {}", entry.getInstall());
774 entry.setRequestId(status.getRequestId());
775 updateSwViews(entry, true);
782 * Returns true if the flow conflicts with all the container's flows. This
783 * means that if the function returns true, the passed flow entry is
784 * congruent with at least one container flow, hence it is good to be
785 * installed on this container.
788 * @return true if flow conflicts with all the container flows, false
791 private boolean entryConflictsWithContainerFlows(FlowEntry flowEntry) {
792 List<ContainerFlow> cFlowList = container.getContainerFlows();
794 // Validity check and avoid unnecessary computation
795 // Also takes care of default container where no container flows are
797 if (cFlowList == null || cFlowList.isEmpty()) {
801 for (ContainerFlow cFlow : cFlowList) {
802 if (cFlow.allowsFlow(flowEntry.getFlow())) {
803 // Entry is allowed by at least one container flow: good to go
810 private ConcurrentMap.Entry<Integer, FlowConfig> getStaticFlowEntry(String name, Node node) {
811 for (ConcurrentMap.Entry<Integer, FlowConfig> flowEntry : staticFlows.entrySet()) {
812 FlowConfig flowConfig = flowEntry.getValue();
813 if (flowConfig.isByNameAndNodeIdEqual(name, node)) {
820 private void updateIndexDatabase(FlowEntryInstall entry, boolean add) {
821 // Update node indexed flow database
822 updateNodeFlowsDB(entry, add);
824 // Update group indexed flow database
825 updateGroupFlowsDB(entry, add);
829 * Update the node mapped flows database
831 private void updateSwViews(FlowEntryInstall flowEntries, boolean add) {
833 originalSwView.put(flowEntries.getOriginal(), flowEntries.getOriginal());
834 installedSwView.put(flowEntries, flowEntries);
836 originalSwView.remove(flowEntries.getOriginal());
837 installedSwView.remove(flowEntries);
842 * Update the node mapped flows database
844 private void updateNodeFlowsDB(FlowEntryInstall flowEntries, boolean add) {
845 Node node = flowEntries.getNode();
847 List<FlowEntryInstall> nodeIndeces = this.nodeFlows.get(node);
848 if (nodeIndeces == null) {
852 nodeIndeces = new ArrayList<FlowEntryInstall>();
857 nodeIndeces.add(flowEntries);
859 nodeIndeces.remove(flowEntries);
862 // Update cache across cluster
863 if (nodeIndeces.isEmpty()) {
864 this.nodeFlows.remove(node);
866 this.nodeFlows.put(node, nodeIndeces);
871 * Update the group name mapped flows database
873 private void updateGroupFlowsDB(FlowEntryInstall flowEntries, boolean add) {
874 String groupName = flowEntries.getGroupName();
876 // Flow may not be part of a group
877 if (groupName == null) {
881 List<FlowEntryInstall> indices = this.groupFlows.get(groupName);
882 if (indices == null) {
886 indices = new ArrayList<FlowEntryInstall>();
891 indices.add(flowEntries);
893 indices.remove(flowEntries);
896 // Update cache across cluster
897 if (indices.isEmpty()) {
898 this.groupFlows.remove(groupName);
900 this.groupFlows.put(groupName, indices);
905 * Remove a flow entry that has been added previously First checks if the
906 * entry is effectively present in the local database
908 @SuppressWarnings("unused")
909 private Status removeEntry(Node node, String flowName) {
910 FlowEntryInstall target = null;
913 for (FlowEntryInstall entry : installedSwView.values()) {
914 if (entry.equalsByNodeAndName(node, flowName)) {
920 // If it is not there, stop any further processing
921 if (target == null) {
922 return new Status(StatusCode.SUCCESS, "Entry is not present");
926 Status status = programmer.removeFlow(target.getNode(), target.getInstall().getFlow());
929 if (status.isSuccess()) {
930 updateSwViews(target, false);
933 log.trace("SDN Plugin failed to remove the flow: {}. The failure is: {}", target.getInstall(),
934 status.getDescription());
941 public Status installFlowEntry(FlowEntry flowEntry) {
943 if (isContainerModeAllowed(flowEntry)) {
944 status = addEntry(flowEntry, false);
946 String msg = "Controller in container mode: Install Refused";
947 String logMsg = msg + ": {}";
948 status = new Status(StatusCode.NOTACCEPTABLE, msg);
949 log.warn(logMsg, flowEntry);
955 public Status installFlowEntryAsync(FlowEntry flowEntry) {
957 if (isContainerModeAllowed(flowEntry)) {
958 status = addEntry(flowEntry, true);
960 String msg = "Controller in container mode: Install Refused";
961 status = new Status(StatusCode.NOTACCEPTABLE, msg);
968 public Status uninstallFlowEntry(FlowEntry flowEntry) {
970 if (isContainerModeAllowed(flowEntry)) {
971 status = removeEntry(flowEntry, false);
973 String msg = "Controller in container mode: Uninstall Refused";
974 String logMsg = msg + ": {}";
975 status = new Status(StatusCode.NOTACCEPTABLE, msg);
976 log.warn(logMsg, flowEntry);
982 public Status uninstallFlowEntryAsync(FlowEntry flowEntry) {
984 if (isContainerModeAllowed(flowEntry)) {
985 status = removeEntry(flowEntry, true);
987 String msg = "Controller in container mode: Uninstall Refused";
988 status = new Status(StatusCode.NOTACCEPTABLE, msg);
995 public Status modifyFlowEntry(FlowEntry currentFlowEntry, FlowEntry newFlowEntry) {
996 Status status = null;
997 if (isContainerModeAllowed(currentFlowEntry)) {
998 status = modifyEntry(currentFlowEntry, newFlowEntry, false);
1000 String msg = "Controller in container mode: Modify Refused";
1001 String logMsg = msg + ": {}";
1002 status = new Status(StatusCode.NOTACCEPTABLE, msg);
1003 log.warn(logMsg, newFlowEntry);
1009 public Status modifyFlowEntryAsync(FlowEntry currentFlowEntry, FlowEntry newFlowEntry) {
1010 Status status = null;
1011 if (isContainerModeAllowed(currentFlowEntry)) {
1012 status = modifyEntry(currentFlowEntry, newFlowEntry, true);
1014 String msg = "Controller in container mode: Modify Refused";
1015 status = new Status(StatusCode.NOTACCEPTABLE, msg);
1022 * Returns whether the specified flow entry is allowed to be
1023 * installed/removed/modified based on the current container mode status.
1024 * This call always returns true in the container instance of forwarding
1025 * rules manager. It is meant for the global instance only (default
1026 * container) of forwarding rules manager. Idea is that for assuring
1027 * container isolation of traffic, flow installation in default container is
1028 * blocked when in container mode (containers are present). The only flows
1029 * that are allowed in container mode in the default container are the
1030 * proactive flows, the ones automatically installed on the network node
1031 * which forwarding mode has been configured to "proactive". These flows are
1032 * needed by controller to discover the nodes topology and to discover the
1033 * attached hosts for some SDN switches.
1036 * The flow entry to be installed/removed/modified
1037 * @return true if not in container mode or if flowEntry is internally
1040 private boolean isContainerModeAllowed(FlowEntry flowEntry) {
1041 return (!inContainerMode) ? true : flowEntry.isInternal();
1045 public Status modifyOrAddFlowEntry(FlowEntry newFlowEntry) {
1047 * Run a check on the original entries to decide whether to go with a
1048 * add or modify method. A loose check means only check against the
1049 * original flow entry requests and not against the installed flow
1050 * entries which are the result of the original entry merged with the
1051 * container flow(s) (if any). The modifyFlowEntry method in presence of
1052 * conflicts with the Container flows (if any) would revert back to a
1053 * delete + add pattern
1055 FlowEntry currentFlowEntry = originalSwView.get(newFlowEntry);
1057 if (currentFlowEntry != null) {
1058 return modifyFlowEntry(currentFlowEntry, newFlowEntry);
1060 return installFlowEntry(newFlowEntry);
1065 public Status modifyOrAddFlowEntryAsync(FlowEntry newFlowEntry) {
1067 * Run a check on the original entries to decide whether to go with a
1068 * add or modify method. A loose check means only check against the
1069 * original flow entry requests and not against the installed flow
1070 * entries which are the result of the original entry merged with the
1071 * container flow(s) (if any). The modifyFlowEntry method in presence of
1072 * conflicts with the Container flows (if any) would revert back to a
1073 * delete + add pattern
1075 FlowEntry currentFlowEntry = originalSwView.get(newFlowEntry);
1077 if (currentFlowEntry != null) {
1078 return modifyFlowEntryAsync(currentFlowEntry, newFlowEntry);
1080 return installFlowEntryAsync(newFlowEntry);
1085 public Status uninstallFlowEntryGroup(String groupName) {
1086 if (groupName == null || groupName.isEmpty()) {
1087 return new Status(StatusCode.BADREQUEST, "Invalid group name");
1089 if (groupName.equals(FlowConfig.INTERNALSTATICFLOWGROUP)) {
1090 return new Status(StatusCode.BADREQUEST, "Internal static flows group cannot be deleted through this api");
1092 if (inContainerMode) {
1093 String msg = "Controller in container mode: Group Uninstall Refused";
1094 String logMsg = msg + ": {}";
1095 log.warn(logMsg, groupName);
1096 return new Status(StatusCode.NOTACCEPTABLE, msg);
1098 int toBeRemoved = 0;
1100 if (groupFlows.containsKey(groupName)) {
1101 List<FlowEntryInstall> list = new ArrayList<FlowEntryInstall>(groupFlows.get(groupName));
1102 toBeRemoved = list.size();
1103 for (FlowEntryInstall entry : list) {
1104 Status status = this.removeEntry(entry.getOriginal(), false);
1105 if (status.isSuccess()) {
1108 error = status.getDescription();
1112 return (toBeRemoved == 0) ? new Status(StatusCode.SUCCESS) : new Status(StatusCode.INTERNALERROR,
1113 "Not all the flows were removed: " + error);
1117 public Status uninstallFlowEntryGroupAsync(String groupName) {
1118 if (groupName == null || groupName.isEmpty()) {
1119 return new Status(StatusCode.BADREQUEST, "Invalid group name");
1121 if (groupName.equals(FlowConfig.INTERNALSTATICFLOWGROUP)) {
1122 return new Status(StatusCode.BADREQUEST, "Static flows group cannot be deleted through this api");
1124 if (inContainerMode) {
1125 String msg = "Controller in container mode: Group Uninstall Refused";
1126 String logMsg = msg + ": {}";
1127 log.warn(logMsg, groupName);
1128 return new Status(StatusCode.NOTACCEPTABLE, msg);
1130 if (groupFlows.containsKey(groupName)) {
1131 List<FlowEntryInstall> list = new ArrayList<FlowEntryInstall>(groupFlows.get(groupName));
1132 for (FlowEntryInstall entry : list) {
1133 this.removeEntry(entry.getOriginal(), true);
1136 return new Status(StatusCode.SUCCESS);
1140 public boolean checkFlowEntryConflict(FlowEntry flowEntry) {
1141 return entryConflictsWithContainerFlows(flowEntry);
1145 * Updates all installed flows because the container flow got updated This
1146 * is obtained in two phases on per node basis: 1) Uninstall of all flows 2)
1147 * Reinstall of all flows This is needed because a new container flows
1148 * merged flow may conflict with an existing old container flows merged flow
1149 * on the network node
1151 protected void updateFlowsContainerFlow() {
1152 Set<FlowEntry> toReInstall = new HashSet<FlowEntry>();
1153 // First remove all installed entries
1154 for (ConcurrentMap.Entry<FlowEntryInstall, FlowEntryInstall> entry : installedSwView.entrySet()) {
1155 FlowEntryInstall current = entry.getValue();
1156 // Store the original entry
1157 toReInstall.add(current.getOriginal());
1158 // Remove the old couples. No validity checks to be run, use the
1160 this.removeEntryInternal(current, false);
1162 // Then reinstall the original entries
1163 for (FlowEntry entry : toReInstall) {
1164 // Reinstall the original flow entries, via the regular path: new
1165 // cFlow merge + validations
1166 this.installFlowEntry(entry);
1170 private void nonClusterObjectCreate() {
1171 originalSwView = new ConcurrentHashMap<FlowEntry, FlowEntry>();
1172 installedSwView = new ConcurrentHashMap<FlowEntryInstall, FlowEntryInstall>();
1173 TSPolicies = new ConcurrentHashMap<String, Object>();
1174 staticFlowsOrdinal = new ConcurrentHashMap<Integer, Integer>();
1175 portGroupConfigs = new ConcurrentHashMap<String, PortGroupConfig>();
1176 portGroupData = new ConcurrentHashMap<PortGroupConfig, Map<Node, PortGroup>>();
1177 staticFlows = new ConcurrentHashMap<Integer, FlowConfig>();
1178 inactiveFlows = new ConcurrentHashMap<FlowEntry, FlowEntry>();
1181 private void registerWithOSGIConsole() {
1182 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
1183 bundleContext.registerService(CommandProvider.class.getName(), this, null);
1187 public void setTSPolicyData(String policyname, Object o, boolean add) {
1190 /* Check if this policy already exists */
1191 if (!(TSPolicies.containsKey(policyname))) {
1192 TSPolicies.put(policyname, o);
1195 TSPolicies.remove(policyname);
1197 if (frmAware != null) {
1198 synchronized (frmAware) {
1199 for (IForwardingRulesManagerAware frma : frmAware) {
1201 frma.policyUpdate(policyname, add);
1202 } catch (Exception e) {
1203 log.warn("Exception on callback", e);
1211 public Map<String, Object> getTSPolicyData() {
1216 public Object getTSPolicyData(String policyName) {
1217 if (TSPolicies.containsKey(policyName)) {
1218 return TSPolicies.get(policyName);
1225 public List<FlowEntry> getFlowEntriesForGroup(String policyName) {
1226 List<FlowEntry> list = new ArrayList<FlowEntry>();
1227 if (policyName != null && !policyName.trim().isEmpty()) {
1228 for (Map.Entry<FlowEntry, FlowEntry> entry : this.originalSwView.entrySet()) {
1229 if (policyName.equals(entry.getKey().getGroupName())) {
1230 list.add(entry.getKey().clone());
1238 public List<FlowEntry> getInstalledFlowEntriesForGroup(String policyName) {
1239 List<FlowEntry> list = new ArrayList<FlowEntry>();
1240 if (policyName != null && !policyName.trim().isEmpty()) {
1241 for (Map.Entry<FlowEntryInstall, FlowEntryInstall> entry : this.installedSwView.entrySet()) {
1242 if (policyName.equals(entry.getKey().getGroupName())) {
1243 list.add(entry.getKey().getInstall().clone());
1251 public void addOutputPort(Node node, String flowName, List<NodeConnector> portList) {
1253 for (FlowEntryInstall flow : this.nodeFlows.get(node)) {
1254 if (flow.getFlowName().equals(flowName)) {
1255 FlowEntry currentFlowEntry = flow.getOriginal();
1256 FlowEntry newFlowEntry = currentFlowEntry.clone();
1257 for (NodeConnector dstPort : portList) {
1258 newFlowEntry.getFlow().addAction(new Output(dstPort));
1260 Status error = modifyEntry(currentFlowEntry, newFlowEntry, false);
1261 if (error.isSuccess()) {
1262 log.info("Ports {} added to FlowEntry {}", portList, flowName);
1264 log.warn("Failed to add ports {} to Flow entry {}. The failure is: {}", portList,
1265 currentFlowEntry.toString(), error.getDescription());
1270 log.warn("Failed to add ports to Flow {} on Node {}: Entry Not Found", flowName, node);
1274 public void removeOutputPort(Node node, String flowName, List<NodeConnector> portList) {
1275 for (FlowEntryInstall index : this.nodeFlows.get(node)) {
1276 FlowEntryInstall flow = this.installedSwView.get(index);
1277 if (flow.getFlowName().equals(flowName)) {
1278 FlowEntry currentFlowEntry = flow.getOriginal();
1279 FlowEntry newFlowEntry = currentFlowEntry.clone();
1280 for (NodeConnector dstPort : portList) {
1281 Action action = new Output(dstPort);
1282 newFlowEntry.getFlow().removeAction(action);
1284 Status status = modifyEntry(currentFlowEntry, newFlowEntry, false);
1285 if (status.isSuccess()) {
1286 log.info("Ports {} removed from FlowEntry {}", portList, flowName);
1288 log.warn("Failed to remove ports {} from Flow entry {}. The failure is: {}", portList,
1289 currentFlowEntry.toString(), status.getDescription());
1294 log.warn("Failed to remove ports from Flow {} on Node {}: Entry Not Found", flowName, node);
1298 * This function assumes the target flow has only one output port
1301 public void replaceOutputPort(Node node, String flowName, NodeConnector outPort) {
1302 FlowEntry currentFlowEntry = null;
1303 FlowEntry newFlowEntry = null;
1306 for (FlowEntryInstall index : this.nodeFlows.get(node)) {
1307 FlowEntryInstall flow = this.installedSwView.get(index);
1308 if (flow.getFlowName().equals(flowName)) {
1309 currentFlowEntry = flow.getOriginal();
1313 if (currentFlowEntry == null) {
1314 log.warn("Failed to replace output port for flow {} on node {}: Entry Not Found", flowName, node);
1318 // Create a flow copy with the new output port
1319 newFlowEntry = currentFlowEntry.clone();
1320 Action target = null;
1321 for (Action action : newFlowEntry.getFlow().getActions()) {
1322 if (action.getType() == ActionType.OUTPUT) {
1327 newFlowEntry.getFlow().removeAction(target);
1328 newFlowEntry.getFlow().addAction(new Output(outPort));
1330 // Modify on network node
1331 Status status = modifyEntry(currentFlowEntry, newFlowEntry, false);
1333 if (status.isSuccess()) {
1334 log.info("Output port replaced with {} for flow {} on node {}", outPort, flowName, node);
1336 log.warn("Failed to replace output port for flow {} on node {}. The failure is: {}", flowName, node,
1337 status.getDescription());
1343 public NodeConnector getOutputPort(Node node, String flowName) {
1344 for (FlowEntryInstall index : this.nodeFlows.get(node)) {
1345 FlowEntryInstall flow = this.installedSwView.get(index);
1346 if (flow.getFlowName().equals(flowName)) {
1347 for (Action action : flow.getOriginal().getFlow().getActions()) {
1348 if (action.getType() == ActionType.OUTPUT) {
1349 return ((Output) action).getPort();
1357 private void cacheStartup() {
1362 private void allocateCaches() {
1363 if (this.clusterContainerService == null) {
1364 log.warn("Un-initialized clusterContainerService, can't create cache");
1368 log.debug("Allocating caches for Container {}", container.getName());
1371 clusterContainerService.createCache(ORIGINAL_SW_VIEW_CACHE,
1372 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1374 clusterContainerService.createCache(INSTALLED_SW_VIEW_CACHE,
1375 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1377 clusterContainerService.createCache("frm.inactiveFlows",
1378 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1380 clusterContainerService.createCache("frm.staticFlows",
1381 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1383 clusterContainerService.createCache("frm.staticFlowsOrdinal",
1384 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1386 clusterContainerService.createCache("frm.portGroupConfigs",
1387 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1389 clusterContainerService.createCache("frm.portGroupData",
1390 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1392 clusterContainerService.createCache("frm.TSPolicies",
1393 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
1395 clusterContainerService.createCache(WORK_STATUS_CACHE,
1396 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL, IClusterServices.cacheMode.ASYNC));
1398 clusterContainerService.createCache(WORK_ORDER_CACHE,
1399 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL, IClusterServices.cacheMode.ASYNC));
1401 } catch (CacheConfigException cce) {
1402 log.error("CacheConfigException");
1403 } catch (CacheExistException cce) {
1404 log.error("CacheExistException");
1408 @SuppressWarnings({ "unchecked" })
1409 private void retrieveCaches() {
1410 ConcurrentMap<?, ?> map;
1412 if (this.clusterContainerService == null) {
1413 log.warn("un-initialized clusterContainerService, can't retrieve cache");
1414 nonClusterObjectCreate();
1418 log.debug("Retrieving Caches for Container {}", container.getName());
1420 map = clusterContainerService.getCache(ORIGINAL_SW_VIEW_CACHE);
1422 originalSwView = (ConcurrentMap<FlowEntry, FlowEntry>) map;
1424 log.error("Retrieval of frm.originalSwView cache failed for Container {}", container.getName());
1427 map = clusterContainerService.getCache(INSTALLED_SW_VIEW_CACHE);
1429 installedSwView = (ConcurrentMap<FlowEntryInstall, FlowEntryInstall>) map;
1431 log.error("Retrieval of frm.installedSwView cache failed for Container {}", container.getName());
1434 map = clusterContainerService.getCache("frm.inactiveFlows");
1436 inactiveFlows = (ConcurrentMap<FlowEntry, FlowEntry>) map;
1438 log.error("Retrieval of frm.inactiveFlows cache failed for Container {}", container.getName());
1441 map = clusterContainerService.getCache("frm.staticFlows");
1443 staticFlows = (ConcurrentMap<Integer, FlowConfig>) map;
1445 log.error("Retrieval of frm.staticFlows cache failed for Container {}", container.getName());
1448 map = clusterContainerService.getCache("frm.staticFlowsOrdinal");
1450 staticFlowsOrdinal = (ConcurrentMap<Integer, Integer>) map;
1452 log.error("Retrieval of frm.staticFlowsOrdinal cache failed for Container {}", container.getName());
1455 map = clusterContainerService.getCache("frm.portGroupConfigs");
1457 portGroupConfigs = (ConcurrentMap<String, PortGroupConfig>) map;
1459 log.error("Retrieval of frm.portGroupConfigs cache failed for Container {}", container.getName());
1462 map = clusterContainerService.getCache("frm.portGroupData");
1464 portGroupData = (ConcurrentMap<PortGroupConfig, Map<Node, PortGroup>>) map;
1466 log.error("Retrieval of frm.portGroupData allocation failed for Container {}", container.getName());
1469 map = clusterContainerService.getCache("frm.TSPolicies");
1471 TSPolicies = (ConcurrentMap<String, Object>) map;
1473 log.error("Retrieval of frm.TSPolicies cache failed for Container {}", container.getName());
1476 map = clusterContainerService.getCache(WORK_ORDER_CACHE);
1478 workOrder = (ConcurrentMap<FlowEntryDistributionOrder, FlowEntryInstall>) map;
1480 log.error("Retrieval of " + WORK_ORDER_CACHE + " cache failed for Container {}", container.getName());
1483 map = clusterContainerService.getCache(WORK_STATUS_CACHE);
1485 workStatus = (ConcurrentMap<FlowEntryDistributionOrder, Status>) map;
1487 log.error("Retrieval of " + WORK_STATUS_CACHE + " cache failed for Container {}", container.getName());
1491 private boolean flowConfigExists(FlowConfig config) {
1492 // Flow name has to be unique on per node id basis
1493 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1494 if (entry.getValue().isByNameAndNodeIdEqual(config)) {
1502 public Status addStaticFlow(FlowConfig config) {
1503 // Configuration object validation
1504 Status status = config.validate(container);
1505 if (!status.isSuccess()) {
1506 log.warn("Invalid Configuration for flow {}. The failure is {}", config, status.getDescription());
1507 String error = "Invalid Configuration (" + status.getDescription() + ")";
1508 config.setStatus(error);
1509 return new Status(StatusCode.BADREQUEST, error);
1511 return addStaticFlowInternal(config, false);
1515 * Private method to add a static flow configuration which does not run any
1516 * validation on the passed FlowConfig object. If restore is set to true,
1517 * configuration is stored in configuration database regardless the
1518 * installation on the network node was successful. This is useful at boot
1519 * when static flows are present in startup configuration and are read
1520 * before the switches connects.
1523 * The static flow configuration
1525 * if true, the configuration is stored regardless the
1526 * installation on the network node was successful
1527 * @return The status of this request
1529 private Status addStaticFlowInternal(FlowConfig config, boolean restore) {
1530 boolean multipleFlowPush = false;
1533 config.setStatus(StatusCode.SUCCESS.toString());
1536 if (flowConfigExists(config)) {
1537 error = "Entry with this name on specified switch already exists";
1538 log.warn("Entry with this name on specified switch already exists: {}", config);
1539 config.setStatus(error);
1540 return new Status(StatusCode.CONFLICT, error);
1543 if ((config.getIngressPort() == null) && config.getPortGroup() != null) {
1544 for (String portGroupName : portGroupConfigs.keySet()) {
1545 if (portGroupName.equalsIgnoreCase(config.getPortGroup())) {
1546 multipleFlowPush = true;
1550 if (!multipleFlowPush) {
1551 log.warn("Invalid Configuration(Invalid PortGroup Name) for flow {}", config);
1552 error = "Invalid Configuration (Invalid PortGroup Name)";
1553 config.setStatus(error);
1554 return new Status(StatusCode.BADREQUEST, error);
1559 * If requested program the entry in hardware first before updating the
1562 if (!multipleFlowPush) {
1564 if (config.installInHw()) {
1565 FlowEntry entry = config.getFlowEntry();
1566 status = this.installFlowEntry(entry);
1567 if (!status.isSuccess()) {
1568 config.setStatus(status.getDescription());
1577 * When the control reaches this point, either of the following
1578 * conditions is true 1. This is a single entry configuration (non
1579 * PortGroup) and the hardware installation is successful 2. This is a
1580 * multiple entry configuration (PortGroup) and hardware installation is
1581 * NOT done directly on this event. 3. The User prefers to retain the
1582 * configuration in Controller and skip hardware installation.
1584 * Hence it is safe to update the StaticFlow DB at this point.
1586 * Note : For the case of PortGrouping, it is essential to have this DB
1587 * populated before the PortGroupListeners can query for the DB
1588 * triggered using portGroupChanged event...
1590 Integer ordinal = staticFlowsOrdinal.get(0);
1591 staticFlowsOrdinal.put(0, ++ordinal);
1592 staticFlows.put(ordinal, config);
1594 if (multipleFlowPush) {
1595 PortGroupConfig pgconfig = portGroupConfigs.get(config.getPortGroup());
1596 Map<Node, PortGroup> existingData = portGroupData.get(pgconfig);
1597 if (existingData != null) {
1598 portGroupChanged(pgconfig, existingData, true);
1601 return new Status(StatusCode.SUCCESS);
1604 private void addStaticFlowsToSwitch(Node node) {
1605 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1606 FlowConfig config = entry.getValue();
1607 if (config.isPortGroupEnabled()) {
1610 if (config.getNode().equals(node)) {
1611 if (config.installInHw() && !config.getStatus().equals(StatusCode.SUCCESS.toString())) {
1612 Status status = this.installFlowEntryAsync(config.getFlowEntry());
1613 config.setStatus(status.getDescription());
1617 // Update cluster cache
1618 refreshClusterStaticFlowsStatus(node);
1621 private void updateStaticFlowConfigsOnNodeDown(Node node) {
1622 log.trace("Updating Static Flow configs on node down: {}", node);
1624 List<Integer> toRemove = new ArrayList<Integer>();
1625 for (Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1627 FlowConfig config = entry.getValue();
1629 if (config.isPortGroupEnabled()) {
1633 if (config.installInHw() && config.getNode().equals(node)) {
1634 if (config.isInternalFlow()) {
1635 // Take note of this controller generated static flow
1636 toRemove.add(entry.getKey());
1638 config.setStatus(NODE_DOWN);
1642 // Remove controller generated static flows for this node
1643 for (Integer index : toRemove) {
1644 staticFlows.remove(index);
1646 // Update cluster cache
1647 refreshClusterStaticFlowsStatus(node);
1651 private void updateStaticFlowConfigsOnContainerModeChange(UpdateType update) {
1652 log.trace("Updating Static Flow configs on container mode change: {}", update);
1654 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1655 FlowConfig config = entry.getValue();
1656 if (config.isPortGroupEnabled()) {
1659 if (config.installInHw() && !config.isInternalFlow()) {
1662 config.setStatus("Removed from node because in container mode");
1665 config.setStatus(StatusCode.SUCCESS.toString());
1671 // Update cluster cache
1672 refreshClusterStaticFlowsStatus(null);
1676 public Status removeStaticFlow(FlowConfig config) {
1678 * No config.isInternal() check as NB does not take this path and GUI
1679 * cannot issue a delete on an internal generated flow. We need this
1680 * path to be accessible when switch mode is changed from proactive to
1681 * reactive, so that we can remove the internal generated LLDP and ARP
1685 // Look for the target configuration entry
1687 FlowConfig target = null;
1688 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1689 if (entry.getValue().isByNameAndNodeIdEqual(config)) {
1690 key = entry.getKey();
1691 target = entry.getValue();
1695 if (target == null) {
1696 return new Status(StatusCode.NOTFOUND, "Entry Not Present");
1699 // Program the network node
1700 Status status = this.uninstallFlowEntry(config.getFlowEntry());
1702 // Update configuration database if programming was successful
1703 if (status.isSuccess()) {
1704 staticFlows.remove(key);
1711 public Status removeStaticFlow(String name, Node node) {
1712 // Look for the target configuration entry
1714 FlowConfig target = null;
1715 for (ConcurrentMap.Entry<Integer, FlowConfig> mapEntry : staticFlows.entrySet()) {
1716 if (mapEntry.getValue().isByNameAndNodeIdEqual(name, node)) {
1717 key = mapEntry.getKey();
1718 target = mapEntry.getValue();
1722 if (target == null) {
1723 return new Status(StatusCode.NOTFOUND, "Entry Not Present");
1726 // Validity check for api3 entry point
1727 if (target.isInternalFlow()) {
1728 String msg = "Invalid operation: Controller generated flow cannot be deleted";
1729 String logMsg = msg + ": {}";
1730 log.warn(logMsg, name);
1731 return new Status(StatusCode.NOTACCEPTABLE, msg);
1734 if (target.isPortGroupEnabled()) {
1735 String msg = "Invalid operation: Port Group flows cannot be deleted through this API";
1736 String logMsg = msg + ": {}";
1737 log.warn(logMsg, name);
1738 return new Status(StatusCode.NOTACCEPTABLE, msg);
1741 // Program the network node
1742 Status status = this.removeEntry(target.getFlowEntry(), false);
1744 // Update configuration database if programming was successful
1745 if (status.isSuccess()) {
1746 staticFlows.remove(key);
1753 public Status modifyStaticFlow(FlowConfig newFlowConfig) {
1754 // Validity check for api3 entry point
1755 if (newFlowConfig.isInternalFlow()) {
1756 String msg = "Invalid operation: Controller generated flow cannot be modified";
1757 String logMsg = msg + ": {}";
1758 log.warn(logMsg, newFlowConfig);
1759 return new Status(StatusCode.NOTACCEPTABLE, msg);
1763 Status status = newFlowConfig.validate(container);
1764 if (!status.isSuccess()) {
1765 String msg = "Invalid Configuration (" + status.getDescription() + ")";
1766 newFlowConfig.setStatus(msg);
1767 log.warn("Invalid Configuration for flow {}. The failure is {}", newFlowConfig, status.getDescription());
1768 return new Status(StatusCode.BADREQUEST, msg);
1771 FlowConfig oldFlowConfig = null;
1772 Integer index = null;
1773 for (ConcurrentMap.Entry<Integer, FlowConfig> mapEntry : staticFlows.entrySet()) {
1774 FlowConfig entry = mapEntry.getValue();
1775 if (entry.isByNameAndNodeIdEqual(newFlowConfig.getName(), newFlowConfig.getNode())) {
1776 oldFlowConfig = entry;
1777 index = mapEntry.getKey();
1782 if (oldFlowConfig == null) {
1783 String msg = "Attempt to modify a non existing static flow";
1784 String logMsg = msg + ": {}";
1785 log.warn(logMsg, newFlowConfig);
1786 return new Status(StatusCode.NOTFOUND, msg);
1789 // Do not attempt to reinstall the flow, warn user
1790 if (newFlowConfig.equals(oldFlowConfig)) {
1791 String msg = "No modification detected";
1792 log.info("Static flow modification skipped. New flow and old flow are the same: {}", newFlowConfig);
1793 return new Status(StatusCode.SUCCESS, msg);
1796 // If flow is installed, program the network node
1797 status = new Status(StatusCode.SUCCESS, "Saved in config");
1798 if (oldFlowConfig.installInHw()) {
1799 status = this.modifyFlowEntry(oldFlowConfig.getFlowEntry(), newFlowConfig.getFlowEntry());
1802 // Update configuration database if programming was successful
1803 if (status.isSuccess()) {
1804 newFlowConfig.setStatus(status.getDescription());
1805 staticFlows.put(index, newFlowConfig);
1812 public Status toggleStaticFlowStatus(String name, Node node) {
1813 return toggleStaticFlowStatus(getStaticFlow(name, node));
1817 public Status toggleStaticFlowStatus(FlowConfig config) {
1818 if (config == null) {
1819 String msg = "Invalid request: null flow config";
1821 return new Status(StatusCode.BADREQUEST, msg);
1823 // Validity check for api3 entry point
1824 if (config.isInternalFlow()) {
1825 String msg = "Invalid operation: Controller generated flow cannot be modified";
1826 String logMsg = msg + ": {}";
1827 log.warn(logMsg, config);
1828 return new Status(StatusCode.NOTACCEPTABLE, msg);
1831 // Find the config entry
1833 FlowConfig target = null;
1834 for (Map.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1835 FlowConfig conf = entry.getValue();
1836 if (conf.isByNameAndNodeIdEqual(config)) {
1837 key = entry.getKey();
1842 if (target != null) {
1843 Status status = target.validate(container);
1844 if (!status.isSuccess()) {
1845 log.warn(status.getDescription());
1848 status = (target.installInHw()) ? this.uninstallFlowEntry(target.getFlowEntry()) : this
1849 .installFlowEntry(target.getFlowEntry());
1850 if (status.isSuccess()) {
1851 // Update Configuration database
1852 target.setStatus(StatusCode.SUCCESS.toString());
1853 target.toggleInstallation();
1854 staticFlows.put(key, target);
1859 return new Status(StatusCode.NOTFOUND, "Unable to locate the entry. Failed to toggle status");
1863 * Reinsert all static flows entries in the cache to force cache updates in
1864 * the cluster. This is useful when only some parameters were changed in the
1865 * entries, like the status.
1868 * The node for which the static flow configurations have to be
1869 * refreshed. If null, all nodes static flows will be refreshed.
1871 private void refreshClusterStaticFlowsStatus(Node node) {
1872 // Refresh cluster cache
1873 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1874 if (node == null || entry.getValue().getNode().equals(node)) {
1875 staticFlows.put(entry.getKey(), entry.getValue());
1881 * Uninstall all the non-internal Flow Entries present in the software view.
1882 * If requested, a copy of each original flow entry will be stored in the
1883 * inactive list so that it can be re-applied when needed (This is typically
1884 * the case when running in the default container and controller moved to
1885 * container mode) NOTE WELL: The routine as long as does a bulk change will
1886 * operate only on the entries for nodes locally attached so to avoid
1887 * redundant operations initiated by multiple nodes
1889 * @param preserveFlowEntries
1890 * if true, a copy of each original entry is stored in the
1893 private void uninstallAllFlowEntries(boolean preserveFlowEntries) {
1894 log.info("Uninstalling all non-internal flows");
1896 List<FlowEntryInstall> toRemove = new ArrayList<FlowEntryInstall>();
1898 // Store entries / create target list
1899 for (ConcurrentMap.Entry<FlowEntryInstall, FlowEntryInstall> mapEntry : installedSwView.entrySet()) {
1900 FlowEntryInstall flowEntries = mapEntry.getValue();
1901 // Skip internal generated static flows
1902 if (!flowEntries.isInternal()) {
1903 toRemove.add(flowEntries);
1904 // Store the original entries if requested
1905 if (preserveFlowEntries) {
1906 inactiveFlows.put(flowEntries.getOriginal(), flowEntries.getOriginal());
1911 // Now remove the entries
1912 for (FlowEntryInstall flowEntryHw : toRemove) {
1913 Node n = flowEntryHw.getNode();
1914 if (n != null && connectionManager.getLocalityStatus(n) == ConnectionLocality.LOCAL) {
1915 Status status = this.removeEntryInternal(flowEntryHw, false);
1916 if (!status.isSuccess()) {
1917 log.trace("Failed to remove entry: {}. The failure is: {}", flowEntryHw, status.getDescription());
1920 log.debug("Not removing entry {} because not connected locally, the remote guy will do it's job",
1927 * Re-install all the Flow Entries present in the inactive list The inactive
1928 * list will be empty at the end of this call This function is called on the
1929 * default container instance of FRM only when the last container is deleted
1931 private void reinstallAllFlowEntries() {
1932 log.info("Reinstalling all inactive flows");
1934 for (FlowEntry flowEntry : this.inactiveFlows.keySet()) {
1935 this.addEntry(flowEntry, false);
1938 // Empty inactive list in any case
1939 inactiveFlows.clear();
1943 public List<FlowConfig> getStaticFlows() {
1944 return getStaticFlowsOrderedList(staticFlows, staticFlowsOrdinal.get(0).intValue());
1947 // TODO: need to come out with a better algorithm for maintaining the order
1948 // of the configuration entries
1949 // with actual one, index associated to deleted entries cannot be reused and
1951 private List<FlowConfig> getStaticFlowsOrderedList(ConcurrentMap<Integer, FlowConfig> flowMap, int maxKey) {
1952 List<FlowConfig> orderedList = new ArrayList<FlowConfig>();
1953 for (int i = 0; i <= maxKey; i++) {
1954 FlowConfig entry = flowMap.get(i);
1955 if (entry != null) {
1956 orderedList.add(entry);
1963 public FlowConfig getStaticFlow(String name, Node node) {
1964 ConcurrentMap.Entry<Integer, FlowConfig> entry = getStaticFlowEntry(name, node);
1966 return entry.getValue();
1972 public List<FlowConfig> getStaticFlows(Node node) {
1973 List<FlowConfig> list = new ArrayList<FlowConfig>();
1974 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1975 if (entry.getValue().onNode(node)) {
1976 list.add(entry.getValue());
1983 public List<String> getStaticFlowNamesForNode(Node node) {
1984 List<String> list = new ArrayList<String>();
1985 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1986 if (entry.getValue().onNode(node)) {
1987 list.add(entry.getValue().getName());
1994 public List<Node> getListNodeWithConfiguredFlows() {
1995 Set<Node> set = new HashSet<Node>();
1996 for (ConcurrentMap.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1997 set.add(entry.getValue().getNode());
1999 return new ArrayList<Node>(set);
2002 @SuppressWarnings("unchecked")
2003 private void loadFlowConfiguration() {
2004 ObjectReader objReader = new ObjectReader();
2005 ConcurrentMap<Integer, FlowConfig> confList = (ConcurrentMap<Integer, FlowConfig>) objReader.read(this,
2008 ConcurrentMap<String, PortGroupConfig> pgConfig = (ConcurrentMap<String, PortGroupConfig>) objReader.read(this,
2011 if (pgConfig != null) {
2012 for (ConcurrentMap.Entry<String, PortGroupConfig> entry : pgConfig.entrySet()) {
2013 addPortGroupConfig(entry.getKey(), entry.getValue().getMatchString(), true);
2017 if (confList == null) {
2022 for (Integer key : confList.keySet()) {
2023 if (key.intValue() > maxKey) {
2024 maxKey = key.intValue();
2028 for (FlowConfig conf : getStaticFlowsOrderedList(confList, maxKey)) {
2029 addStaticFlowInternal(conf, true);
2034 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
2035 return ois.readObject();
2039 public Status saveConfig() {
2040 return saveConfigInternal();
2043 private Status saveConfigInternal() {
2044 ObjectWriter objWriter = new ObjectWriter();
2045 ConcurrentMap<Integer, FlowConfig> nonDynamicFlows = new ConcurrentHashMap<Integer, FlowConfig>();
2046 for (Integer ordinal : staticFlows.keySet()) {
2047 FlowConfig config = staticFlows.get(ordinal);
2048 // Do not save dynamic and controller generated static flows
2049 if (config.isDynamic() || config.isInternalFlow()) {
2052 nonDynamicFlows.put(ordinal, config);
2054 objWriter.write(nonDynamicFlows, frmFileName);
2055 objWriter.write(new ConcurrentHashMap<String, PortGroupConfig>(portGroupConfigs), portGroupFileName);
2056 return new Status(StatusCode.SUCCESS, null);
2060 public void subnetNotify(Subnet sub, boolean add) {
2066 * @see org.opendaylight.controller.switchmanager.ISwitchManagerAware#modeChangeNotify(org.opendaylight.controller.sal.core.Node,
2069 * This method can be called from within the OSGi framework context,
2070 * given the programming operation can take sometime, it not good
2071 * pratice to have in it's context operations that can take time,
2072 * hence moving off to a different thread for async processing.
2074 private ExecutorService executor;
2076 public void modeChangeNotify(final Node node, final boolean proactive) {
2077 Callable<Status> modeChangeCallable = new Callable<Status>() {
2079 public Status call() throws Exception {
2080 List<FlowConfig> defaultConfigs = new ArrayList<FlowConfig>();
2082 List<String> puntAction = new ArrayList<String>();
2083 puntAction.add(ActionType.CONTROLLER.toString());
2085 FlowConfig allowARP = new FlowConfig();
2086 allowARP.setInstallInHw(true);
2087 allowARP.setName(FlowConfig.INTERNALSTATICFLOWBEGIN + "Punt ARP" + FlowConfig.INTERNALSTATICFLOWEND);
2088 allowARP.setPriority("1");
2089 allowARP.setNode(node);
2090 allowARP.setEtherType("0x" + Integer.toHexString(EtherTypes.ARP.intValue())
2092 allowARP.setActions(puntAction);
2093 defaultConfigs.add(allowARP);
2095 FlowConfig allowLLDP = new FlowConfig();
2096 allowLLDP.setInstallInHw(true);
2097 allowLLDP.setName(FlowConfig.INTERNALSTATICFLOWBEGIN + "Punt LLDP" + FlowConfig.INTERNALSTATICFLOWEND);
2098 allowLLDP.setPriority("1");
2099 allowLLDP.setNode(node);
2100 allowLLDP.setEtherType("0x" + Integer.toHexString(EtherTypes.LLDP.intValue())
2102 allowLLDP.setActions(puntAction);
2103 defaultConfigs.add(allowLLDP);
2105 List<String> dropAction = new ArrayList<String>();
2106 dropAction.add(ActionType.DROP.toString());
2108 FlowConfig dropAllConfig = new FlowConfig();
2109 dropAllConfig.setInstallInHw(true);
2110 dropAllConfig.setName(FlowConfig.INTERNALSTATICFLOWBEGIN + "Catch-All Drop"
2111 + FlowConfig.INTERNALSTATICFLOWEND);
2112 dropAllConfig.setPriority("0");
2113 dropAllConfig.setNode(node);
2114 dropAllConfig.setActions(dropAction);
2115 defaultConfigs.add(dropAllConfig);
2117 log.info("Forwarding mode for node {} set to {}", node, (proactive ? "proactive" : "reactive"));
2118 for (FlowConfig fc : defaultConfigs) {
2119 Status status = (proactive) ? addStaticFlowInternal(fc, false) : removeStaticFlow(fc);
2120 if (status.isSuccess()) {
2121 log.info("{} Proactive Static flow: {}", (proactive ? "Installed" : "Removed"), fc.getName());
2123 log.warn("Failed to {} Proactive Static flow: {}", (proactive ? "install" : "remove"),
2127 return new Status(StatusCode.SUCCESS);
2132 * Execute the work outside the caller context, this could be an
2133 * expensive operation and we don't want to block the caller for it.
2135 this.executor.submit(modeChangeCallable);
2139 * Remove from the databases all the flows installed on the node
2143 private void cleanDatabaseForNode(Node node) {
2144 log.info("Cleaning Flow database for Node {}", node);
2145 if (nodeFlows.containsKey(node)) {
2146 List<FlowEntryInstall> toRemove = new ArrayList<FlowEntryInstall>(nodeFlows.get(node));
2148 for (FlowEntryInstall entry : toRemove) {
2149 updateSwViews(entry, false);
2154 private boolean doesFlowContainNodeConnector(Flow flow, NodeConnector nc) {
2159 Match match = flow.getMatch();
2160 if (match.isPresent(MatchType.IN_PORT)) {
2161 NodeConnector matchPort = (NodeConnector) match.getField(MatchType.IN_PORT).getValue();
2162 if (matchPort.equals(nc)) {
2166 List<Action> actionsList = flow.getActions();
2167 if (actionsList != null) {
2168 for (Action action : actionsList) {
2169 if (action instanceof Output) {
2170 NodeConnector actionPort = ((Output) action).getPort();
2171 if (actionPort.equals(nc)) {
2181 public void notifyNode(Node node, UpdateType type, Map<String, Property> propMap) {
2182 this.pendingEvents.offer(new NodeUpdateEvent(type, node));
2186 public void notifyNodeConnector(NodeConnector nodeConnector, UpdateType type, Map<String, Property> propMap) {
2187 boolean updateStaticFlowCluster = false;
2193 Config config = (propMap == null) ? null : (Config) propMap.get(Config.ConfigPropName);
2194 if (config != null) {
2195 switch (config.getValue()) {
2196 case Config.ADMIN_DOWN:
2197 log.trace("Port {} is administratively down: uninstalling interested flows", nodeConnector);
2198 updateStaticFlowCluster = removeFlowsOnNodeConnectorDown(nodeConnector);
2200 case Config.ADMIN_UP:
2201 log.trace("Port {} is administratively up: installing interested flows", nodeConnector);
2202 updateStaticFlowCluster = installFlowsOnNodeConnectorUp(nodeConnector);
2204 case Config.ADMIN_UNDEF:
2211 // This is the case where a switch port is removed from the SDN agent space
2212 log.trace("Port {} was removed from our control: uninstalling interested flows", nodeConnector);
2213 updateStaticFlowCluster = removeFlowsOnNodeConnectorDown(nodeConnector);
2219 if (updateStaticFlowCluster) {
2220 refreshClusterStaticFlowsStatus(nodeConnector.getNode());
2225 * It goes through the static flows configuration, it identifies the ones
2226 * which have the specified node connector as input or output port and
2227 * install them on the network node if they are marked to be installed in
2228 * hardware and their status shows they were not installed yet
2230 private boolean installFlowsOnNodeConnectorUp(NodeConnector nodeConnector) {
2231 boolean updated = false;
2232 List<FlowConfig> flowConfigForNode = getStaticFlows(nodeConnector.getNode());
2233 for (FlowConfig flowConfig : flowConfigForNode) {
2234 if (doesFlowContainNodeConnector(flowConfig.getFlow(), nodeConnector)) {
2235 if (flowConfig.installInHw() && !flowConfig.getStatus().equals(StatusCode.SUCCESS.toString())) {
2236 Status status = this.installFlowEntry(flowConfig.getFlowEntry());
2237 if (!status.isSuccess()) {
2238 flowConfig.setStatus(status.getDescription());
2240 flowConfig.setStatus(StatusCode.SUCCESS.toString());
2250 * Remove from the network node all the flows which have the specified node
2251 * connector as input or output port. If any of the flow entry is a static
2252 * flow, it updates the correspondent configuration.
2254 private boolean removeFlowsOnNodeConnectorDown(NodeConnector nodeConnector) {
2255 boolean updated = false;
2256 List<FlowEntryInstall> nodeFlowEntries = nodeFlows.get(nodeConnector.getNode());
2257 if (nodeFlowEntries == null) {
2260 for (FlowEntryInstall fei : new ArrayList<FlowEntryInstall>(nodeFlowEntries)) {
2261 if (doesFlowContainNodeConnector(fei.getInstall().getFlow(), nodeConnector)) {
2262 Status status = this.removeEntryInternal(fei, true);
2263 if (!status.isSuccess()) {
2267 * If the flow entry is a static flow, then update its
2270 if (fei.getGroupName().equals(FlowConfig.STATICFLOWGROUP)) {
2271 FlowConfig flowConfig = getStaticFlow(fei.getFlowName(), fei.getNode());
2272 if (flowConfig != null) {
2273 flowConfig.setStatus(PORT_REMOVED);
2282 private FlowConfig getDerivedFlowConfig(FlowConfig original, String configName, Short port) {
2283 FlowConfig derivedFlow = new FlowConfig(original);
2284 derivedFlow.setDynamic(true);
2285 derivedFlow.setPortGroup(null);
2286 derivedFlow.setName(original.getName() + "_" + configName + "_" + port);
2287 derivedFlow.setIngressPort(port + "");
2291 private void addPortGroupFlows(PortGroupConfig config, Node node, PortGroup data) {
2292 for (FlowConfig staticFlow : staticFlows.values()) {
2293 if (staticFlow.getPortGroup() == null) {
2296 if ((staticFlow.getNode().equals(node)) && (staticFlow.getPortGroup().equals(config.getName()))) {
2297 for (Short port : data.getPorts()) {
2298 FlowConfig derivedFlow = getDerivedFlowConfig(staticFlow, config.getName(), port);
2299 addStaticFlowInternal(derivedFlow, false);
2305 private void removePortGroupFlows(PortGroupConfig config, Node node, PortGroup data) {
2306 for (FlowConfig staticFlow : staticFlows.values()) {
2307 if (staticFlow.getPortGroup() == null) {
2310 if (staticFlow.getNode().equals(node) && staticFlow.getPortGroup().equals(config.getName())) {
2311 for (Short port : data.getPorts()) {
2312 FlowConfig derivedFlow = getDerivedFlowConfig(staticFlow, config.getName(), port);
2313 removeStaticFlow(derivedFlow);
2320 public void portGroupChanged(PortGroupConfig config, Map<Node, PortGroup> data, boolean add) {
2321 log.info("PortGroup Changed for: {} Data: {}", config, portGroupData);
2322 Map<Node, PortGroup> existingData = portGroupData.get(config);
2323 if (existingData != null) {
2324 for (Map.Entry<Node, PortGroup> entry : data.entrySet()) {
2325 PortGroup existingPortGroup = existingData.get(entry.getKey());
2326 if (existingPortGroup == null) {
2328 existingData.put(entry.getKey(), entry.getValue());
2329 addPortGroupFlows(config, entry.getKey(), entry.getValue());
2333 existingPortGroup.getPorts().addAll(entry.getValue().getPorts());
2334 addPortGroupFlows(config, entry.getKey(), entry.getValue());
2336 existingPortGroup.getPorts().removeAll(entry.getValue().getPorts());
2337 removePortGroupFlows(config, entry.getKey(), entry.getValue());
2343 portGroupData.put(config, data);
2344 for (Node swid : data.keySet()) {
2345 addPortGroupFlows(config, swid, data.get(swid));
2352 public boolean addPortGroupConfig(String name, String regex, boolean restore) {
2353 PortGroupConfig config = portGroupConfigs.get(name);
2354 if (config != null) {
2358 if ((portGroupProvider == null) && !restore) {
2361 if ((portGroupProvider != null) && (!portGroupProvider.isMatchCriteriaSupported(regex))) {
2365 config = new PortGroupConfig(name, regex);
2366 portGroupConfigs.put(name, config);
2367 if (portGroupProvider != null) {
2368 portGroupProvider.createPortGroupConfig(config);
2374 public boolean delPortGroupConfig(String name) {
2375 PortGroupConfig config = portGroupConfigs.get(name);
2376 if (config == null) {
2380 if (portGroupProvider != null) {
2381 portGroupProvider.deletePortGroupConfig(config);
2383 portGroupConfigs.remove(name);
2388 public Map<String, PortGroupConfig> getPortGroupConfigs() {
2389 return portGroupConfigs;
2392 public boolean isPortGroupSupported() {
2393 if (portGroupProvider == null) {
2399 public void setIContainer(IContainer s) {
2403 public void unsetIContainer(IContainer s) {
2404 if (this.container == s) {
2405 this.container = null;
2410 public PortGroupProvider getPortGroupProvider() {
2411 return portGroupProvider;
2414 public void unsetPortGroupProvider(PortGroupProvider portGroupProvider) {
2415 this.portGroupProvider = null;
2418 public void setPortGroupProvider(PortGroupProvider portGroupProvider) {
2419 this.portGroupProvider = portGroupProvider;
2420 portGroupProvider.registerPortGroupChange(this);
2421 for (PortGroupConfig config : portGroupConfigs.values()) {
2422 portGroupProvider.createPortGroupConfig(config);
2426 public void setFrmAware(IForwardingRulesManagerAware obj) {
2427 this.frmAware.add(obj);
2430 public void unsetFrmAware(IForwardingRulesManagerAware obj) {
2431 this.frmAware.remove(obj);
2434 void setClusterContainerService(IClusterContainerServices s) {
2435 log.debug("Cluster Service set");
2436 this.clusterContainerService = s;
2439 void unsetClusterContainerService(IClusterContainerServices s) {
2440 if (this.clusterContainerService == s) {
2441 log.debug("Cluster Service removed!");
2442 this.clusterContainerService = null;
2446 private String getContainerName() {
2447 if (container == null) {
2448 return GlobalConstants.DEFAULT.toString();
2450 return container.getName();
2454 * Function called by the dependency manager when all the required
2455 * dependencies are satisfied
2459 frmFileName = GlobalConstants.STARTUPHOME.toString() + "frm_staticflows_" + this.getContainerName() + ".conf";
2460 portGroupFileName = GlobalConstants.STARTUPHOME.toString() + "portgroup_" + this.getContainerName() + ".conf";
2462 inContainerMode = false;
2464 if (portGroupProvider != null) {
2465 portGroupProvider.registerPortGroupChange(this);
2468 nodeFlows = new ConcurrentHashMap<Node, List<FlowEntryInstall>>();
2469 groupFlows = new ConcurrentHashMap<String, List<FlowEntryInstall>>();
2473 registerWithOSGIConsole();
2476 * If we are not the first cluster node to come up, do not initialize
2477 * the static flow entries ordinal
2479 if (staticFlowsOrdinal.size() == 0) {
2480 staticFlowsOrdinal.put(0, Integer.valueOf(0));
2483 pendingEvents = new LinkedBlockingQueue<FRMEvent>();
2485 // Initialize the event handler thread
2486 frmEventHandler = new Thread(new Runnable() {
2491 final FRMEvent event = pendingEvents.take();
2492 if (event == null) {
2493 log.warn("Dequeued null event");
2496 log.trace("Dequeued {} event", event.getClass().getSimpleName());
2497 if (event instanceof NodeUpdateEvent) {
2498 NodeUpdateEvent update = (NodeUpdateEvent) event;
2499 Node node = update.getNode();
2500 switch (update.getUpdateType()) {
2502 addStaticFlowsToSwitch(node);
2505 cleanDatabaseForNode(node);
2506 updateStaticFlowConfigsOnNodeDown(node);
2510 } else if (event instanceof ErrorReportedEvent) {
2511 ErrorReportedEvent errEvent = (ErrorReportedEvent) event;
2512 processErrorEvent(errEvent);
2513 } else if (event instanceof WorkOrderEvent) {
2515 * Take care of handling the remote Work request
2517 Runnable r = new Runnable() {
2520 WorkOrderEvent work = (WorkOrderEvent) event;
2521 FlowEntryDistributionOrder fe = work.getFe();
2523 logsync.trace("Executing the workOrder {}", fe);
2524 Status gotStatus = null;
2525 FlowEntryInstall feiCurrent = fe.getEntry();
2526 FlowEntryInstall feiNew = workOrder.get(fe);
2527 switch (fe.getUpType()) {
2529 gotStatus = addEntriesInternal(feiCurrent, false);
2532 gotStatus = modifyEntryInternal(feiCurrent, feiNew, false);
2535 gotStatus = removeEntryInternal(feiCurrent, false);
2539 workOrder.remove(fe);
2541 "The workOrder has been executed and now the status is being returned {}", fe);
2543 workStatus.put(fe, gotStatus);
2545 log.warn("Not expected null WorkOrder", work);
2549 if(executor != null) {
2550 executor.execute(r);
2552 } else if (event instanceof WorkStatusCleanup) {
2554 * Take care of handling the remote Work request
2556 WorkStatusCleanup work = (WorkStatusCleanup) event;
2557 FlowEntryDistributionOrder fe = work.getFe();
2559 logsync.trace("The workStatus {} is being removed", fe);
2560 workStatus.remove(fe);
2562 log.warn("Not expected null WorkStatus", work);
2564 } else if (event instanceof ContainerFlowChangeEvent) {
2566 * Whether it is an addition or removal, we have to
2567 * recompute the merged flows entries taking into
2568 * account all the current container flows because
2569 * flow merging is not an injective function
2571 updateFlowsContainerFlow();
2572 } else if (event instanceof UpdateIndexDBs) {
2573 UpdateIndexDBs update = (UpdateIndexDBs)event;
2574 updateIndexDatabase(update.getFei(), update.isAddition());
2576 log.warn("Dequeued unknown event {}", event.getClass().getSimpleName());
2578 } catch (InterruptedException e) {
2579 // clear pending events
2580 pendingEvents.clear();
2584 }, "FRM EventHandler Collector");
2588 * Function called by the dependency manager when at least one dependency
2589 * become unsatisfied or when the component is shutting down because for
2590 * example bundle is being stopped.
2594 // Interrupt the thread
2595 frmEventHandler.interrupt();
2596 // Clear the pendingEvents queue
2597 pendingEvents.clear();
2599 workMonitor.clear();
2603 * Function called by dependency manager after "init ()" is called and after
2604 * the services provided by the class are registered in the service registry
2609 * If running in default container, need to know if controller is in
2612 if (GlobalConstants.DEFAULT.toString().equals(this.getContainerName())) {
2613 inContainerMode = containerManager.inContainerMode();
2616 // Initialize graceful stop flag
2619 // Allocate the executor service
2620 this.executor = Executors.newFixedThreadPool(maxPoolSize);
2622 // Start event handler thread
2623 frmEventHandler.start();
2626 * Read startup and build database if we have not already gotten the
2627 * configurations synced from another node
2629 if (staticFlows.isEmpty()) {
2630 loadFlowConfiguration();
2635 * Function called by the dependency manager before the services exported by
2636 * the component are unregistered, this will be followed by a "destroy ()"
2641 uninstallAllFlowEntries(false);
2642 // Shutdown executor
2643 this.executor.shutdownNow();
2644 // Now walk all the workMonitor and wake up the one sleeping because
2645 // destruction is happening
2646 for (FlowEntryDistributionOrder fe : workMonitor.keySet()) {
2647 FlowEntryDistributionOrderFutureTask task = workMonitor.get(fe);
2652 public void setFlowProgrammerService(IFlowProgrammerService service) {
2653 this.programmer = service;
2656 public void unsetFlowProgrammerService(IFlowProgrammerService service) {
2657 if (this.programmer == service) {
2658 this.programmer = null;
2662 public void setSwitchManager(ISwitchManager switchManager) {
2663 this.switchManager = switchManager;
2666 public void unsetSwitchManager(ISwitchManager switchManager) {
2667 if (this.switchManager == switchManager) {
2668 this.switchManager = null;
2673 public void tagUpdated(String containerName, Node n, short oldTag, short newTag, UpdateType t) {
2674 if (!container.getName().equals(containerName)) {
2680 public void containerFlowUpdated(String containerName, ContainerFlow previous, ContainerFlow current, UpdateType t) {
2681 if (!container.getName().equals(containerName)) {
2684 log.trace("Container {}: Updating installed flows because of container flow change: {} {}",
2685 container.getName(), t, current);
2686 ContainerFlowChangeEvent ev = new ContainerFlowChangeEvent(previous, current, t);
2687 pendingEvents.offer(ev);
2691 public void nodeConnectorUpdated(String containerName, NodeConnector nc, UpdateType t) {
2692 if (!container.getName().equals(containerName)) {
2696 boolean updateStaticFlowCluster = false;
2700 log.trace("Port {} was removed from container: uninstalling interested flows", nc);
2701 updateStaticFlowCluster = removeFlowsOnNodeConnectorDown(nc);
2704 log.trace("Port {} was added to container: reinstall interested flows", nc);
2705 updateStaticFlowCluster = installFlowsOnNodeConnectorUp(nc);
2713 if (updateStaticFlowCluster) {
2714 refreshClusterStaticFlowsStatus(nc.getNode());
2719 public void containerModeUpdated(UpdateType update) {
2720 // Only default container instance reacts on this event
2721 if (!container.getName().equals(GlobalConstants.DEFAULT.toString())) {
2727 * Controller is moving to container mode. We are in the default
2728 * container context, we need to remove all our non-internal flows
2729 * to prevent any container isolation breakage. We also need to
2730 * preserve our flow so that they can be re-installed if we move
2731 * back to non container mode (no containers).
2733 this.inContainerMode = true;
2734 this.uninstallAllFlowEntries(true);
2737 this.inContainerMode = false;
2738 this.reinstallAllFlowEntries();
2743 // Update our configuration DB
2744 updateStaticFlowConfigsOnContainerModeChange(update);
2747 protected abstract class FRMEvent {
2751 private class NodeUpdateEvent extends FRMEvent {
2752 private final Node node;
2753 private final UpdateType update;
2755 public NodeUpdateEvent(UpdateType update, Node node) {
2756 this.update = update;
2760 public UpdateType getUpdateType() {
2764 public Node getNode() {
2769 private class ErrorReportedEvent extends FRMEvent {
2770 private final long rid;
2771 private final Node node;
2772 private final Object error;
2774 public ErrorReportedEvent(long rid, Node node, Object error) {
2780 public long getRequestId() {
2784 public Object getError() {
2788 public Node getNode() {
2793 private class WorkOrderEvent extends FRMEvent {
2794 private FlowEntryDistributionOrder fe;
2795 private FlowEntryInstall newEntry;
2801 WorkOrderEvent(FlowEntryDistributionOrder fe, FlowEntryInstall newEntry) {
2803 this.newEntry = newEntry;
2809 public FlowEntryDistributionOrder getFe() {
2814 * @return the newEntry
2816 public FlowEntryInstall getNewEntry() {
2820 private class ContainerFlowChangeEvent extends FRMEvent {
2821 private final ContainerFlow previous;
2822 private final ContainerFlow current;
2823 private final UpdateType type;
2825 public ContainerFlowChangeEvent(ContainerFlow previous, ContainerFlow current, UpdateType type) {
2826 this.previous = previous;
2827 this.current = current;
2831 public ContainerFlow getPrevious() {
2832 return this.previous;
2835 public ContainerFlow getCurrent() {
2836 return this.current;
2839 public UpdateType getType() {
2845 private class WorkStatusCleanup extends FRMEvent {
2846 private FlowEntryDistributionOrder fe;
2851 WorkStatusCleanup(FlowEntryDistributionOrder fe) {
2858 public FlowEntryDistributionOrder getFe() {
2863 private class UpdateIndexDBs extends FRMEvent {
2864 private FlowEntryInstall fei;
2865 private boolean add;
2869 * @param fei the flow entry which was installed/removed on the netwrok node
2872 UpdateIndexDBs(FlowEntryInstall fei, boolean add) {
2879 * @return the flowEntryInstall object which was added/removed
2880 * to/from the installed software view cache
2882 public FlowEntryInstall getFei() {
2888 * @return whether this was an flow addition or removal
2890 public boolean isAddition() {
2899 public String getHelp() {
2900 StringBuffer help = new StringBuffer();
2901 return help.toString();
2905 public Status saveConfiguration() {
2906 return saveConfig();
2909 public void _frmNodeFlows(CommandInterpreter ci) {
2910 String nodeId = ci.nextArgument();
2911 Node node = Node.fromString(nodeId);
2913 ci.println("frmNodeFlows <node> [verbose]");
2916 boolean verbose = false;
2917 String verboseCheck = ci.nextArgument();
2918 if (verboseCheck != null) {
2919 verbose = verboseCheck.equals("true");
2922 if (!nodeFlows.containsKey(node)) {
2925 // Dump per node database
2926 for (FlowEntryInstall entry : nodeFlows.get(node)) {
2928 ci.println(node + " " + installedSwView.get(entry).getFlowName());
2930 ci.println(node + " " + installedSwView.get(entry).toString());
2935 public void _frmGroupFlows(CommandInterpreter ci) {
2936 String group = ci.nextArgument();
2937 if (group == null) {
2938 ci.println("frmGroupFlows <group> [verbose]");
2941 boolean verbose = false;
2942 String verboseCheck = ci.nextArgument();
2943 if (verboseCheck != null) {
2944 verbose = verboseCheck.equalsIgnoreCase("true");
2947 if (!groupFlows.containsKey(group)) {
2950 // Dump per node database
2951 ci.println("Group " + group + ":\n");
2952 for (FlowEntryInstall flowEntry : groupFlows.get(group)) {
2954 ci.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
2956 ci.println(flowEntry.getNode() + " " + flowEntry.toString());
2961 public void _frmProcessErrorEvent(CommandInterpreter ci) throws UnknownHostException {
2964 String nodeId = ci.nextArgument();
2965 if (nodeId == null) {
2966 ci.print("Node id not specified");
2969 String requestId = ci.nextArgument();
2970 if (requestId == null) {
2971 ci.print("Request id not specified");
2975 node = NodeCreator.createOFNode(Long.valueOf(nodeId));
2976 } catch (NumberFormatException e) {
2977 ci.print("Node id not a number");
2981 reqId = Long.parseLong(requestId);
2982 } catch (NumberFormatException e) {
2983 ci.print("Request id not a number");
2986 // null for error object is good enough for now
2987 ErrorReportedEvent event = new ErrorReportedEvent(reqId, node, null);
2988 this.processErrorEvent(event);
2992 public void flowRemoved(Node node, Flow flow) {
2993 log.trace("Received flow removed notification on {} for {}", node, flow);
2995 // For flow entry identification, only node, match and priority matter
2996 FlowEntryInstall test = new FlowEntryInstall(new FlowEntry("", "", flow, node), null);
2997 FlowEntryInstall installedEntry = this.installedSwView.get(test);
2998 if (installedEntry == null) {
2999 log.trace("Entry is not known to us");
3003 // Update Static flow status
3005 FlowConfig target = null;
3006 for (Map.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
3007 FlowConfig conf = entry.getValue();
3008 if (conf.isByNameAndNodeIdEqual(installedEntry.getFlowName(), node)) {
3009 key = entry.getKey();
3014 if (target != null) {
3015 // Update Configuration database
3016 target.toggleInstallation();
3017 target.setStatus(StatusCode.SUCCESS.toString());
3018 staticFlows.put(key, target);
3021 // Update software views
3022 this.updateSwViews(installedEntry, false);
3026 public void flowErrorReported(Node node, long rid, Object err) {
3027 log.trace("Got error {} for message rid {} from node {}", new Object[] { err, rid, node });
3028 pendingEvents.offer(new ErrorReportedEvent(rid, node, err));
3031 private void processErrorEvent(ErrorReportedEvent event) {
3032 Node node = event.getNode();
3033 long rid = event.getRequestId();
3034 Object error = event.getError();
3035 String errorString = (error == null) ? "Not provided" : error.toString();
3037 * If this was for a flow install, remove the corresponding entry from
3038 * the software view. If it was a Looking for the rid going through the
3039 * software database. TODO: A more efficient rid <-> FlowEntryInstall
3040 * mapping will have to be added in future
3042 FlowEntryInstall target = null;
3043 List<FlowEntryInstall> flowEntryInstallList = nodeFlows.get(node);
3044 // flowEntryInstallList could be null.
3046 if(flowEntryInstallList != null) {
3047 for (FlowEntryInstall index : flowEntryInstallList) {
3048 FlowEntryInstall entry = installedSwView.get(index);
3050 if (entry.getRequestId() == rid) {
3057 if (target != null) {
3058 // This was a flow install, update database
3059 this.updateSwViews(target, false);
3060 // also update the config
3061 if(FlowConfig.STATICFLOWGROUP.equals(target.getGroupName())) {
3062 ConcurrentMap.Entry<Integer, FlowConfig> staticFlowEntry = getStaticFlowEntry(target.getFlowName(),target.getNode());
3063 // staticFlowEntry should never be null.
3064 // the null check is just an extra defensive check.
3065 if(staticFlowEntry != null) {
3066 staticFlows.remove(staticFlowEntry.getKey());
3072 if (frmAware != null) {
3073 synchronized (frmAware) {
3074 for (IForwardingRulesManagerAware frma : frmAware) {
3076 frma.requestFailed(rid, errorString);
3077 } catch (Exception e) {
3078 log.warn("Failed to notify {}", frma);
3086 public Status solicitStatusResponse(Node node, boolean blocking) {
3087 Status rv = new Status(StatusCode.INTERNALERROR);
3089 if (this.programmer != null) {
3091 rv = programmer.syncSendBarrierMessage(node);
3093 rv = programmer.asyncSendBarrierMessage(node);
3100 public void unsetIConnectionManager(IConnectionManager s) {
3101 if (s == this.connectionManager) {
3102 this.connectionManager = null;
3106 public void setIConnectionManager(IConnectionManager s) {
3107 this.connectionManager = s;
3110 public void unsetIContainerManager(IContainerManager s) {
3111 if (s == this.containerManager) {
3112 this.containerManager = null;
3116 public void setIContainerManager(IContainerManager s) {
3117 this.containerManager = s;
3121 public void entryCreated(Object key, String cacheName, boolean originLocal) {
3128 public void entryUpdated(Object key, Object new_value, String cacheName, boolean originLocal) {
3130 * Streamline the updates for the per node and per group index databases
3132 if (cacheName.equals(INSTALLED_SW_VIEW_CACHE)) {
3133 pendingEvents.offer(new UpdateIndexDBs((FlowEntryInstall)key, true));
3138 * Local updates are of no interest
3142 if (cacheName.equals(WORK_ORDER_CACHE)) {
3143 logsync.trace("Got a WorkOrderCacheUpdate for {}", key);
3145 * This is the case of one workOrder becoming available, so we need
3146 * to dispatch the work to the appropriate handler
3148 FlowEntryDistributionOrder fe = (FlowEntryDistributionOrder) key;
3149 FlowEntryInstall fei = fe.getEntry();
3153 Node n = fei.getNode();
3154 if (connectionManager.getLocalityStatus(n) == ConnectionLocality.LOCAL) {
3155 logsync.trace("workOrder for fe {} processed locally", fe);
3156 // I'm the controller in charge for the request, queue it for
3158 pendingEvents.offer(new WorkOrderEvent(fe, (FlowEntryInstall) new_value));
3160 } else if (cacheName.equals(WORK_STATUS_CACHE)) {
3161 logsync.trace("Got a WorkStatusCacheUpdate for {}", key);
3163 * This is the case of one workOrder being completed and a status
3166 FlowEntryDistributionOrder fe = (FlowEntryDistributionOrder) key;
3168 * Check if the order was initiated by this controller in that case
3169 * we need to actually look at the status returned
3171 if (fe.getRequestorController()
3172 .equals(clusterContainerService.getMyAddress())) {
3173 FlowEntryDistributionOrderFutureTask fet = workMonitor.remove(fe);
3175 logsync.trace("workStatus response is for us {}", fe);
3176 // Signal we got the status
3177 fet.gotStatus(fe, workStatus.get(fe));
3178 pendingEvents.offer(new WorkStatusCleanup(fe));
3185 public void entryDeleted(Object key, String cacheName, boolean originLocal) {
3187 * Streamline the updates for the per node and per group index databases
3189 if (cacheName.equals(INSTALLED_SW_VIEW_CACHE)) {
3190 pendingEvents.offer(new UpdateIndexDBs((FlowEntryInstall)key, false));
3198 public List<FlowEntry> getFlowEntriesForNode(Node node) {
3199 List<FlowEntry> list = new ArrayList<FlowEntry>();
3201 for (Map.Entry<FlowEntry, FlowEntry> entry : this.originalSwView.entrySet()) {
3202 if (node.equals(entry.getKey().getNode())) {
3203 list.add(entry.getKey().clone());
3214 public List<FlowEntry> getInstalledFlowEntriesForNode(Node node) {
3215 List<FlowEntry> list = new ArrayList<FlowEntry>();
3217 List<FlowEntryInstall> flowEntryInstallList = this.nodeFlows.get(node);
3218 if(flowEntryInstallList != null) {
3219 for(FlowEntryInstall fi: flowEntryInstallList) {
3220 list.add(fi.getInstall().clone());