Update FlowManagerModule's implementation.

- Updated FlowManagerModule to use FlowMap and FlowBatchMap
  for its storage to store flows and batch operations.
- Implemented getFlow() and getFlows() methods.
- Removed FlowOperationMap class. This class has been replaced by FlowBatchMap.
- This task is a part of ONOS-1690, ONOS-1692 and ONOS-1736.

Change-Id: Id148b6191471eb209ff28b801a3e7ca075d7da6b
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchHandleImpl.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchHandleImpl.java
index 61a450f..0f8c9a0 100644
--- a/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchHandleImpl.java
+++ b/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchHandleImpl.java
@@ -9,7 +9,7 @@
 import net.onrc.onos.api.flowmanager.FlowBatchState;
 
 public class FlowBatchHandleImpl implements FlowBatchHandle {
-    private final FlowOperationMap flowOperationMap;
+    private final FlowBatchMap flowBatchMap;
     private final FlowBatchId batchId;
 
     /**
@@ -18,18 +18,18 @@
      * The ID is automatically generated and assigned by FlowManager, and used
      * as an internal key for the flow batch operation map.
      *
-     * @param opMap the FlowOperationMap object which maintains the flow batch
+     * @param map the {@link FlowBatchMap} object which maintains the flow batch
      *        operation
-     * @param id the batch operation ID
+     * @param id the Id for this batch operation
      */
-    public FlowBatchHandleImpl(FlowOperationMap opMap, FlowBatchId id) {
-        flowOperationMap = opMap;
+    public FlowBatchHandleImpl(FlowBatchMap map, FlowBatchId id) {
+        flowBatchMap = map;
         batchId = id;
     }
 
     @Override
     public FlowBatchOperation getFlowBatchOperation() {
-        FlowBatchOperation op = checkNotNull(flowOperationMap.getBatchOperation(batchId),
+        FlowBatchOperation op = checkNotNull(flowBatchMap.get(batchId),
                 "The requested flow batch operation does not exist in the map.");
 
         // TODO: should be an instance of immutable batch operation class.
@@ -38,14 +38,14 @@
 
     @Override
     public FlowBatchState getState() {
-        return flowOperationMap.getState(batchId);
+        return flowBatchMap.getState(batchId);
     }
 
     @Override
     public void purge() {
         FlowBatchState state = getState();
         if (state == FlowBatchState.COMPLETED || state == FlowBatchState.FAILED) {
-            flowOperationMap.removeBatchOperation(batchId);
+            flowBatchMap.remove(batchId);
         }
     }
 }
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java
index 1187c88..1a3523b 100644
--- a/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java
+++ b/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java
@@ -18,13 +18,14 @@
 import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy;
 import net.onrc.onos.api.flowmanager.Flow;
 import net.onrc.onos.api.flowmanager.FlowBatchHandle;
+import net.onrc.onos.api.flowmanager.FlowBatchId;
 import net.onrc.onos.api.flowmanager.FlowBatchOperation;
 import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
 import net.onrc.onos.api.flowmanager.FlowId;
 import net.onrc.onos.api.flowmanager.FlowIdGenerator;
 import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService;
 import net.onrc.onos.api.flowmanager.FlowManagerListener;
-import net.onrc.onos.core.datagrid.IDatagridService;
+import net.onrc.onos.core.datagrid.ISharedCollectionsService;
 import net.onrc.onos.core.matchaction.MatchActionIdGeneratorWithIdBlockAllocator;
 import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
 import net.onrc.onos.core.matchaction.MatchActionOperations;
@@ -40,11 +41,14 @@
  */
 public class FlowManagerModule implements FlowManagerFloodlightService, IFloodlightModule {
     private ConflictDetectionPolicy conflictDetectionPolicy;
-    private FlowOperationMap flowOperationMap;
+    private FlowIdGeneratorWithIdBlockAllocator flowIdGenerator;
+    private FlowBatchIdGeneratorWithIdBlockAllocator flowBatchIdGenerator;
     private MatchActionIdGeneratorWithIdBlockAllocator maIdGenerator;
     private MatchActionOperationsIdGeneratorWithIdBlockAllocator maoIdGenerator;
-    private FlowIdGeneratorWithIdBlockAllocator flowIdGenerator;
     private IControllerRegistryService registryService;
+    private ISharedCollectionsService sharedCollectionService;
+    private FlowMap flowMap;
+    private FlowBatchMap flowBatchMap;
 
     @Override
     public Collection<Class<? extends IFloodlightService>> getModuleServices() {
@@ -65,32 +69,36 @@
     @Override
     public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
         return Arrays.asList(
-                IDatagridService.class,
+                // TODO: Add MatchActionService.class.
+                // The class has to be an instance of IFloodlightService.
+                ISharedCollectionsService.class,
                 IControllerRegistryService.class);
     }
 
     @Override
     public void init(FloodlightModuleContext context) throws FloodlightModuleException {
         registryService = context.getServiceImpl(IControllerRegistryService.class);
+        sharedCollectionService = context.getServiceImpl(ISharedCollectionsService.class);
     }
 
     @Override
     public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {
 
         IdBlockAllocator idBlockAllocator = registryService;
-        this.flowIdGenerator =
+        flowIdGenerator =
                 new FlowIdGeneratorWithIdBlockAllocator(idBlockAllocator);
+        flowBatchIdGenerator =
+                new FlowBatchIdGeneratorWithIdBlockAllocator(idBlockAllocator);
 
-        // TODO: MatchActionOperationsIdGenerator should be retrieved from
+        // TODO: MatchAction related ID generator should be retrieved from
         // MatchAction Module.
-        this.maIdGenerator =
+        maIdGenerator =
                 new MatchActionIdGeneratorWithIdBlockAllocator(idBlockAllocator);
-        this.maoIdGenerator =
+        maoIdGenerator =
                 new MatchActionOperationsIdGeneratorWithIdBlockAllocator(idBlockAllocator);
 
-        // TODO: datagridService =
-        // context.getServiceImpl(IDatagridService.class);
-        this.flowOperationMap = new FlowOperationMap(idBlockAllocator);
+        flowMap = new SharedFlowMap(sharedCollectionService);
+        flowBatchMap = new SharedFlowBatchMap(sharedCollectionService);
     }
 
     /**
@@ -116,14 +124,12 @@
 
     @Override
     public Flow getFlow(FlowId id) {
-        // TODO Auto-generated method stub
-        return null;
+        return flowMap.get(id);
     }
 
     @Override
     public Collection<Flow> getFlows() {
-        // TODO Auto-generated method stub
-        return null;
+        return flowMap.getAll();
     }
 
     /**
@@ -131,18 +137,20 @@
      * <p>
      * To track the execution result, use the returned FlowBatchHandle object.
      * <p>
-     * This method just put the batch-operation object to the global flow
+     * This method just put the batch-operation object to the global flow batch
      * operation map with unique ID. The worker process for execution and
      * installation will get the appended operation when it gets events from the
      * map. This method returns a handler for obtaining the result of this
      * operation, control the executing process, etc.
      *
      * @param ops flow operations to be executed
-     * @return FlowBatchHandle object if succeeded, null otherwise
+     * @return {@link FlowBatchHandle} object if succeeded, null otherwise
      */
     @Override
     public FlowBatchHandle executeBatch(FlowBatchOperation ops) {
-        return flowOperationMap.putBatchOperation(ops);
+        FlowBatchId id = flowBatchIdGenerator.getNewId();
+        flowBatchMap.put(id, ops);
+        return new FlowBatchHandleImpl(flowBatchMap, id);
     }
 
     @Override
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowOperationMap.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowOperationMap.java
deleted file mode 100644
index 6535269..0000000
--- a/src/main/java/net/onrc/onos/core/flowmanager/FlowOperationMap.java
+++ /dev/null
@@ -1,82 +0,0 @@
-package net.onrc.onos.core.flowmanager;
-
-import net.onrc.onos.api.flowmanager.FlowBatchHandle;
-import net.onrc.onos.api.flowmanager.FlowBatchId;
-import net.onrc.onos.api.flowmanager.FlowBatchOperation;
-import net.onrc.onos.api.flowmanager.FlowBatchState;
-import net.onrc.onos.core.util.IdBlockAllocator;
-
-/**
- * Manages the set of flow operations throughout the ONOS instances.
- * <p>
- * Application can access to this map using {@link FlowBatchHandle}.
- */
-public class FlowOperationMap {
-    private final FlowBatchIdGeneratorWithIdBlockAllocator flowBatchIdGenerator;
-
-    /**
-     * Creates a driver for the shared flow batch operation map.
-     *
-     * @param allocator {@link IdBlockAllocator} to be used for batch IDs
-     */
-    FlowOperationMap(IdBlockAllocator allocator) {
-        flowBatchIdGenerator = new FlowBatchIdGeneratorWithIdBlockAllocator(allocator);
-    }
-
-    /**
-     * Adds a flow batch operation to the map.
-     *
-     * @param ops the flow batch operation to be added
-     * @return {@link FlowBatchHandle} handle if succeeded, null otherwise
-     */
-    FlowBatchHandle putBatchOperation(FlowBatchOperation ops) {
-        FlowBatchId id = flowBatchIdGenerator.getNewId();
-
-        // TODO: put batch operation to map
-
-        boolean succeeded = false;
-
-        return succeeded ? new FlowBatchHandleImpl(this, id) : null;
-    }
-
-    /**
-     * Gets the flow batch operation from the map specified with an ID.
-     *
-     * @param id the ID for the operation
-     * @return the flow batch operation if exists, null otherwise
-     */
-    FlowBatchOperation getBatchOperation(FlowBatchId id) {
-        // TODO: implement it
-        return null;
-    }
-
-    /**
-     * Removes the flow batch operation from the map specified with an ID.
-     */
-    void removeBatchOperation(FlowBatchId id) {
-        // TODO: implement it
-    }
-
-    /**
-     * Gets the state for the flow batch operation specified with an ID.
-     *
-     * @param id the ID for the batch operation
-     * @return the state of the batch operation
-     */
-    FlowBatchState getState(FlowBatchId id) {
-        // TODO implement it
-        return null;
-    }
-
-    /**
-     * Updates the state for the flow batch operation specified with an ID.
-     *
-     * @param id the ID for the batch operation
-     * @param state new state for the batch operation
-     * @return true if succeeded, false otherwise
-     */
-    boolean setState(FlowBatchId id, FlowBatchState state) {
-        // TODO: implement it
-        return false;
-    }
-}