Merge "Remove unused code"
[controller.git] / opendaylight / forwardingrulesmanager / implementation / src / main / java / org / opendaylight / controller / forwardingrulesmanager / internal / ForwardingRulesManager.java
index bbc6048d8465f86f2678bc72861895a224d14955..f7b647dd721a1f1f9117d95bc3723867574e5064 100644 (file)
@@ -11,8 +11,6 @@ package org.opendaylight.controller.forwardingrulesmanager.internal;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.ObjectInputStream;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.EnumSet;
@@ -30,14 +28,14 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 
-import org.eclipse.osgi.framework.console.CommandInterpreter;
-import org.eclipse.osgi.framework.console.CommandProvider;
 import org.opendaylight.controller.clustering.services.CacheConfigException;
 import org.opendaylight.controller.clustering.services.CacheExistException;
 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
 import org.opendaylight.controller.clustering.services.IClusterServices;
+import org.opendaylight.controller.configuration.ConfigurationObject;
 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
+import org.opendaylight.controller.configuration.IConfigurationContainerService;
 import org.opendaylight.controller.connectionmanager.IConnectionManager;
 import org.opendaylight.controller.containermanager.IContainerManager;
 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
@@ -52,10 +50,10 @@ import org.opendaylight.controller.forwardingrulesmanager.PortGroupProvider;
 import org.opendaylight.controller.forwardingrulesmanager.implementation.data.FlowEntryDistributionOrder;
 import org.opendaylight.controller.sal.action.Action;
 import org.opendaylight.controller.sal.action.ActionType;
-import org.opendaylight.controller.sal.action.Controller;
+import org.opendaylight.controller.sal.action.Enqueue;
 import org.opendaylight.controller.sal.action.Flood;
+import org.opendaylight.controller.sal.action.FloodAll;
 import org.opendaylight.controller.sal.action.Output;
-import org.opendaylight.controller.sal.action.PopVlan;
 import org.opendaylight.controller.sal.connection.ConnectionLocality;
 import org.opendaylight.controller.sal.core.Config;
 import org.opendaylight.controller.sal.core.ContainerFlow;
@@ -72,21 +70,13 @@ import org.opendaylight.controller.sal.match.Match;
 import org.opendaylight.controller.sal.match.MatchType;
 import org.opendaylight.controller.sal.utils.EtherTypes;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
-import org.opendaylight.controller.sal.utils.HexEncode;
 import org.opendaylight.controller.sal.utils.IObjectReader;
-import org.opendaylight.controller.sal.utils.IPProtocols;
-import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
-import org.opendaylight.controller.sal.utils.NodeCreator;
-import org.opendaylight.controller.sal.utils.ObjectReader;
-import org.opendaylight.controller.sal.utils.ObjectWriter;
 import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.controller.sal.utils.StatusCode;
 import org.opendaylight.controller.switchmanager.IInventoryListener;
 import org.opendaylight.controller.switchmanager.ISwitchManager;
 import org.opendaylight.controller.switchmanager.ISwitchManagerAware;
 import org.opendaylight.controller.switchmanager.Subnet;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -104,22 +94,22 @@ public class ForwardingRulesManager implements
         IInventoryListener,
         IObjectReader,
         ICacheUpdateAware<Object,Object>,
