ONOS-5503 Added CLIs for administering OFAgent

Change-Id: I58256316e2054952da9dce04bf927901761807e5
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/impl/OFAgentManager.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/impl/OFAgentManager.java
index ce7c30f..454f7d7 100644
--- a/apps/ofagent/src/main/java/org/onosproject/ofagent/impl/OFAgentManager.java
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/impl/OFAgentManager.java
@@ -67,6 +67,8 @@
     private static final String MSG_CREATED = "created";
     private static final String MSG_UPDATED = "updated";
     private static final String MSG_REMOVED = "removed";
+    private static final String MSG_STARTED = "started";
+    private static final String MSG_STOPPED = "stopped";
     private static final String MSG_IN_STARTED = "is already in active state, do nothing";
     private static final String MSG_IN_STOPPED = "is already in inactive state, do nothing";
 
@@ -117,7 +119,9 @@
         leadershipService.removeListener(leadershipListener);
         virtualNetService.removeListener(virtualNetListener);
         ofAgentStore.unsetDelegate(delegate);
-        ofAgentStore.ofAgents().forEach(ofAgent -> stopAgent(ofAgent.networkId()));
+        ofAgentStore.ofAgents().stream()
+                .filter(ofAgent -> ofAgent.state() == STARTED)
+                .forEach(ofAgent -> stopAgent(ofAgent.networkId()));
 
         eventExecutor.shutdown();
         leadershipService.withdraw(appId.name());
@@ -132,6 +136,7 @@
             log.warn(String.format(MSG_OFAGENT, ofAgent.networkId(), ERR_IN_USE));
             return;
         }
+        // TODO check if the virtual network exists
         ofAgentStore.createOfAgent(ofAgent);
         log.info(String.format(MSG_OFAGENT, ofAgent.networkId(), MSG_CREATED));
     }
@@ -144,7 +149,7 @@
     }
 
     @Override
-    public void removeAgent(NetworkId networkId) {
+    public OFAgent removeAgent(NetworkId networkId) {
         checkNotNull(networkId, ERR_NULL_NETID);
         synchronized (this) {
             OFAgent existing = ofAgentStore.ofAgent(networkId);
@@ -156,8 +161,8 @@
                 final String error = String.format(MSG_OFAGENT, networkId, ERR_IN_USE);
                 throw new IllegalStateException(error);
             }
-            ofAgentStore.removeOfAgent(networkId);
             log.info(String.format(MSG_OFAGENT, networkId, MSG_REMOVED));
+            return ofAgentStore.removeOfAgent(networkId);
         }
     }
 
@@ -171,11 +176,13 @@
                 throw new IllegalStateException(error);
             }
             if (existing.state() == STARTED) {
-                log.warn(String.format(MSG_OFAGENT, networkId, MSG_IN_STARTED));
-                return;
+                final String error = String.format(MSG_OFAGENT, networkId, MSG_IN_STARTED);
+                throw new IllegalStateException(error);
             }
-            OFAgent updated = DefaultOFAgent.builder(existing).state(STARTED).build();
+            OFAgent updated = DefaultOFAgent.builder()
+                    .from(existing).state(STARTED).build();
             ofAgentStore.updateOfAgent(updated);
+            log.info(String.format(MSG_OFAGENT, networkId, MSG_STARTED));
         }
     }
 
@@ -189,11 +196,13 @@
                 throw new IllegalStateException(error);
             }
             if (existing.state() == STOPPED) {
-                log.warn(String.format(MSG_OFAGENT, networkId, MSG_IN_STOPPED));
-                return;
+                final String error = String.format(MSG_OFAGENT, networkId, MSG_IN_STOPPED);
+                throw new IllegalStateException(error);
             }
-            OFAgent updated = DefaultOFAgent.builder(existing).state(STOPPED).build();
+            OFAgent updated = DefaultOFAgent.builder()
+                    .from(existing).state(STOPPED).build();
             ofAgentStore.updateOfAgent(updated);
+            log.info(String.format(MSG_OFAGENT, networkId, MSG_STOPPED));
         }
     }