[ONOS-3114] Changes on Flow Classifier Manager

Change-Id: I5cdbe2c0c9769d16381322bb6c952cdc37ecccfc
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/FlowClassifierService.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/FlowClassifierService.java
index c160d22..c5911ff 100644
--- a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/FlowClassifierService.java
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/FlowClassifierService.java
@@ -24,49 +24,59 @@
 public interface FlowClassifierService {
 
     /**
+     * Check whether Flow Classifier is present based on given Flow Classifier
+     * Id.
+     *
+     * @param id flow classifier identifier
+     * @return true if flow classifier is present otherwise return false
+     */
+    boolean exists(FlowClassifierId id);
+
+    /**
+     * Returns the number of flow classifiers known to the system.
+     *
+     * @return number of flow classifiers
+     */
+    int getFlowClassifierCount();
+
+    /**
      * Store Flow Classifier.
      *
-     * @param flowClassifier Flow Classifier
-     * @return true if adding Flow Classifier into store is success otherwise return false.
+     * @param flowClassifier flow classifier
+     * @return true if adding flow classifier into store is success otherwise
+     *         return false
      */
     boolean createFlowClassifier(FlowClassifier flowClassifier);
 
     /**
      * Return the existing collection of Flow Classifier.
      *
-     * @return Flow Classifier collections.
+     * @return flow classifier collections
      */
     Iterable<FlowClassifier> getFlowClassifiers();
 
     /**
-     * Check whether Flow Classifier is present based on given Flow Classifier Id.
-     *
-     * @param id Flow Classifier.
-     * @return true if Flow Classifier is present otherwise return false.
-     */
-    boolean hasFlowClassifier(FlowClassifierId id);
-
-    /**
      * Retrieve the Flow Classifier based on given Flow Classifier id.
      *
-     * @param id Flow Classifier Id.
-     * @return Flow Classifier if present otherwise returns null.
+     * @param id flow classifier identifier
+     * @return flow classifier if present otherwise returns null
      */
     FlowClassifier getFlowClassifier(FlowClassifierId id);
 
     /**
      * Update Flow Classifier based on given Flow Classifier Id.
      *
-     * @param flowClassifier Flow Classifier.
-     * @return true if update is success otherwise return false.
+     * @param flowClassifier flow classifier
+     * @return true if flow classifier update is success otherwise return false
      */
     boolean updateFlowClassifier(FlowClassifier flowClassifier);
 
     /**
      * Remove Flow Classifier from store based on given Flow Classifier Id.
      *
-     * @param id Flow Classifier Id.
-     * @return true if Flow Classifier removal is success otherwise return false.
+     * @param id flow classifier identifier
+     * @return true if flow classifier removal is success otherwise return
+     *         false
      */
     boolean removeFlowClassifier(FlowClassifierId id);
 }
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/impl/FlowClassifierManager.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/impl/FlowClassifierManager.java
index 015163d..4a60cd3 100644
--- a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/impl/FlowClassifierManager.java
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/flowclassifier/impl/FlowClassifierManager.java
@@ -73,6 +73,28 @@
     }
 
     @Override
+    public boolean exists(FlowClassifierId id) {
+        checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
+        return flowClassifierStore.containsKey(id);
+    }
+
+    @Override
+    public int getFlowClassifierCount() {
+        return flowClassifierStore.size();
+    }
+
+    @Override
+    public Iterable<FlowClassifier> getFlowClassifiers() {
+        return ImmutableList.copyOf(flowClassifierStore.values());
+    }
+
+    @Override
+    public FlowClassifier getFlowClassifier(FlowClassifierId id) {
+        checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
+        return flowClassifierStore.get(id);
+    }
+
+    @Override
     public boolean createFlowClassifier(FlowClassifier flowClassifier) {
         log.debug("createFlowClassifier");
         checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
@@ -87,27 +109,22 @@
     }
 
     @Override
-    public Iterable<FlowClassifier> getFlowClassifiers() {
-        return ImmutableList.copyOf(flowClassifierStore.values());
-    }
-
-    @Override
-    public boolean hasFlowClassifier(FlowClassifierId id) {
-        checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
-        return flowClassifierStore.containsKey(id);
-    }
-
-    @Override
-    public FlowClassifier getFlowClassifier(FlowClassifierId id) {
-        checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
-        return flowClassifierStore.get(id);
-    }
-
-    @Override
     public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
         checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
-        FlowClassifierId id = flowClassifier.flowClassifierId();
-        flowClassifierStore.put(id, flowClassifier);
+
+        if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
+            log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
+                    .toString());
+            return false;
+        }
+
+        flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
+
+        if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
+            log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
+                    .flowClassifierId().toString());
+            return false;
+        }
         return true;
     }
 