-        CommandProvider,
         IFlowProgrammerListener {
 
     private static final Logger log = LoggerFactory.getLogger(ForwardingRulesManager.class);
     private static final Logger logsync = LoggerFactory.getLogger("FRMsync");
-    private static final String PORTREMOVED = "Port removed";
-    private static final String NODEDOWN = "Node is Down";
+    private static final String PORT_REMOVED = "Port removed";
+    private static final String NODE_DOWN = "Node is Down";
     private static final String INVALID_FLOW_ENTRY = "Invalid FlowEntry";
-    private String frmFileName;
-    private String portGroupFileName;
+    private static final String STATIC_FLOWS_FILE_NAME = "frm_staticflows.conf";
+    private static final String PORT_GROUP_FILE_NAME = "portgroup.conf";
     private ConcurrentMap<Integer, FlowConfig> staticFlows;
     private ConcurrentMap<Integer, Integer> staticFlowsOrdinal;
     private ConcurrentMap<String, PortGroupConfig> portGroupConfigs;
     private ConcurrentMap<PortGroupConfig, Map<Node, PortGroup>> portGroupData;
     private ConcurrentMap<String, Object> TSPolicies;
     private IContainerManager containerManager;
+    private IConfigurationContainerService configurationService;
     private boolean inContainerMode; // being used by global instance only
     protected boolean stopping;
 
@@ -167,6 +157,8 @@ public class ForwardingRulesManager implements
      */
     static final String WORK_ORDER_CACHE = "frm.workOrder";
     static final String WORK_STATUS_CACHE = "frm.workStatus";
+    static final String ORIGINAL_SW_VIEW_CACHE = "frm.originalSwView";
+    static final String INSTALLED_SW_VIEW_CACHE = "frm.installedSwView";
 
     /*
      * Data structure responsible for distributing the FlowEntryInstall requests
@@ -254,6 +246,57 @@ public class ForwardingRulesManager implements
         return null;
     }
 
+    /**
+     * Checks if the FlowEntry targets are valid for this container
+     *
+     * @param flowEntry
+     *            The flow entry to test
+     * @return a Status object representing the result of the validation
+     */
+    private Status validateEntry(FlowEntry flowEntry) {
+        // Node presence check
+        Node node = flowEntry.getNode();
+        if (!switchManager.getNodes().contains(node)) {
+            return new Status(StatusCode.BADREQUEST, String.format("Node %s is not present in this container", node));
+        }
+
+        // Ports and actions validation check
+        Flow flow = flowEntry.getFlow();
+        Match match = flow.getMatch();
+        if (match.isPresent(MatchType.IN_PORT)) {
+            NodeConnector inputPort = (NodeConnector)match.getField(MatchType.IN_PORT).getValue();
+            if (!switchManager.getNodeConnectors(node).contains(inputPort)) {
+                String msg = String.format("Ingress port %s is not present on this container", inputPort);
+                return new Status(StatusCode.BADREQUEST, msg);
+            }
+        }
+        for (Action action : flow.getActions()) {
+            if (action instanceof Flood && !GlobalConstants.DEFAULT.toString().equals(getContainerName())) {
+                return new Status(StatusCode.BADREQUEST, String.format("Flood is only allowed in default container"));
+            }
+            if (action instanceof FloodAll && !GlobalConstants.DEFAULT.toString().equals(getContainerName())) {
+                return new Status(StatusCode.BADREQUEST, String.format("FloodAll is only allowed in default container"));
+            }
+            if (action instanceof Output) {
+                Output out = (Output)action;
+                NodeConnector outputPort = out.getPort();
+                if (!switchManager.getNodeConnectors(node).contains(outputPort)) {
+                    String msg = String.format("Output port %s is not present on this container", outputPort);
+                    return new Status(StatusCode.BADREQUEST, msg);
+                }
+            }
+            if (action instanceof Enqueue) {
+                Enqueue out = (Enqueue)action;
+                NodeConnector outputPort = out.getPort();
+                if (!switchManager.getNodeConnectors(node).contains(outputPort)) {
+                    String msg = String.format("Enqueue port %s is not present on this container", outputPort);
+                    return new Status(StatusCode.BADREQUEST, msg);
+                }
+            }
+        }
+        return new Status(StatusCode.SUCCESS);
+    }
+
     /**
      * Adds a flow entry onto the network node It runs various validity checks
      * and derive the final container flows merged entries that will be
@@ -275,6 +318,15 @@ public class ForwardingRulesManager implements
             return new Status(StatusCode.NOTACCEPTABLE, INVALID_FLOW_ENTRY);
         }
 
+        // Operational check: input, output and queue ports presence check and
+        // action validation for this container
+        Status status = validateEntry(flowEntry);
+        if (!status.isSuccess()) {
+            String msg = String.format("%s: %s", INVALID_FLOW_ENTRY, status.getDescription());
+            log.warn("{}: {}", msg, flowEntry);
+            return new Status(StatusCode.NOTACCEPTABLE, msg);
+        }
+
         /*
          * Redundant Check: Check if the request is a redundant one from the
          * same application the flowEntry is equal to an existing one. Given we
@@ -342,7 +394,7 @@ public class ForwardingRulesManager implements
         for (FlowEntryInstall installEntry : toInstallSafe) {
 
             // Install and update database
-            Status ret = addEntriesInternal(installEntry, async);
+            Status ret = addEntryInternal(installEntry, async);
 
             if (ret.isSuccess()) {
                 oneSucceded = true;
@@ -433,6 +485,15 @@ public class ForwardingRulesManager implements
             return new Status(StatusCode.SUCCESS, msg);
         }
 
+        // Operational check: input, output and queue ports presence check and
+        // action validation for this container
+        Status status = validateEntry(newFlowEntry);
+        if (!status.isSuccess()) {
+            String msg = String.format("Modify: %s: %s", INVALID_FLOW_ENTRY, status.getDescription());
+            log.warn("{}: {}", msg, newFlowEntry);
+            return new Status(StatusCode.NOTACCEPTABLE, msg);
+        }
+
         /*
          * Conflict Check: Verify the new entry would not conflict with an
          * existing one. This is a loose check on the previous original flow
@@ -479,7 +540,7 @@ public class ForwardingRulesManager implements
         Status succeeded = null;
         boolean decouple = false;
         if (installedList.size() != toInstallList.size()) {
-            log.info("Modify: New flow entry does not satisfy the same "
+            log.trace("Modify: New flow entry does not satisfy the same "
                     + "number of container flows as the original entry does");
             decouple = true;
         }
@@ -491,7 +552,7 @@ public class ForwardingRulesManager implements
              */
             FlowEntryInstall sameMatchEntry = installedSwView.get(installEntry);
             if (sameMatchEntry != null && !sameMatchEntry.getOriginal().equals(currentFlowEntry)) {
-                log.info("Modify: new container flow merged flow entry clashes with existing flow");
+                log.trace("Modify: new container flow merged flow entry clashes with existing flow");
                 decouple = true;
             } else {
                 toInstallSafe.add(installEntry);
@@ -505,7 +566,7 @@ public class ForwardingRulesManager implements
             }
             // Install new entries
             for (FlowEntryInstall newEntry : toInstallSafe) {
-                succeeded = this.addEntriesInternal(newEntry, async);
+                succeeded = this.addEntryInternal(newEntry, async);
             }
         } else {
             /*
@@ -565,7 +626,9 @@ public class ForwardingRulesManager implements
     /**
      * This is the function that modifies the final container flows merged
      * entries on the network node and update the database. It expects that all
-     * the validity checks are passed
+     * the validity checks are passed.
+     * This function is supposed to be called only on the controller on which
+     * the IFRM call is executed.
      *
      * @param currentEntries
      * @param newEntries
@@ -575,13 +638,13 @@ public class ForwardingRulesManager implements
      *         contain the unique id assigned to this request
      */
     private Status modifyEntryInternal(FlowEntryInstall currentEntries, FlowEntryInstall newEntries, boolean async) {
+        Status status = new Status(StatusCode.UNDEFINED);
         FlowEntryDistributionOrderFutureTask futureStatus =
                 distributeWorkOrder(currentEntries, newEntries, UpdateType.CHANGED);
         if (futureStatus != null) {
-            Status retStatus = new Status(StatusCode.UNDEFINED);
             try {
-                retStatus = futureStatus.get();
-                if (retStatus.getCode()
+                status = futureStatus.get();
+                if (status.getCode()
                         .equals(StatusCode.TIMEOUT)) {
                     // A timeout happened, lets cleanup the workMonitor
                     workMonitor.remove(futureStatus.getOrder());
@@ -591,30 +654,31 @@ public class ForwardingRulesManager implements
             } catch (ExecutionException e) {
                 log.error("", e);
             }
-            return retStatus;
         } else {
             // Modify the flow on the network node
-            Status status = async ? programmer.modifyFlowAsync(currentEntries.getNode(), currentEntries.getInstall()
-                    .getFlow(), newEntries.getInstall()
-                    .getFlow()) : programmer.modifyFlow(currentEntries.getNode(), currentEntries.getInstall()
-                    .getFlow(), newEntries.getInstall()
-                    .getFlow());
+            status = modifyEntryInHw(currentEntries, newEntries, async);
+        }
 
-            if (!status.isSuccess()) {
-                log.trace("SDN Plugin failed to program the flow: {}. The failure is: {}", newEntries.getInstall(),
-                        status.getDescription());
-                return status;
-            }
+        if (!status.isSuccess()) {
+            log.trace("{} SDN Plugin failed to program the flow: {}. The failure is: {}",
+                    (futureStatus != null) ? "Remote" : "Local", newEntries.getInstall(), status.getDescription());
+            return status;
+        }
 
-            log.trace("Modified {} => {}", currentEntries.getInstall(), newEntries.getInstall());
+        log.trace("Modified {} => {}", currentEntries.getInstall(), newEntries.getInstall());
 
-            // Update DB
-            newEntries.setRequestId(status.getRequestId());
-            updateLocalDatabase(currentEntries, false);
-            updateLocalDatabase(newEntries, true);
+        // Update DB
+        newEntries.setRequestId(status.getRequestId());
+        updateSwViews(currentEntries, false);
+        updateSwViews(newEntries, true);
 
-            return status;
-        }
+        return status;
+    }
+
+    private Status modifyEntryInHw(FlowEntryInstall currentEntries, FlowEntryInstall newEntries, boolean async) {
+        return async ? programmer.modifyFlowAsync(currentEntries.getNode(), currentEntries.getInstall().getFlow(),
+                newEntries.getInstall().getFlow()) : programmer.modifyFlow(currentEntries.getNode(), currentEntries
+                .getInstall().getFlow(), newEntries.getInstall().getFlow());
     }
 
     /**
@@ -683,6 +747,8 @@ public class ForwardingRulesManager implements
      * This is the function that removes the final container flows merged entry
      * from the network node and update the database. It expects that all the
      * validity checks are passed
+     * This function is supposed to be called only on the controller on which
+     * the IFRM call is executed.
      *
      * @param entry
      *            the flow entry to remove
@@ -692,13 +758,12 @@ public class ForwardingRulesManager implements
      *         contain the unique id assigned to this request
      */
     private Status removeEntryInternal(FlowEntryInstall entry, boolean async) {
+        Status status = new Status(StatusCode.UNDEFINED);
         FlowEntryDistributionOrderFutureTask futureStatus = distributeWorkOrder(entry, null, UpdateType.REMOVED);
         if (futureStatus != null) {
-            Status retStatus = new Status(StatusCode.UNDEFINED);
             try {
-                retStatus = futureStatus.get();
-                if (retStatus.getCode()
-                        .equals(StatusCode.TIMEOUT)) {
+                status = futureStatus.get();
+                if (status.getCode().equals(StatusCode.TIMEOUT)) {
                     // A timeout happened, lets cleanup the workMonitor
                     workMonitor.remove(futureStatus.getOrder());
                 }
@@ -707,28 +772,31 @@ public class ForwardingRulesManager implements
             } catch (ExecutionException e) {
                 log.error("", e);
             }
-            return retStatus;
         } else {
             // Mark the entry to be deleted (for CC just in case we fail)
             entry.toBeDeleted();
 
             // Remove from node
-            Status status = async ? programmer.removeFlowAsync(entry.getNode(), entry.getInstall()
-                    .getFlow()) : programmer.removeFlow(entry.getNode(), entry.getInstall()
-                    .getFlow());
-
-            if (!status.isSuccess()) {
-                log.trace("SDN Plugin failed to remove the flow: {}. The failure is: {}", entry.getInstall(),
-                        status.getDescription());
-                return status;
-            }
-            log.trace("Removed  {}", entry.getInstall());
-
-            // Update DB
-            updateLocalDatabase(entry, false);
+            status = removeEntryInHw(entry, async);
+        }
 
+        if (!status.isSuccess()) {
+            log.trace("{} SDN Plugin failed to remove the flow: {}. The failure is: {}",
+                    (futureStatus != null) ? "Remote" : "Local", entry.getInstall(), status.getDescription());
             return status;
         }
+
+        log.trace("Removed  {}", entry.getInstall());
+
+        // Update DB
+        updateSwViews(entry, false);
+
+        return status;
+    }
+
+    private Status removeEntryInHw(FlowEntryInstall entry, boolean async) {
+        return async ? programmer.removeFlowAsync(entry.getNode(), entry.getInstall().getFlow()) : programmer
+                .removeFlow(entry.getNode(), entry.getInstall().getFlow());
     }
 
     /**
@@ -736,6 +804,8 @@ public class ForwardingRulesManager implements
      * on the network node and updates the database. It expects that all the
      * validity and conflict checks are passed. That means it does not check
      * whether this flow would conflict or overwrite an existing one.
+     * This function is supposed to be called only on the controller on which
+     * the IFRM call is executed.
      *
      * @param entry
      *            the flow entry to install
@@ -744,14 +814,13 @@ public class ForwardingRulesManager implements
      * @return the status of this request. In case of asynchronous call, it will
      *         contain the unique id assigned to this request
      */
-    private Status addEntriesInternal(FlowEntryInstall entry, boolean async) {
+    private Status addEntryInternal(FlowEntryInstall entry, boolean async) {
+        Status status = new Status(StatusCode.UNDEFINED);
         FlowEntryDistributionOrderFutureTask futureStatus = distributeWorkOrder(entry, null, UpdateType.ADDED);
         if (futureStatus != null) {
-            Status retStatus = new Status(StatusCode.UNDEFINED);
             try {
-                retStatus = futureStatus.get();
-                if (retStatus.getCode()
-                        .equals(StatusCode.TIMEOUT)) {
+                status = futureStatus.get();
+                if (status.getCode().equals(StatusCode.TIMEOUT)) {
                     // A timeout happened, lets cleanup the workMonitor
                     workMonitor.remove(futureStatus.getOrder());
                 }
@@ -760,27 +829,29 @@ public class ForwardingRulesManager implements
             } catch (ExecutionException e) {
                 log.error("", e);
             }
-            return retStatus;
         } else {
-            // Install the flow on the network node
-            Status status = async ? programmer.addFlowAsync(entry.getNode(), entry.getInstall()
-                    .getFlow()) : programmer.addFlow(entry.getNode(), entry.getInstall()
-                    .getFlow());
+            status = addEntryInHw(entry, async);
+        }
 
-            if (!status.isSuccess()) {
-                log.trace("SDN Plugin failed to program the flow: {}. The failure is: {}", entry.getInstall(),
-                        status.getDescription());
-                return status;
-            }
+        if (!status.isSuccess()) {
+            log.trace("{} SDN Plugin failed to program the flow: {}. The failure is: {}",
+                    (futureStatus != null) ? "Remote" : "Local", entry.getInstall(), status.getDescription());
+            return status;
+        }
 
-            log.trace("Added    {}", entry.getInstall());
+        log.trace("Added    {}", entry.getInstall());
 
-            // Update DB
-            entry.setRequestId(status.getRequestId());
-            updateLocalDatabase(entry, true);
+        // Update DB
+        entry.setRequestId(status.getRequestId());
+        updateSwViews(entry, true);
 
-            return status;
-        }
+        return status;
+    }
+
+    private Status addEntryInHw(FlowEntryInstall entry, boolean async) {
+        // Install the flow on the network node
+        return async ? programmer.addFlowAsync(entry.getNode(), entry.getInstall().getFlow()) : programmer.addFlow(
+                entry.getNode(), entry.getInstall().getFlow());
     }
 
     /**
@@ -822,10 +893,7 @@ public class ForwardingRulesManager implements
         return null;
     }
 
-    private void updateLocalDatabase(FlowEntryInstall entry, boolean add) {
-        // Update the software view
-        updateSwViewes(entry, add);
-
+    private void updateIndexDatabase(FlowEntryInstall entry, boolean add) {
         // Update node indexed flow database
         updateNodeFlowsDB(entry, add);
 
@@ -836,7 +904,7 @@ public class ForwardingRulesManager implements
     /*
      * Update the node mapped flows database
      */
-    private void updateSwViewes(FlowEntryInstall flowEntries, boolean add) {
+    private void updateSwViews(FlowEntryInstall flowEntries, boolean add) {
         if (add) {
             originalSwView.put(flowEntries.getOriginal(), flowEntries.getOriginal());
             installedSwView.put(flowEntries, flowEntries);
@@ -862,6 +930,17 @@ public class ForwardingRulesManager implements
         }
 
         if (add) {
+            // there may be an already existing entry.
+            // remove it before adding the new one.
+            // This is necessary since we have observed that in some cases
+            // Infinispan does aggregation for operations (eg:- remove and then put a different value)
+            // related to the same key within the same transaction.
+            // Need this defensive code as the new FlowEntryInstall may be different
+            // than the old one even though the equals method returns true. This is because
+            // the equals method does not take into account the action list.
+            if(nodeIndeces.contains(flowEntries)) {
+                nodeIndeces.remove(flowEntries);
+            }
             nodeIndeces.add(flowEntries);
         } else {
             nodeIndeces.remove(flowEntries);
@@ -896,6 +975,11 @@ public class ForwardingRulesManager implements
         }
 
         if (add) {
+            // same comments in the similar code section in
+            // updateNodeFlowsDB method apply here too
+            if(indices.contains(flowEntries)) {
+                indices.remove(flowEntries);
+            }
             indices.add(flowEntries);
         } else {
             indices.remove(flowEntries);
@@ -935,7 +1019,7 @@ public class ForwardingRulesManager implements
 
         // Update DB
         if (status.isSuccess()) {
-            updateLocalDatabase(target, false);
+            updateSwViews(target, false);
         } else {
             // log the error
             log.trace("SDN Plugin failed to remove the flow: {}. The failure is: {}", target.getInstall(),
@@ -1109,7 +1193,10 @@ public class ForwardingRulesManager implements
             List<FlowEntryInstall> list = new ArrayList<FlowEntryInstall>(groupFlows.get(groupName));
             toBeRemoved = list.size();
             for (FlowEntryInstall entry : list) {
-                Status status = this.removeEntry(entry.getOriginal(), false);
+                // since this is the entry that was stored in groupFlows
+                // it is already validated and merged
+                // so can call removeEntryInternal directly
+                Status status = this.removeEntryInternal(entry, false);
                 if (status.isSuccess()) {
                     toBeRemoved -= 1;
                 } else {
@@ -1178,8 +1265,6 @@ public class ForwardingRulesManager implements
     private void nonClusterObjectCreate() {
         originalSwView = new ConcurrentHashMap<FlowEntry, FlowEntry>();
         installedSwView = new ConcurrentHashMap<FlowEntryInstall, FlowEntryInstall>();
-        nodeFlows = new ConcurrentHashMap<Node, List<FlowEntryInstall>>();
-        groupFlows = new ConcurrentHashMap<String, List<FlowEntryInstall>>();
         TSPolicies = new ConcurrentHashMap<String, Object>();
         staticFlowsOrdinal = new ConcurrentHashMap<Integer, Integer>();
         portGroupConfigs = new ConcurrentHashMap<String, PortGroupConfig>();
@@ -1188,11 +1273,6 @@ public class ForwardingRulesManager implements
         inactiveFlows = new ConcurrentHashMap<FlowEntry, FlowEntry>();
     }
 
-    private void registerWithOSGIConsole() {
-        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
-        bundleContext.registerService(CommandProvider.class.getName(), this, null);
-    }
-
     @Override
     public void setTSPolicyData(String policyname, Object o, boolean add) {
 
@@ -1237,7 +1317,7 @@ public class ForwardingRulesManager implements
         if (policyName != null && !policyName.trim().isEmpty()) {
             for (Map.Entry<FlowEntry, FlowEntry> entry : this.originalSwView.entrySet()) {
                 if (policyName.equals(entry.getKey().getGroupName())) {
-                    list.add(entry.getKey().clone());
+                    list.add(entry.getValue().clone());
                 }
             }
         }
@@ -1250,7 +1330,7 @@ public class ForwardingRulesManager implements
         if (policyName != null && !policyName.trim().isEmpty()) {
             for (Map.Entry<FlowEntryInstall, FlowEntryInstall> entry : this.installedSwView.entrySet()) {
                 if (policyName.equals(entry.getKey().getGroupName())) {
-                    list.add(entry.getKey().getInstall().clone());
+                    list.add(entry.getValue().getInstall().clone());
                 }
             }
         }
@@ -1269,7 +1349,7 @@ public class ForwardingRulesManager implements
                 }
                 Status error = modifyEntry(currentFlowEntry, newFlowEntry, false);
                 if (error.isSuccess()) {
-                    log.info("Ports {} added to FlowEntry {}", portList, flowName);
+                    log.trace("Ports {} added to FlowEntry {}", portList, flowName);
                 } else {
                     log.warn("Failed to add ports {} to Flow entry {}. The failure is: {}", portList,
                             currentFlowEntry.toString(), error.getDescription());
@@ -1293,7 +1373,7 @@ public class ForwardingRulesManager implements
                 }
                 Status status = modifyEntry(currentFlowEntry, newFlowEntry, false);
                 if (status.isSuccess()) {
-                    log.info("Ports {} removed from FlowEntry {}", portList, flowName);
+                    log.trace("Ports {} removed from FlowEntry {}", portList, flowName);
                 } else {
                     log.warn("Failed to remove ports {} from Flow entry {}. The failure is: {}", portList,
                             currentFlowEntry.toString(), status.getDescription());
@@ -1341,7 +1421,7 @@ public class ForwardingRulesManager implements
         Status status = modifyEntry(currentFlowEntry, newFlowEntry, false);
 
         if (status.isSuccess()) {
-            log.info("Output port replaced with {} for flow {} on node {}", outPort, flowName, node);
+            log.trace("Output port replaced with {} for flow {} on node {}", outPort, flowName, node);
         } else {
             log.warn("Failed to replace output port for flow {} on node {}. The failure is: {}", flowName, node,
                     status.getDescription());
@@ -1378,21 +1458,15 @@ public class ForwardingRulesManager implements
         log.debug("Allocating caches for Container {}", container.getName());
 
         try {
-            clusterContainerService.createCache("frm.originalSwView",
+            clusterContainerService.createCache(ORIGINAL_SW_VIEW_CACHE,
                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
 
-            clusterContainerService.createCache("frm.installedSwView",
+            clusterContainerService.createCache(INSTALLED_SW_VIEW_CACHE,
                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
 
             clusterContainerService.createCache("frm.inactiveFlows",
                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
 
-            clusterContainerService.createCache("frm.nodeFlows",
-                    EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
-
-            clusterContainerService.createCache("frm.groupFlows",
-                    EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
-
             clusterContainerService.createCache("frm.staticFlows",
                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
 
@@ -1433,14 +1507,14 @@ public class ForwardingRulesManager implements
 
         log.debug("Retrieving Caches for Container {}", container.getName());
 
-        map = clusterContainerService.getCache("frm.originalSwView");
+        map = clusterContainerService.getCache(ORIGINAL_SW_VIEW_CACHE);
         if (map != null) {
             originalSwView = (ConcurrentMap<FlowEntry, FlowEntry>) map;
         } else {
             log.error("Retrieval of frm.originalSwView cache failed for Container {}", container.getName());
         }
 
-        map = clusterContainerService.getCache("frm.installedSwView");
+        map = clusterContainerService.getCache(INSTALLED_SW_VIEW_CACHE);
         if (map != null) {
             installedSwView = (ConcurrentMap<FlowEntryInstall, FlowEntryInstall>) map;
         } else {
@@ -1454,20 +1528,6 @@ public class ForwardingRulesManager implements
             log.error("Retrieval of frm.inactiveFlows cache failed for Container {}", container.getName());
         }
 
-        map = clusterContainerService.getCache("frm.nodeFlows");
-        if (map != null) {
-            nodeFlows = (ConcurrentMap<Node, List<FlowEntryInstall>>) map;
-        } else {
-            log.error("Retrieval of cache failed for Container {}", container.getName());
-        }
-
-        map = clusterContainerService.getCache("frm.groupFlows");
-        if (map != null) {
-            groupFlows = (ConcurrentMap<String, List<FlowEntryInstall>>) map;
-        } else {
-            log.error("Retrieval of frm.groupFlows cache failed for Container {}", container.getName());
-        }
-
         map = clusterContainerService.getCache("frm.staticFlows");
         if (map != null) {
             staticFlows = (ConcurrentMap<Integer, FlowConfig>) map;
@@ -1530,15 +1590,25 @@ public class ForwardingRulesManager implements
 
     @Override
     public Status addStaticFlow(FlowConfig config) {
+        return addStaticFlow(config, false);
+    }
+
+    private Status addStaticFlow(FlowConfig config, boolean async) {
         // Configuration object validation
-        Status status = config.validate(container);
+        Status status = config.validate();
         if (!status.isSuccess()) {
             log.warn("Invalid Configuration for flow {}. The failure is {}", config, status.getDescription());
             String error = "Invalid Configuration (" + status.getDescription() + ")";
             config.setStatus(error);
             return new Status(StatusCode.BADREQUEST, error);
         }
-        return addStaticFlowInternal(config, false);
+        return addStaticFlowInternal(config, async, false);
+    }
+
+
+    @Override
+    public Status addStaticFlowAsync(FlowConfig config) {
+        return addStaticFlow(config, true);
     }
 
     /**
@@ -1556,7 +1626,7 @@ public class ForwardingRulesManager implements
      *            installation on the network node was successful
      * @return The status of this request
      */
-    private Status addStaticFlowInternal(FlowConfig config, boolean restore) {
+    private Status addStaticFlowInternal(FlowConfig config, boolean async, boolean restore) {
         boolean multipleFlowPush = false;
         String error;
         Status status;
@@ -1593,7 +1663,7 @@ public class ForwardingRulesManager implements
             // Program hw
             if (config.installInHw()) {
                 FlowEntry entry = config.getFlowEntry();
-                status = this.installFlowEntry(entry);
+                status = async ? this.installFlowEntryAsync(entry) : this.installFlowEntry(entry);
                 if (!status.isSuccess()) {
                     config.setStatus(status.getDescription());
                     if (!restore) {
@@ -1665,7 +1735,7 @@ public class ForwardingRulesManager implements
                     // Take note of this controller generated static flow
                     toRemove.add(entry.getKey());
                 } else {
-                    config.setStatus(NODEDOWN);
+                    config.setStatus(NODE_DOWN);
                 }
             }
         }
@@ -1695,6 +1765,7 @@ public class ForwardingRulesManager implements
                     config.setStatus(StatusCode.SUCCESS.toString());
                     break;
                 default:
+                    break;
                 }
             }
         }
@@ -1704,6 +1775,15 @@ public class ForwardingRulesManager implements
 
     @Override
     public Status removeStaticFlow(FlowConfig config) {
+        return removeStaticFlow(config, false);
+    }
+
+    @Override
+    public Status removeStaticFlowAsync(FlowConfig config) {
+        return removeStaticFlow(config, true);
+    }
+
+    private Status removeStaticFlow(FlowConfig config, boolean async) {
         /*
          * No config.isInternal() check as NB does not take this path and GUI
          * cannot issue a delete on an internal generated flow. We need this
@@ -1727,7 +1807,8 @@ public class ForwardingRulesManager implements
         }
 
         // Program the network node
-        Status status = this.uninstallFlowEntry(config.getFlowEntry());
+        Status status = async ? this.uninstallFlowEntryAsync(config.getFlowEntry()) : this.uninstallFlowEntry(config
+                .getFlowEntry());
 
         // Update configuration database if programming was successful
         if (status.isSuccess()) {
@@ -1739,6 +1820,15 @@ public class ForwardingRulesManager implements
 
     @Override
     public Status removeStaticFlow(String name, Node node) {
+       return removeStaticFlow(name, node, false);
+    }
+
+    @Override
+    public Status removeStaticFlowAsync(String name, Node node) {
+        return removeStaticFlow(name, node, true);
+    }
+
+    private Status removeStaticFlow(String name, Node node, boolean async) {
         // Look for the target configuration entry
         Integer key = 0;
         FlowConfig target = null;
@@ -1769,7 +1859,7 @@ public class ForwardingRulesManager implements
         }
 
         // Program the network node
-        Status status = this.removeEntry(target.getFlowEntry(), false);
+        Status status = this.removeEntry(target.getFlowEntry(), async);
 
         // Update configuration database if programming was successful
         if (status.isSuccess()) {
@@ -1790,7 +1880,7 @@ public class ForwardingRulesManager implements
         }
 
         // Validity Check
-        Status status = newFlowConfig.validate(container);
+        Status status = newFlowConfig.validate();
         if (!status.isSuccess()) {
             String msg = "Invalid Configuration (" + status.getDescription() + ")";
             newFlowConfig.setStatus(msg);
@@ -1819,7 +1909,7 @@ public class ForwardingRulesManager implements
         // Do not attempt to reinstall the flow, warn user
         if (newFlowConfig.equals(oldFlowConfig)) {
             String msg = "No modification detected";
-            log.info("Static flow modification skipped. New flow and old flow are the same: {}", newFlowConfig);
+            log.trace("Static flow modification skipped. New flow and old flow are the same: {}", newFlowConfig);
             return new Status(StatusCode.SUCCESS, msg);
         }
 
@@ -1870,7 +1960,7 @@ public class ForwardingRulesManager implements
             }
         }
         if (target != null) {
-            Status status = target.validate(container);
+            Status status = target.validate();
             if (!status.isSuccess()) {
                 log.warn(status.getDescription());
                 return status;
@@ -1921,7 +2011,7 @@ public class ForwardingRulesManager implements
      *            inactive list
      */
     private void uninstallAllFlowEntries(boolean preserveFlowEntries) {
-        log.info("Uninstalling all non-internal flows");
+        log.trace("Uninstalling all non-internal flows");
 
         List<FlowEntryInstall> toRemove = new ArrayList<FlowEntryInstall>();
 
@@ -1959,7 +2049,7 @@ public class ForwardingRulesManager implements
      * default container instance of FRM only when the last container is deleted
      */
     private void reinstallAllFlowEntries() {
-        log.info("Reinstalling all inactive flows");
+        log.trace("Reinstalling all inactive flows");
 
         for (FlowEntry flowEntry : this.inactiveFlows.keySet()) {
             this.addEntry(flowEntry, false);
@@ -1971,22 +2061,7 @@ public class ForwardingRulesManager implements
 
     @Override
     public List<FlowConfig> getStaticFlows() {
-        return getStaticFlowsOrderedList(staticFlows, staticFlowsOrdinal.get(0).intValue());
-    }
-
-    // TODO: need to come out with a better algorithm for maintaining the order
-    // of the configuration entries
-    // with actual one, index associated to deleted entries cannot be reused and
-    // map grows...
-    private List<FlowConfig> getStaticFlowsOrderedList(ConcurrentMap<Integer, FlowConfig> flowMap, int maxKey) {
-        List<FlowConfig> orderedList = new ArrayList<FlowConfig>();
-        for (int i = 0; i <= maxKey; i++) {
-            FlowConfig entry = flowMap.get(i);
-            if (entry != null) {
-                orderedList.add(entry);
-            }
-        }
-        return orderedList;
+        return new ArrayList<FlowConfig>(staticFlows.values());
     }
 
     @Override
@@ -2029,34 +2104,13 @@ public class ForwardingRulesManager implements
         return new ArrayList<Node>(set);
     }
 
-    @SuppressWarnings("unchecked")
     private void loadFlowConfiguration() {
-        ObjectReader objReader = new ObjectReader();
-        ConcurrentMap<Integer, FlowConfig> confList = (ConcurrentMap<Integer, FlowConfig>) objReader.read(this,
-                frmFileName);
-
-        ConcurrentMap<String, PortGroupConfig> pgConfig = (ConcurrentMap<String, PortGroupConfig>) objReader.read(this,
-                portGroupFileName);
-
-        if (pgConfig != null) {
-            for (ConcurrentMap.Entry<String, PortGroupConfig> entry : pgConfig.entrySet()) {
-                addPortGroupConfig(entry.getKey(), entry.getValue().getMatchString(), true);
-            }
+        for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, PORT_GROUP_FILE_NAME)) {
+            addPortGroupConfig(((PortGroupConfig) conf).getName(), ((PortGroupConfig) conf).getMatchString(), true);
         }
 
-        if (confList == null) {
-            return;
-        }
-
-        int maxKey = 0;
-        for (Integer key : confList.keySet()) {
-            if (key.intValue() > maxKey) {
-                maxKey = key.intValue();
-            }
-        }
-
-        for (FlowConfig conf : getStaticFlowsOrderedList(confList, maxKey)) {
-            addStaticFlowInternal(conf, true);
+        for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, STATIC_FLOWS_FILE_NAME)) {
+            addStaticFlowInternal((FlowConfig) conf, false, true);
         }
     }
 
@@ -2071,43 +2125,42 @@ public class ForwardingRulesManager implements
     }
 
     private Status saveConfigInternal() {
-        ObjectWriter objWriter = new ObjectWriter();
-        ConcurrentMap<Integer, FlowConfig> nonDynamicFlows = new ConcurrentHashMap<Integer, FlowConfig>();
+        List<ConfigurationObject> nonDynamicFlows = new ArrayList<ConfigurationObject>();
+
         for (Integer ordinal : staticFlows.keySet()) {
             FlowConfig config = staticFlows.get(ordinal);
             // Do not save dynamic and controller generated static flows
             if (config.isDynamic() || config.isInternalFlow()) {
                 continue;
             }
-            nonDynamicFlows.put(ordinal, config);
+            nonDynamicFlows.add(config);
         }
-        objWriter.write(nonDynamicFlows, frmFileName);
-        objWriter.write(new ConcurrentHashMap<String, PortGroupConfig>(portGroupConfigs), portGroupFileName);
-        return new Status(StatusCode.SUCCESS, null);
+
+        configurationService.persistConfiguration(nonDynamicFlows, STATIC_FLOWS_FILE_NAME);
+        configurationService.persistConfiguration(new ArrayList<ConfigurationObject>(portGroupConfigs.values()),
+                PORT_GROUP_FILE_NAME);
+
+        return new Status(StatusCode.SUCCESS);
     }
 
     @Override
     public void subnetNotify(Subnet sub, boolean add) {
     }
 
-    private void installImplicitARPReplyPunt(Node node) {
-
-        if (node == null) {
-            return;
+    private boolean programInternalFlow(boolean proactive, FlowConfig fc) {
+        boolean retVal = true; // program flows unless determined otherwise
+        if(proactive) {
+            // if the flow already exists do not program
+            if(flowConfigExists(fc)) {
+                retVal = false;
+            }
+        } else {
+            // if the flow does not exist do not program
+            if(!flowConfigExists(fc)) {
+                retVal = false;
+            }
         }
-
-        List<String> puntAction = new ArrayList<String>();
-        puntAction.add(ActionType.CONTROLLER.toString());
-
-        FlowConfig allowARP = new FlowConfig();
-        allowARP.setInstallInHw(true);
-        allowARP.setName(FlowConfig.INTERNALSTATICFLOWBEGIN + "Punt ARP Reply" + FlowConfig.INTERNALSTATICFLOWEND);
-        allowARP.setPriority("500");
-        allowARP.setNode(node);
-        allowARP.setEtherType("0x" + Integer.toHexString(EtherTypes.ARP.intValue()).toUpperCase());
-        allowARP.setDstMac(HexEncode.bytesToHexString(switchManager.getControllerMAC()));
-        allowARP.setActions(puntAction);
-        addStaticFlowInternal(allowARP, true); // skip validation on internal static flow name
+        return retVal;
     }
 
     /**
@@ -2164,14 +2217,20 @@ public class ForwardingRulesManager implements
                 dropAllConfig.setActions(dropAction);
                 defaultConfigs.add(dropAllConfig);
 
-                log.info("Forwarding mode for node {} set to {}", node, (proactive ? "proactive" : "reactive"));
+                log.trace("Forwarding mode for node {} set to {}", node, (proactive ? "proactive" : "reactive"));
                 for (FlowConfig fc : defaultConfigs) {
-                    Status status = (proactive) ? addStaticFlowInternal(fc, false) : removeStaticFlow(fc);
-                    if (status.isSuccess()) {
-                        log.info("{} Proactive Static flow: {}", (proactive ? "Installed" : "Removed"), fc.getName());
+                    // check if the frm really needs to act on the notification.
+                    // this is to check against duplicate notifications
+                    if(programInternalFlow(proactive, fc)) {
+                        Status status = (proactive) ? addStaticFlowInternal(fc, false, false) : removeStaticFlow(fc);
+                        if (status.isSuccess()) {
+                            log.trace("{} Proactive Static flow: {}", (proactive ? "Installed" : "Removed"), fc.getName());
+                        } else {
+                            log.warn("Failed to {} Proactive Static flow: {}", (proactive ? "install" : "remove"),
+                                    fc.getName());
+                        }
                     } else {
-                        log.warn("Failed to {} Proactive Static flow: {}", (proactive ? "install" : "remove"),
-                                fc.getName());
+                        log.debug("Got redundant install request for internal flow: {} on node: {}. Request not sent to FRM.", fc.getName(), node);
                     }
                 }
                 return new Status(StatusCode.SUCCESS);
@@ -2191,12 +2250,12 @@ public class ForwardingRulesManager implements
      * @param node
      */
     private void cleanDatabaseForNode(Node node) {
-        log.info("Cleaning Flow database for Node {}", node);
+        log.trace("Cleaning Flow database for Node {}", node);
         if (nodeFlows.containsKey(node)) {
             List<FlowEntryInstall> toRemove = new ArrayList<FlowEntryInstall>(nodeFlows.get(node));
 
             for (FlowEntryInstall entry : toRemove) {
-                updateLocalDatabase(entry, false);
+                updateSwViews(entry, false);
             }
         }
     }
@@ -2320,7 +2379,7 @@ public class ForwardingRulesManager implements
                 if (fei.getGroupName().equals(FlowConfig.STATICFLOWGROUP)) {
                     FlowConfig flowConfig = getStaticFlow(fei.getFlowName(), fei.getNode());
                     if (flowConfig != null) {
-                        flowConfig.setStatus(PORTREMOVED);
+                        flowConfig.setStatus(PORT_REMOVED);
                         updated = true;
                     }
                 }
@@ -2346,7 +2405,7 @@ public class ForwardingRulesManager implements
             if ((staticFlow.getNode().equals(node)) && (staticFlow.getPortGroup().equals(config.getName()))) {
                 for (Short port : data.getPorts()) {
                     FlowConfig derivedFlow = getDerivedFlowConfig(staticFlow, config.getName(), port);
-                    addStaticFlowInternal(derivedFlow, false);
+                    addStaticFlowInternal(derivedFlow, false, false);
                 }
             }
         }
@@ -2368,7 +2427,7 @@ public class ForwardingRulesManager implements
 
     @Override
     public void portGroupChanged(PortGroupConfig config, Map<Node, PortGroup> data, boolean add) {
-        log.info("PortGroup Changed for: {} Data: {}", config, portGroupData);
+        log.trace("PortGroup Changed for: {} Data: {}", config, portGroupData);
         Map<Node, PortGroup> existingData = portGroupData.get(config);
         if (existingData != null) {
             for (Map.Entry<Node, PortGroup> entry : data.entrySet()) {
@@ -2434,17 +2493,6 @@ public class ForwardingRulesManager implements
         return true;
     }
 
-    private void usePortGroupConfig(String name) {
-        PortGroupConfig config = portGroupConfigs.get(name);
-        if (config == null) {
-            return;
-        }
-        if (portGroupProvider != null) {
-            Map<Node, PortGroup> data = portGroupProvider.getPortGroupData(config);
-            portGroupData.put(config, data);
-        }
-    }
-
     @Override
     public Map<String, PortGroupConfig> getPortGroupConfigs() {
         return portGroupConfigs;
@@ -2467,6 +2515,16 @@ public class ForwardingRulesManager implements
         }
     }
 
+    public void setConfigurationContainerService(IConfigurationContainerService service) {
+        log.trace("Got configuration service set request {}", service);
+        this.configurationService = service;
+    }
+
+    public void unsetConfigurationContainerService(IConfigurationContainerService service) {
+        log.trace("Got configuration service UNset request");
+        this.configurationService = null;
+    }
+
     @Override
     public PortGroupProvider getPortGroupProvider() {
         return portGroupProvider;
@@ -2517,8 +2575,6 @@ public class ForwardingRulesManager implements
      *
      */
     void init() {
-        frmFileName = GlobalConstants.STARTUPHOME.toString() + "frm_staticflows_" + this.getContainerName() + ".conf";
-        portGroupFileName = GlobalConstants.STARTUPHOME.toString() + "portgroup_" + this.getContainerName() + ".conf";
 
         inContainerMode = false;
 
@@ -2526,9 +2582,10 @@ public class ForwardingRulesManager implements
             portGroupProvider.registerPortGroupChange(this);
         }
 
-        cacheStartup();
+        nodeFlows = new ConcurrentHashMap<Node, List<FlowEntryInstall>>();
+        groupFlows = new ConcurrentHashMap<String, List<FlowEntryInstall>>();
 
-        registerWithOSGIConsole();
+        cacheStartup();
 
         /*
          * If we are not the first cluster node to come up, do not initialize
@@ -2584,13 +2641,13 @@ public class ForwardingRulesManager implements
                                         FlowEntryInstall feiNew = workOrder.get(fe);
                                         switch (fe.getUpType()) {
                                         case ADDED:
-                                            gotStatus = addEntriesInternal(feiCurrent, false);
+                                            gotStatus = addEntryInHw(feiCurrent, false);
                                             break;
                                         case CHANGED:
-                                            gotStatus = modifyEntryInternal(feiCurrent, feiNew, false);
+                                            gotStatus = modifyEntryInHw(feiCurrent, feiNew, false);
                                             break;
                                         case REMOVED:
-                                            gotStatus = removeEntryInternal(feiCurrent, false);
+                                            gotStatus = removeEntryInHw(feiCurrent, false);
                                             break;
                                         }
                                         // Remove the Order
@@ -2627,9 +2684,11 @@ public class ForwardingRulesManager implements
                              * flow merging is not an injective function
                              */
                             updateFlowsContainerFlow();
+                        } else if (event instanceof UpdateIndexDBs) {
+                            UpdateIndexDBs update = (UpdateIndexDBs)event;
+                            updateIndexDatabase(update.getFei(), update.isAddition());
                         } else {
-                            log.warn("Dequeued unknown event {}", event.getClass()
-                                    .getSimpleName());
+                            log.warn("Dequeued unknown event {}", event.getClass().getSimpleName());
                         }
                     } catch (InterruptedException e) {
                         // clear pending events
@@ -2678,13 +2737,23 @@ public class ForwardingRulesManager implements
         // Start event handler thread
         frmEventHandler.start();
 
+        // replay the installedSwView data structure to populate
+        // node flows and group flows
+        for (FlowEntryInstall fei : installedSwView.values()) {
+            pendingEvents.offer(new UpdateIndexDBs(fei, true));
+        }
+
         /*
-         * Read startup and build database if we have not already gotten the
-         * configurations synced from another node
+         * Read startup and build database if we are the coordinator
          */
-        if (staticFlows.isEmpty()) {
-            loadFlowConfiguration();
-        }
+        loadFlowConfiguration();
+    }
+
+    /**
+     * Function called by the dependency manager before Container is Stopped and Destroyed.
+     */
+    public void containerStop() {
+        uninstallAllFlowEntries(false);
     }
 
     /**
@@ -2694,7 +2763,6 @@ public class ForwardingRulesManager implements
      */
     void stop() {
         stopping = true;
-        uninstallAllFlowEntries(false);
         // Shutdown executor
         this.executor.shutdownNow();
         // Now walk all the workMonitor and wake up the one sleeping because
@@ -2794,6 +2862,7 @@ public class ForwardingRulesManager implements
             this.reinstallAllFlowEntries();
             break;
         default:
+            break;
         }
 
         // Update our configuration DB
@@ -2916,125 +2985,36 @@ public class ForwardingRulesManager implements
         }
     }
 
-    /*
-     * OSGI COMMANDS
-     */
-    @Override
-    public String getHelp() {
-        StringBuffer help = new StringBuffer();
-        help.append("---FRM Matrix Application---\n");
-        help.append("\t printMatrixData        - Prints the Matrix Configs\n");
-        help.append("\t addMatrixConfig <name> <regex>\n");
-        help.append("\t delMatrixConfig <name>\n");
-        help.append("\t useMatrixConfig <name>\n");
-        return help.toString();
-    }
+    private class UpdateIndexDBs extends FRMEvent {
+        private FlowEntryInstall fei;
+        private boolean add;
 
-    public void _printMatrixData(CommandInterpreter ci) {
-        ci.println("Configs : ");
-        ci.println("---------");
-        ci.println(portGroupConfigs);
-
-        ci.println("Data : ");
-        ci.println("------");
-        ci.println(portGroupData);
-    }
-
-    public void _addMatrixConfig(CommandInterpreter ci) {
-        String name = ci.nextArgument();
-        String regex = ci.nextArgument();
-        addPortGroupConfig(name, regex, false);
-    }
-
-    public void _delMatrixConfig(CommandInterpreter ci) {
-        String name = ci.nextArgument();
-        delPortGroupConfig(name);
-    }
-
-    public void _useMatrixConfig(CommandInterpreter ci) {
-        String name = ci.nextArgument();
-        usePortGroupConfig(name);
-    }
-
-    public void _arpPunt(CommandInterpreter ci) {
-        String switchId = ci.nextArgument();
-        long swid = HexEncode.stringToLong(switchId);
-        Node node = NodeCreator.createOFNode(swid);
-        installImplicitARPReplyPunt(node);
-    }
-
-    public void _frmaddflow(CommandInterpreter ci) throws UnknownHostException {
-        Node node = null;
-        String nodeId = ci.nextArgument();
-        if (nodeId == null) {
-            ci.print("Node id not specified");
-            return;
-        }
-        try {
-            node = NodeCreator.createOFNode(Long.valueOf(nodeId));
-        } catch (NumberFormatException e) {
-            ci.print("Node id not a number");
-            return;
+        /**
+         *
+         * @param fei the flow entry which was installed/removed on the netwrok node
+         * @param update
+         */
+        UpdateIndexDBs(FlowEntryInstall fei, boolean add) {
+            this.fei = fei;
+            this.add = add;
         }
-        ci.println(this.programmer.addFlow(node, getSampleFlow(node)));
-    }
 
-    public void _frmremoveflow(CommandInterpreter ci) throws UnknownHostException {
-        Node node = null;
-        String nodeId = ci.nextArgument();
-        if (nodeId == null) {
-            ci.print("Node id not specified");
-            return;
-        }
-        try {
-            node = NodeCreator.createOFNode(Long.valueOf(nodeId));
-        } catch (NumberFormatException e) {
-            ci.print("Node id not a number");
-            return;
+
+        /**
+         * @return the flowEntryInstall object which was added/removed
+         * to/from the installed software view cache
+         */
+        public FlowEntryInstall getFei() {
+            return fei;
         }
-        ci.println(this.programmer.removeFlow(node, getSampleFlow(node)));
-    }
-
-    private Flow getSampleFlow(Node node) throws UnknownHostException {
-        NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
-        NodeConnector oport = NodeConnectorCreator.createOFNodeConnector((short) 30, node);
-        byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
-        byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
-        InetAddress srcIP = InetAddress.getByName("172.28.30.50");
-        InetAddress dstIP = InetAddress.getByName("171.71.9.52");
-        InetAddress ipMask = InetAddress.getByName("255.255.255.0");
-        InetAddress ipMask2 = InetAddress.getByName("255.0.0.0");
-        short ethertype = EtherTypes.IPv4.shortValue();
-        short vlan = (short) 27;
-        byte vlanPr = 3;
-        Byte tos = 4;
-        byte proto = IPProtocols.TCP.byteValue();
-        short src = (short) 55000;
-        short dst = 80;
 
-        /*
-         * Create a SAL Flow aFlow
+        /**
+         *
+         * @return whether this was an flow addition or removal
          */
-        Match match = new Match();
-        match.setField(MatchType.IN_PORT, port);
-        match.setField(MatchType.DL_SRC, srcMac);
-        match.setField(MatchType.DL_DST, dstMac);
-        match.setField(MatchType.DL_TYPE, ethertype);
-        match.setField(MatchType.DL_VLAN, vlan);
-        match.setField(MatchType.DL_VLAN_PR, vlanPr);
-        match.setField(MatchType.NW_SRC, srcIP, ipMask);
-        match.setField(MatchType.NW_DST, dstIP, ipMask2);
-        match.setField(MatchType.NW_TOS, tos);
-        match.setField(MatchType.NW_PROTO, proto);
-        match.setField(MatchType.TP_SRC, src);
-        match.setField(MatchType.TP_DST, dst);
-
-        List<Action> actions = new ArrayList<Action>();
-        actions.add(new Output(oport));
-        actions.add(new PopVlan());
-        actions.add(new Flood());
-        actions.add(new Controller());
-        return new Flow(match, actions);
+        public boolean isAddition() {
+            return add;
+        }
     }
 
     @Override
@@ -3042,88 +3022,6 @@ public class ForwardingRulesManager implements
         return saveConfig();
     }
 
-    public void _frmNodeFlows(CommandInterpreter ci) {
-        String nodeId = ci.nextArgument();
-        Node node = Node.fromString(nodeId);
-        if (node == null) {
-            ci.println("frmNodeFlows <node> [verbose]");
-            return;
-        }
-        boolean verbose = false;
-        String verboseCheck = ci.nextArgument();
-        if (verboseCheck != null) {
-            verbose = verboseCheck.equals("true");
-        }
-
-        if (!nodeFlows.containsKey(node)) {
-            return;
-        }
-        // Dump per node database
-        for (FlowEntryInstall entry : nodeFlows.get(node)) {
-            if (!verbose) {
-                ci.println(node + " " + installedSwView.get(entry).getFlowName());
-            } else {
-                ci.println(node + " " + installedSwView.get(entry).toString());
-            }
-        }
-    }
-
-    public void _frmGroupFlows(CommandInterpreter ci) {
-        String group = ci.nextArgument();
-        if (group == null) {
-            ci.println("frmGroupFlows <group> [verbose]");
-            return;
-        }
-        boolean verbose = false;
-        String verboseCheck = ci.nextArgument();
-        if (verboseCheck != null) {
-            verbose = verboseCheck.equalsIgnoreCase("true");
-        }
-
-        if (!groupFlows.containsKey(group)) {
-            return;
-        }
-        // Dump per node database
-        ci.println("Group " + group + ":\n");
-        for (FlowEntryInstall flowEntry : groupFlows.get(group)) {
-            if (!verbose) {
-                ci.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
-            } else {
-                ci.println(flowEntry.getNode() + " " + flowEntry.toString());
-            }
-        }
-    }
-
-    public void _frmProcessErrorEvent(CommandInterpreter ci) throws UnknownHostException {
-        Node node = null;
-        long reqId = 0L;
-        String nodeId = ci.nextArgument();
-        if (nodeId == null) {
-            ci.print("Node id not specified");
-            return;
-        }
-        String requestId = ci.nextArgument();
-        if (requestId == null) {
-            ci.print("Request id not specified");
-            return;
-        }
-        try {
-            node = NodeCreator.createOFNode(Long.valueOf(nodeId));
-        } catch (NumberFormatException e) {
-            ci.print("Node id not a number");
-            return;
-        }
-        try {
-            reqId = Long.parseLong(requestId);
-        } catch (NumberFormatException e) {
-            ci.print("Request id not a number");
-            return;
-        }
-        // null for error object is good enough for now
-        ErrorReportedEvent event = new ErrorReportedEvent(reqId, node, null);
-        this.processErrorEvent(event);
-    }
-
     @Override
     public void flowRemoved(Node node, Flow flow) {
         log.trace("Received flow removed notification on {} for {}", node, flow);
@@ -3149,13 +3047,21 @@ public class ForwardingRulesManager implements
         }
         if (target != null) {
             // Update Configuration database
-            target.toggleInstallation();
-            target.setStatus(StatusCode.SUCCESS.toString());
+            if (target.getHardTimeout() != null || target.getIdleTimeout() != null) {
+                /*
+                 * No need for checking if actual values: these strings were
+                 * validated at configuration creation. Also, after a switch
+                 * down scenario, no use to reinstall a timed flow. Mark it as
+                 * "do not install". User can manually toggle it.
+                 */
+                target.toggleInstallation();
+            }
+            target.setStatus(StatusCode.GONE.toString());
             staticFlows.put(key, target);
         }
 
         // Update software views
-        this.updateLocalDatabase(installedEntry, false);
+        this.updateSwViews(installedEntry, false);
     }
 
     @Override
@@ -3192,14 +3098,18 @@ public class ForwardingRulesManager implements
         }
         if (target != null) {
             // This was a flow install, update database
-            this.updateLocalDatabase(target, false);
+            this.updateSwViews(target, false);
             // also update the config
             if(FlowConfig.STATICFLOWGROUP.equals(target.getGroupName())) {
                 ConcurrentMap.Entry<Integer, FlowConfig> staticFlowEntry = getStaticFlowEntry(target.getFlowName(),target.getNode());
                 // staticFlowEntry should never be null.
                 // the null check is just an extra defensive check.
                 if(staticFlowEntry != null) {
-                    staticFlows.remove(staticFlowEntry.getKey());
+                    // Modify status and update cluster cache
+                    log.debug("Updating static flow configuration on async error event");
+                    String status = String.format("Cannot be installed on node. reason: %s", errorString);
+                    staticFlowEntry.getValue().setStatus(status);
+                    refreshClusterStaticFlowsStatus(node);
                 }
             }
         }
@@ -3262,6 +3172,13 @@ public class ForwardingRulesManager implements
 
     @Override
     public void entryUpdated(Object key, Object new_value, String cacheName, boolean originLocal) {
+        /*
+         * Streamline the updates for the per node and per group index databases
+         */
+        if (cacheName.equals(INSTALLED_SW_VIEW_CACHE)) {
+            pendingEvents.offer(new UpdateIndexDBs((FlowEntryInstall)new_value, true));
+        }
+
         if (originLocal) {
             /*
              * Local updates are of no interest
@@ -3313,7 +3230,44 @@ public class ForwardingRulesManager implements
     @Override
     public void entryDeleted(Object key, String cacheName, boolean originLocal) {
         /*
-         * Do nothing
+         * Streamline the updates for the per node and per group index databases
          */
+        if (cacheName.equals(INSTALLED_SW_VIEW_CACHE)) {
+            pendingEvents.offer(new UpdateIndexDBs((FlowEntryInstall)key, false));
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public List<FlowEntry> getFlowEntriesForNode(Node node) {
+        List<FlowEntry> list = new ArrayList<FlowEntry>();
+        if (node != null) {
+            for (Map.Entry<FlowEntry, FlowEntry> entry : this.originalSwView.entrySet()) {
+                if (node.equals(entry.getKey().getNode())) {
+                    list.add(entry.getValue().clone());
+                }
+            }
+        }
+        return list;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public List<FlowEntry> getInstalledFlowEntriesForNode(Node node) {
+        List<FlowEntry> list = new ArrayList<FlowEntry>();
+        if (node != null) {
+            List<FlowEntryInstall> flowEntryInstallList = this.nodeFlows.get(node);
+            if(flowEntryInstallList != null) {
+                for(FlowEntryInstall fi: flowEntryInstallList) {
+                    list.add(fi.getInstall().clone());
+                }
+            }
+        }
+        return list;
     }
+
 }