SONA: Openstackswitching
 - Refactored the app to expose APIs to CordVtn App
 - Added clone method in OpenstackPort and OpenstackNetwork
 - Added NetworkConfig to select the working mode of the app
 - Added a few more APIs for getting network topology information
 - Integrated with the modified DhcpService app
Change-Id: I9e266aff10a00d80074d031276864fff195d2b3f
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java
new file mode 100644
index 0000000..fc1509d
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkCodec.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstackswitching.OpenstackNetwork;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of the OpenstackNetwork Codec.
+ *
+ */
+public class OpenstackNetworkCodec extends JsonCodec<OpenstackNetwork> {
+
+    protected static final Logger log = LoggerFactory
+            .getLogger(OpenstackNetworkCodec.class);
+
+    private static final String NETWORK = "network";
+    private static final String NAME = "name";
+    private static final String TENANT_ID = "tenant_id";
+    private static final String SEGMENTATION_ID = "provider:segmentation_id";
+    private static final String NETWORK_TYPE = "provider:network_type";
+    private static final String ID = "id";
+
+    @Override
+    public OpenstackNetwork decode(ObjectNode json, CodecContext context) {
+
+        JsonNode networkInfo = json.get(NETWORK);
+
+        String name = networkInfo.path(NAME).asText();
+        String tenantId = networkInfo.path(TENANT_ID).asText();
+        String id = networkInfo.path(ID).asText();
+
+        OpenstackNetwork.Builder onb = OpenstackNetwork.builder();
+        onb.name(name)
+                .tenantId(tenantId)
+                .id(id);
+
+        if (!networkInfo.path(NETWORK_TYPE).isMissingNode()) {
+            onb.networkType(OpenstackNetwork.NetworkType.valueOf(networkInfo.path(NETWORK_TYPE).
+                    asText().toUpperCase()));
+            onb.name(networkInfo.path(NETWORK_TYPE).asText());
+            onb.segmentId(networkInfo.path(SEGMENTATION_ID).asText());
+        }
+
+        return onb.build();
+    }
+
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java
new file mode 100644
index 0000000..f4c401f
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackNetworkWebResource.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.openstackswitching.OpenstackNetwork;
+import org.onosproject.openstackswitching.OpenstackSwitchingService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+@Path("networks")
+public class OpenstackNetworkWebResource extends AbstractWebResource {
+
+    protected static final Logger log = LoggerFactory
+            .getLogger(OpenstackNetworkWebResource.class);
+
+    private static final OpenstackNetworkCodec NETWORK_CODEC = new OpenstackNetworkCodec();
+
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response createNetwork(InputStream input) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode networkNode = (ObjectNode) mapper.readTree(input);
+
+            OpenstackNetwork openstackNetwork = NETWORK_CODEC.decode(networkNode, this);
+
+            OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class);
+            switchingService.createNetwork(openstackNetwork);
+            return Response.status(Response.Status.OK).build();
+        } catch (Exception e) {
+            log.error("Creates VirtualPort failed because of exception {}",
+                    e.toString());
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+                    .build();
+        }
+    }
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java
new file mode 100644
index 0000000..94a1edb
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortCodec.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.MacAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstackswitching.OpenstackPort;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+
+/**
+ * Encodes and decodes the OpenstackPort.
+ */
+public class OpenstackPortCodec extends JsonCodec<OpenstackPort> {
+
+    private static Logger log = LoggerFactory
+            .getLogger(OpenstackPortCodec.class);
+
+    // JSON field names
+    private static final String PORT = "port";
+    private static final String STATUS = "status";
+    private static final String NAME = "name";
+    private static final String ADDRESS_PAIR = "allowed_address_pairs";
+    private static final String ADMIN_STATUS = "admin_status";
+    private static final String NETWORK_ID = "network_id";
+    private static final String TENANT_ID = "tenant_id";
+    private static final String DEVICE_OWNER = "device_owner";
+    private static final String MAC_ADDRESS = "mac_address";
+    private static final String FIXED_IPS = "fixed_ips";
+    private static final String SUBNET_ID = "subnet_id";
+    private static final String IP_ADDRESS = "ip_address";
+    private static final String ID = "id";
+    private static final String SECURITY_GROUPS = "security_groups";
+    private static final String DEVICE_ID = "device_id";
+
+    @Override
+    public OpenstackPort decode(ObjectNode json, CodecContext context) {
+
+        HashMap<String, Ip4Address> fixedIpMap = new HashMap<>();
+        JsonNode portInfo = json.get(PORT);
+
+        String status = portInfo.path(STATUS).asText();
+        String name = portInfo.path(NAME).asText();
+        boolean adminStateUp = portInfo.path(ADMIN_STATUS).asBoolean();
+        String networkId = portInfo.path(NETWORK_ID).asText();
+        String tenantId = portInfo.path(TENANT_ID).asText();
+        String deviceOwner = portInfo.path(DEVICE_OWNER).asText();
+        String macStr = portInfo.path(MAC_ADDRESS).asText();
+        ArrayNode fixedIpList = (ArrayNode) portInfo.path(FIXED_IPS);
+        for (JsonNode fixedIpInfo: fixedIpList) {
+            String subnetId = fixedIpInfo.path(SUBNET_ID).asText();
+            String ipAddressStr = fixedIpInfo.path(IP_ADDRESS).asText();
+            if (ipAddressStr != null) {
+                Ip4Address ipAddress = Ip4Address.valueOf(ipAddressStr);
+                fixedIpMap.put(subnetId, ipAddress);
+            }
+        }
+        String id = portInfo.path(ID).asText();
+        String securityGroups = portInfo.path(SECURITY_GROUPS).asText();
+        String deviceId = portInfo.path(DEVICE_ID).asText();
+
+        OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder();
+        openstackPortBuilder.portStatus(OpenstackPort.PortStatus.valueOf(status))
+                .name(name)
+                .adminState(adminStateUp)
+                .netwrokId(networkId)
+                .tenantId(tenantId)
+                .deviceOwner(deviceOwner)
+                .macAddress(MacAddress.valueOf(macStr))
+                .fixedIps(fixedIpMap)
+                .id(id)
+                .deviceId(deviceId);
+
+        // FIX ME
+        if (!securityGroups.isEmpty()) {
+            openstackPortBuilder.securityGroup(securityGroups);
+        }
+
+        OpenstackPort openstackPort = openstackPortBuilder.build();
+
+        return openstackPort;
+    }
+
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java
new file mode 100644
index 0000000..e531ba4
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.openstackswitching.OpenstackPort;
+import org.onosproject.openstackswitching.OpenstackSwitchingService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+@Path("ports")
+public class OpenstackPortWebResource extends AbstractWebResource {
+
+    protected static final Logger log = LoggerFactory
+            .getLogger(OpenstackPortWebResource.class);
+
+    private static final OpenstackPortCodec PORT_CODEC = new OpenstackPortCodec();
+
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response createPorts(InputStream input) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode portNode = (ObjectNode) mapper.readTree(input);
+
+            OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this);
+
+            OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class);
+            switchingService.createPorts(openstackPort);
+            log.debug("REST API ports is called with {}", portNode.toString());
+            return Response.status(Response.Status.OK).build();
+        } catch (Exception e) {
+            log.error("Creates VirtualPort failed because of exception {}",
+                    e.toString());
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+                    .build();
+        }
+    }
+
+    @DELETE
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response deletesPorts(InputStream input) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode portNode = (ObjectNode) mapper.readTree(input);
+
+            OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class);
+            switchingService.deletePorts();
+            log.info("REST API ports is called with {}", portNode.toString());
+            return Response.status(Response.Status.OK).build();
+        } catch (Exception e) {
+            log.error("Delete VirtualPort failed because of exception {}",
+                    e.toString());
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+                    .build();
+        }
+    }
+
+    @PUT
+    @Path("{id}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response updatePorts(InputStream input) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode portNode = (ObjectNode) mapper.readTree(input);
+
+            OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class);
+            switchingService.updatePorts();
+            log.info("REST API ports is called with {}", portNode.toString());
+            return Response.status(Response.Status.OK).build();
+        } catch (Exception e) {
+            log.error("Update VirtualPort failed because of exception {}",
+                    e.toString());
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+                    .build();
+        }
+    }
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetCodec.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetCodec.java
new file mode 100644
index 0000000..e504550
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetCodec.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstackswitching.OpenstackSubnet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * Encodes and decodes the OpenstackSubnet.
+ */
+public class OpenstackSubnetCodec extends JsonCodec<OpenstackSubnet> {
+    private static Logger log = LoggerFactory
+            .getLogger(OpenstackSubnetCodec.class);
+
+    // JSON Field names
+    private static final String SUBNET = "subnet";
+    private static final String NAME = "name";
+    private static final String ENABLE_DHCP = "enable_dhcp";
+    private static final String NETWORK_ID = "network_id";
+    private static final String TENANT_ID = "tenant_id";
+    private static final String DNS_NAMESERVERS = "dns_nameservers";
+    private static final String GATEWAY_IP = "gateway_ip";
+    private static final String CIDR = "cidr";
+    private static final String ID = "id";
+
+    @Override
+    public OpenstackSubnet decode(ObjectNode json, CodecContext context) {
+        JsonNode subnetInfo = json.get(SUBNET);
+
+        String name = subnetInfo.path(NAME).asText();
+        boolean enableDhcp = subnetInfo.path(ENABLE_DHCP).asBoolean();
+        String networkId = subnetInfo.path(NETWORK_ID).asText();
+        String tenantId = subnetInfo.path(TENANT_ID).asText();
+        ArrayNode dnsNameservsers = (ArrayNode) subnetInfo.path(DNS_NAMESERVERS);
+        List<Ip4Address> dnsList = Lists.newArrayList();
+        if (dnsNameservsers != null && !dnsNameservsers.isMissingNode()) {
+            dnsNameservsers.forEach(dns -> dnsList.add(Ip4Address.valueOf(dns.asText())));
+        }
+        String gatewayIp = subnetInfo.path(GATEWAY_IP).asText();
+        String cidr = subnetInfo.path(CIDR).asText();
+        String id = subnetInfo.path(ID).asText();
+
+        OpenstackSubnet openstackSubnet = OpenstackSubnet.builder()
+                .setName(name)
+                .setEnableDhcp(enableDhcp)
+                .setNetworkId(networkId)
+                .setTenantId(tenantId)
+                .setDnsNameservers(dnsList)
+                .setGatewayIp(gatewayIp)
+                .setCidr(cidr)
+                .setId(id)
+                .build();
+        return openstackSubnet;
+    }
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetWebResource.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetWebResource.java
new file mode 100644
index 0000000..bc0bb8e
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackSubnetWebResource.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.openstackswitching.OpenstackSubnet;
+import org.onosproject.openstackswitching.OpenstackSwitchingService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+@Path("subnets")
+public class OpenstackSubnetWebResource extends AbstractWebResource {
+    protected static final Logger log = LoggerFactory
+            .getLogger(OpenstackSubnetWebResource.class);
+
+    private static final OpenstackSubnetCodec SUBNET_CODEC = new OpenstackSubnetCodec();
+
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response createSubnet(InputStream input) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode subnetNode = (ObjectNode) mapper.readTree(input);
+
+            OpenstackSubnet openstackSubnet = SUBNET_CODEC.decode(subnetNode, this);
+
+            OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class);
+            switchingService.createSubnet(openstackSubnet);
+            log.debug("REST API subnets is called with {}", subnetNode.toString());
+            return Response.status(Response.Status.OK).build();
+        } catch (Exception e) {
+            log.error("Creates VirtualSubnet failed because of exception {}",
+                    e.toString());
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+                    .build();
+        }
+    }
+
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/package-info.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/package-info.java
new file mode 100644
index 0000000..91e19c6
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * OpenStack switching REST API.
+ */
+package org.onosproject.openstackswitching.web;