diff --git a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java
index b0e2f38..08e37f9 100644
--- a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java
+++ b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java
@@ -15,7 +15,6 @@
  */
 package org.onosproject.vtnweb.resources;
 
-import static javax.ws.rs.core.Response.Status.NOT_FOUND;
 import static javax.ws.rs.core.Response.Status.OK;
 import static org.onlab.util.Tools.nullIsNotFound;
 
@@ -54,7 +53,6 @@
 
     private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class);
 
-    final FlowClassifierService service = get(FlowClassifierService.class);
     public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
 
     /**
@@ -65,7 +63,7 @@
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     public Response getFlowClassifiers() {
-        final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers();
+        Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers();
         ObjectNode result = new ObjectMapper().createObjectNode();
         ArrayNode flowClassifierEntry = result.putArray("flow_classifiers");
         if (flowClassifiers != null) {
@@ -79,19 +77,16 @@
     /**
      * Get details of a flow classifier.
      *
-     * @param id flow classifier id
+     * @param id
+     *            flow classifier id
      * @return 200 OK , 404 if given identifier does not exist
      */
     @GET
     @Path("{flow_id}")
     @Produces(MediaType.APPLICATION_JSON)
     public Response getFlowClassifier(@PathParam("flow_id") String id) {
-
-        if (!service.hasFlowClassifier(FlowClassifierId.of(id))) {
-            return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
-        }
-        FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)),
-                FLOW_CLASSIFIER_NOT_FOUND);
+        FlowClassifier flowClassifier = nullIsNotFound(
+                get(FlowClassifierService.class).getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND);
 
         ObjectNode result = new ObjectMapper().createObjectNode();
         result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this));
@@ -102,9 +97,10 @@
     /**
      * Creates and stores a new flow classifier.
      *
-     * @param stream flow classifier from JSON
+     * @param stream
+     *            flow classifier from JSON
      * @return status of the request - CREATED if the JSON is correct,
-     * BAD_REQUEST if the JSON is invalid
+     *         BAD_REQUEST if the JSON is invalid
      */
     @POST
     @Consumes(MediaType.APPLICATION_JSON)
@@ -116,7 +112,8 @@
             JsonNode flow = jsonTree.get("flow_classifier");
 
             FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
-            Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND);
+            Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier),
+                    FLOW_CLASSIFIER_NOT_FOUND);
             return Response.status(OK).entity(issuccess.toString()).build();
         } catch (IOException ex) {
             log.error("Exception while creating flow classifier {}.", ex.toString());
@@ -127,8 +124,10 @@
     /**
      * Update details of a flow classifier.
      *
-     * @param id flow classifier id
-     * @param stream InputStream
+     * @param id
+     *            flow classifier id
+     * @param stream
+     *            InputStream
      * @return 200 OK, 404 if given identifier does not exist
      */
     @PUT
@@ -141,7 +140,8 @@
             JsonNode jsonTree = mapper().readTree(stream);
             JsonNode flow = jsonTree.get("flow_classifier");
             FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
-            Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND);
+            Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier),
+                    FLOW_CLASSIFIER_NOT_FOUND);
             return Response.status(OK).entity(result.toString()).build();
         } catch (IOException e) {
             log.error("Update flow classifier failed because of exception {}.", e.toString());
@@ -152,14 +152,16 @@
     /**
      * Delete details of a flow classifier.
      *
-     * @param id flow classifier id
+     * @param id
+     *            flow classifier id
      */
     @Path("{flow_id}")
     @DELETE
     public void deleteFlowClassifier(@PathParam("flow_id") String id) {
         log.debug("Deletes flow classifier by identifier {}.", id);
         FlowClassifierId flowClassifierId = FlowClassifierId.of(id);
-        Boolean issuccess = nullIsNotFound(service.removeFlowClassifier(flowClassifierId), FLOW_CLASSIFIER_NOT_FOUND);
+        Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId),
+                FLOW_CLASSIFIER_NOT_FOUND);
 
     }
 }
diff --git a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java
index 67f8f35..be645be 100644
--- a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java
+++ b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java
@@ -227,7 +227,7 @@
         final Set<FlowClassifier> flowClassifiers = new HashSet<>();
         flowClassifiers.add(flowClassifier1);
 
-        expect(flowClassifierService.hasFlowClassifier(anyObject())).andReturn(true).anyTimes();
+        expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
         expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
         replay(flowClassifierService);