Add a REST interface for configuring security group

Change-Id: I1469a22f90a1c28d7f7851ce9d6a816431fee292
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtil.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtil.java
index 0802c9e..e1edb6d 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtil.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtil.java
@@ -115,6 +115,9 @@
     private static final String PROXY_MODE = "proxy";
     private static final String BROADCAST_MODE = "broadcast";
 
+    private static final String ENABLE = "enable";
+    private static final String DISABLE = "disable";
+
     private static final int HTTP_PAYLOAD_BUFFER = 8 * 1024;
 
     private static final String HMAC_SHA256 = "HmacSHA256";
@@ -457,6 +460,24 @@
     }
 
     /**
+     * Checks the validity of activation flag.
+     *
+     * @param activationFlag activation flag
+     * @return returns true if the activation flag is valid, false otherwise
+     */
+    public static boolean checkActivationFlag(String activationFlag) {
+
+        switch (activationFlag) {
+            case ENABLE:
+                return true;
+            case DISABLE:
+                return false;
+            default:
+                throw new IllegalArgumentException("The given activation flag is not valid!");
+        }
+    }
+
+    /**
      * Swaps current location with old location info.
      * The revised instance port will be used to mod the flow rules after migration.
      *
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java
index 72f23574..8b335e6 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java
@@ -29,6 +29,7 @@
 import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService;
 import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
 import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler;
+import org.onosproject.openstacknetworking.impl.OpenstackSecurityGroupHandler;
 import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler;
 import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil;
 import org.onosproject.openstacknode.api.NodeState;
@@ -53,9 +54,7 @@
 
 import static java.lang.Thread.sleep;
 import static org.onlab.util.Tools.nullIsIllegal;
-import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface;
-import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode;
-import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue;
+import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.*;
 import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER;
 
 /**
@@ -67,6 +66,7 @@
 
     private static final String FLOATINGIPS = "floatingips";
     private static final String ARP_MODE_NAME = "arpMode";
+    private static final String USE_SECURITY_GROUP_NAME = "useSecurityGroup";
 
     private static final long SLEEP_MS = 3000; // we wait 3s for init each node
 
@@ -74,6 +74,8 @@
 
     private static final String ARP_MODE_REQUIRED = "ARP mode is not specified";
 
+    private static final String SECURITY_GROUP_FLAG_REQUIRED = "Security Group flag is not specified";
+
     private final ObjectNode root = mapper().createObjectNode();
     private final ArrayNode floatingipsNode = root.putArray(FLOATINGIPS);
 
@@ -252,6 +254,28 @@
     }
 
     /**
+     * Configures the security group (enable | disable).
+     *
+     * @param securityGroup security group activation flag
+     * @return 200 OK with config result, 404 not found
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("config/securityGroup/{securityGroup}")
+    public Response configSecurityGroup(@PathParam("securityGroup") String securityGroup) {
+        String securityGroupStr = nullIsIllegal(securityGroup, SECURITY_GROUP_FLAG_REQUIRED);
+
+        boolean flag = checkActivationFlag(securityGroupStr);
+
+        ComponentConfigService service = get(ComponentConfigService.class);
+        String securityGroupComponent = OpenstackSecurityGroupHandler.class.getName();
+
+        service.setProperty(securityGroupComponent, USE_SECURITY_GROUP_NAME, String.valueOf(flag));
+
+        return ok(mapper().createObjectNode()).build();
+    }
+
+    /**
      * Obtains a collection of all floating IPs.
      *
      * @return 200 OK with a collection of floating IPs, 404 not found
diff --git a/apps/openstacknetworking/app/src/test/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtilTest.java b/apps/openstacknetworking/app/src/test/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtilTest.java
index ed18a79..c6e403a 100644
--- a/apps/openstacknetworking/app/src/test/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtilTest.java
+++ b/apps/openstacknetworking/app/src/test/java/org/onosproject/openstacknetworking/util/OpenstackNetworkingUtilTest.java
@@ -78,6 +78,7 @@
 import static org.onosproject.openstacknetworking.api.Constants.UNSUPPORTED_VENDOR;
 import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface;
 import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.associatedFloatingIp;
+import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkActivationFlag;
 import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode;
 import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getConnectedClient;
 import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByComputeDevId;
@@ -472,6 +473,24 @@
         assertEquals(vnicType(portNameUnsupported), Constants.VnicType.UNSUPPORTED);
     }
 
+    /**
+     * Tests the checkActivationFlag method.
+     */
+    @Test
+    public void testCheckActivationFlag() {
+        assertFalse(checkActivationFlag("disable"));
+        assertTrue(checkActivationFlag("enable"));
+    }
+
+    /**
+     * Tests the checkActivationFlag method with incorrect input parameters.
+     */
+    @Test (expected = IllegalArgumentException.class)
+    public void testCheckActivationFlagWithException() {
+        checkActivationFlag("test");
+        checkActivationFlag(null);
+    }
+
     private DeviceId genDeviceId(int index) {
         return DeviceId.deviceId("of:compute-" + index);
     }