Added flow event listener
[controller.git] / opendaylight / forwardingrulesmanager_mdsal / openflow / src / main / java / org / opendaylight / controller / forwardingrulesmanager_mdsal / consumer / impl / FlowConsumerImpl.java
index 7545203f06d4005f39086fe290fd8c28c44226f3..1ff7a98ebb816f07ede8e891c71c152ae46447b2 100644 (file)
@@ -35,10 +35,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddF
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
@@ -53,9 +58,9 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class FlowConsumerImpl {
+public class FlowConsumerImpl implements IForwardingRulesManager {
     protected static final Logger logger = LoggerFactory.getLogger(FlowConsumerImpl.class);
-    private FlowEventListener flowEventListener = new FlowEventListener();
+    private final FlowEventListener flowEventListener = new FlowEventListener();
     private Registration<NotificationListener> listener1Reg;
     private SalFlowService flowService;
     // private FlowDataListener listener;
@@ -200,7 +205,7 @@ public class FlowConsumerImpl {
         // updating the staticflow cache
         Integer ordinal = staticFlowsOrdinal.get(0);
         staticFlowsOrdinal.put(0, ++ordinal);
-        staticFlows.put(ordinal, (Flow) dataObject);
+        staticFlows.put(ordinal, dataObject);
 
         // We send flow to the sounthbound plugin
         flowService.addFlow(input.build());
@@ -240,6 +245,29 @@ public class FlowConsumerImpl {
         updateLocalDatabase((NodeFlow) dataObject, false);
     }
 
+    /**
+     * Update flow to the southbound plugin and our internal database
+     *
+     * @param path
+     * @param dataObject
+     */
+    private void updateFlow(InstanceIdentifier<?> path, Flow dataObject) {
+
+        UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
+        UpdatedFlowBuilder updatedflowbuilder = new UpdatedFlowBuilder();
+        updatedflowbuilder.fieldsFrom(dataObject);
+        input.setUpdatedFlow(updatedflowbuilder.build());
+
+        // updating the staticflow cache
+        Integer ordinal = staticFlowsOrdinal.get(0);
+        staticFlowsOrdinal.put(0, ++ordinal);
+        staticFlows.put(ordinal, dataObject);
+
+        // We send flow to the sounthbound plugin
+        flowService.updateFlow(input.build());
+        updateLocalDatabase((NodeFlow) dataObject, true);
+    }
+
     @SuppressWarnings("unchecked")
     private void commitToPlugin(internalTransaction transaction) {
         for (Entry<InstanceIdentifier<?>, Flow> entry : transaction.additions.entrySet()) {
@@ -249,7 +277,7 @@ public class FlowConsumerImpl {
         for (@SuppressWarnings("unused")
         Entry<InstanceIdentifier<?>, Flow> entry : transaction.updates.entrySet()) {
             System.out.println("Coming update cc in FlowDatacommitHandler");
-            // updateFlow(entry.getKey(),entry.getValue());
+            updateFlow(entry.getKey(), entry.getValue());
         }
 
         for (Entry<InstanceIdentifier<?>, Flow> entry : transaction.removals.entrySet()) {
@@ -315,7 +343,7 @@ public class FlowConsumerImpl {
                     logger.error(error);
                     return;
                 }
-                if (originalSwView.containsKey((FlowKey) entry)) {
+                if (originalSwView.containsKey(entry)) {
                     logger.warn("Operation Rejected: A flow with same match and priority exists on the target node");
                     logger.trace("Aborting to install {}", entry);
                     continue;
@@ -463,6 +491,24 @@ public class FlowConsumerImpl {
             updatedFlows.add(notification);
         }
 
+        @Override
+        public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
+            // TODO
+        }
+
+        @Override
+        public void onNodeErrorNotification(NodeErrorNotification notification) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void onNodeExperimenterErrorNotification(
+                NodeExperimenterErrorNotification notification) {
+            // TODO Auto-generated method stub
+            
+        };
+
     }
 
     // Commented out DataChangeListene - to be used by Stats
@@ -520,10 +566,39 @@ public class FlowConsumerImpl {
             FlowConsumerImpl.originalSwView.put((FlowKey) entry, (Flow) entry);
             installedSwView.put((FlowKey) entry, (Flow) entry);
         } else {
-            originalSwView.remove((Flow) entry);
-            installedSwView.remove((FlowKey) entry);
+            originalSwView.remove(entry);
+            installedSwView.remove(entry);
+
+        }
+    }
+
+    @Override
+    public List<DataObject> get() {
 
+        List<DataObject> orderedList = new ArrayList<DataObject>();
+        ConcurrentMap<Integer, Flow> flowMap = staticFlows;
+        int maxKey = staticFlowsOrdinal.get(0).intValue();
+        for (int i = 0; i <= maxKey; i++) {
+            Flow entry = flowMap.get(i);
+            if (entry != null) {
+                orderedList.add(entry);
+            }
+        }
+        return orderedList;
+    }
+
+    @Override
+    public DataObject getWithName(String name, org.opendaylight.controller.sal.core.Node n) {
+        if (this instanceof FlowConsumerImpl) {
+            for (ConcurrentMap.Entry<Integer, Flow> flowEntry : staticFlows.entrySet()) {
+                Flow flow = flowEntry.getValue();
+                if (flow.getNode().equals(n) && flow.getFlowName().equals(name)) {
+
+                    return flowEntry.getValue();
+                }
+            }
         }
+        return null;
     }
 
     /*
@@ -544,7 +619,7 @@ public class FlowConsumerImpl {
         if (add) {
             nodeIndeces.add((Flow) entry);
         } else {
-            nodeIndeces.remove((Flow) entry);
+            nodeIndeces.remove(entry);
         }
 
         // Update cache across cluster