[ONOS-3624] update vtnweb's bug and add Not-Null constraints of export.

Change-Id: I7699d8766f0653f5fafd4b32a68051994f43b00e
diff --git a/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/manager/impl/VTNManager.java b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/manager/impl/VTNManager.java
index 53fa64a..ac898d1 100644
--- a/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/manager/impl/VTNManager.java
+++ b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/manager/impl/VTNManager.java
@@ -413,7 +413,9 @@
         if (type == Objective.Operation.ADD) {
             // Save external port
             Port export = getExPort(device.id());
-            exPortOfDevice.put(device.id(), export);
+            if (export != null) {
+                exPortOfDevice.put(device.id(), export);
+            }
             switchOfLocalHostPorts.put(device.id(), new NetworkOfLocalHostPorts());
         } else if (type == Objective.Operation.REMOVE) {
             exPortOfDevice.remove(device.id());
@@ -777,7 +779,7 @@
         for (RouterInterface r : interfacesSet) {
             // Get all the host of the subnet
             Map<HostId, Host> hosts = hostsOfSubnet.get(r.subnetId());
-            if (hosts.size() > 0) {
+            if (hosts != null && hosts.size() > 0) {
                 subnetVmNum++;
                 if (subnetVmNum >= SUBNET_NUM) {
                     routerInfFlagOfTenant.put(r.tenantId(), true);
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/routerinterface/impl/RouterInterfaceManager.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/routerinterface/impl/RouterInterfaceManager.java
index 244a5c0..bbcc04f 100644
--- a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/routerinterface/impl/RouterInterfaceManager.java
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/routerinterface/impl/RouterInterfaceManager.java
@@ -134,6 +134,12 @@
     @Override
     public boolean addRouterInterface(RouterInterface routerInterface) {
         checkNotNull(routerInterface, ROUTER_INTERFACE_NULL);
+        if (!virtualPortService.exists(routerInterface.portId())) {
+            log.debug("The port ID of interface is not exist whose identifier is {}",
+                      routerInterface.portId().toString());
+            throw new IllegalArgumentException(
+                                               "port ID of interface doesn't exist");
+        }
         verifyRouterInterfaceData(routerInterface);
         routerInterfaceStore.put(routerInterface.subnetId(), routerInterface);
         if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) {
@@ -188,12 +194,6 @@
             throw new IllegalArgumentException(
                                                "subnet ID of interface doesn't exist");
         }
-        if (!virtualPortService.exists(routerInterface.portId())) {
-            log.debug("The port ID of interface is not exist whose identifier is {}",
-                      routerInterface.portId().toString());
-            throw new IllegalArgumentException(
-                                               "port ID of interface doesn't exist");
-        }
         if (!routerService.exists(routerInterface.routerId())) {
             log.debug("The router ID of interface is not exist whose identifier is {}",
                       routerInterface.routerId().toString());
diff --git a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java
index 6f80dd1..dad611e 100644
--- a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java
+++ b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java
@@ -104,8 +104,7 @@
                     .entity("The Router does not exists").build();
         }
         Router sub = nullIsNotFound(get(RouterService.class)
-                                            .getRouter(RouterId.valueOf(id)),
-                                    NOT_EXIST);
+                .getRouter(RouterId.valueOf(id)), NOT_EXIST);
 
         ObjectNode result = new ObjectMapper().createObjectNode();
         if (fields.size() > 0) {
@@ -127,8 +126,7 @@
             Collection<Router> routers = createOrUpdateByInputStream(subnode);
 
             Boolean result = nullIsNotFound((get(RouterService.class)
-                                                    .createRouters(routers)),
-                                            CREATE_FAIL);
+                    .createRouters(routers)), CREATE_FAIL);
             if (!result) {
                 return Response.status(CONFLICT).entity(CREATE_FAIL).build();
             }
@@ -148,7 +146,7 @@
         try {
             ObjectMapper mapper = new ObjectMapper();
             JsonNode subnode = mapper.readTree(input);
-            Collection<Router> routers = createOrUpdateByInputStream(subnode);
+            Collection<Router> routers = changeUpdateJsonToSub(subnode, id);
             Boolean result = nullIsNotFound(get(RouterService.class)
                     .updateRouters(routers), UPDATE_FAIL);
             if (!result) {
@@ -197,22 +195,22 @@
             } else if (subnode.get("subnet_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("subnet_id should not be empty");
             }
-            SubnetId subnetId = SubnetId.subnetId(subnode.get("subnet_id")
-                    .asText());
+            SubnetId subnetId = SubnetId
+                    .subnetId(subnode.get("subnet_id").asText());
             if (!subnode.hasNonNull("tenant_id")) {
                 throw new IllegalArgumentException("tenant_id should not be null");
             } else if (subnode.get("tenant_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("tenant_id should not be empty");
             }
-            TenantId tenentId = TenantId.tenantId(subnode.get("tenant_id")
-                    .asText());
+            TenantId tenentId = TenantId
+                    .tenantId(subnode.get("tenant_id").asText());
             if (!subnode.hasNonNull("port_id")) {
                 throw new IllegalArgumentException("port_id should not be null");
             } else if (subnode.get("port_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("port_id should not be empty");
             }
-            VirtualPortId portId = VirtualPortId.portId(subnode.get("port_id")
-                    .asText());
+            VirtualPortId portId = VirtualPortId
+                    .portId(subnode.get("port_id").asText());
             RouterInterface routerInterface = RouterInterface
                     .routerInterface(subnetId, portId, routerId, tenentId);
             get(RouterInterfaceService.class)
@@ -246,22 +244,22 @@
             } else if (subnode.get("subnet_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("subnet_id should not be empty");
             }
-            SubnetId subnetId = SubnetId.subnetId(subnode.get("subnet_id")
-                    .asText());
+            SubnetId subnetId = SubnetId
+                    .subnetId(subnode.get("subnet_id").asText());
             if (!subnode.hasNonNull("port_id")) {
                 throw new IllegalArgumentException("port_id should not be null");
             } else if (subnode.get("port_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("port_id should not be empty");
             }
-            VirtualPortId portId = VirtualPortId.portId(subnode.get("port_id")
-                    .asText());
+            VirtualPortId portId = VirtualPortId
+                    .portId(subnode.get("port_id").asText());
             if (!subnode.hasNonNull("tenant_id")) {
                 throw new IllegalArgumentException("tenant_id should not be null");
             } else if (subnode.get("tenant_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("tenant_id should not be empty");
             }
-            TenantId tenentId = TenantId.tenantId(subnode.get("tenant_id")
-                    .asText());
+            TenantId tenentId = TenantId
+                    .tenantId(subnode.get("tenant_id").asText());
             RouterInterface routerInterface = RouterInterface
                     .routerInterface(subnetId, portId, routerId, tenentId);
             get(RouterInterfaceService.class)
@@ -311,13 +309,13 @@
         } else if (routerNode.get("tenant_id").asText().isEmpty()) {
             throw new IllegalArgumentException("tenant_id should not be empty");
         }
-        TenantId tenantId = TenantId.tenantId(routerNode.get("tenant_id")
-                .asText());
+        TenantId tenantId = TenantId
+                .tenantId(routerNode.get("tenant_id").asText());
 
         VirtualPortId gwPortId = null;
         if (routerNode.hasNonNull("gw_port_id")) {
-            gwPortId = VirtualPortId.portId(routerNode.get("gw_port_id")
-                    .asText());
+            gwPortId = VirtualPortId
+                    .portId(routerNode.get("gw_port_id").asText());
         }
 
         if (!routerNode.hasNonNull("status")) {
@@ -344,7 +342,59 @@
         }
         RouterGateway gateway = null;
         if (routerNode.hasNonNull("external_gateway_info")) {
-            gateway = jsonNodeToGateway(routerNode.get("external_gateway_info"));
+            gateway = jsonNodeToGateway(routerNode
+                    .get("external_gateway_info"));
+        }
+        List<String> routes = new ArrayList<String>();
+        DefaultRouter routerObj = new DefaultRouter(id, routerName,
+                                                    adminStateUp, status,
+                                                    distributed, gateway,
+                                                    gwPortId, tenantId, routes);
+        subMap.put(id, routerObj);
+        return Collections.unmodifiableCollection(subMap.values());
+    }
+
+    /**
+     * Returns a collection of floatingIps from floatingIpNodes.
+     *
+     * @param subnode the router json node
+     * @param routerId the router identify
+     * @return routers a collection of router
+     * @throws Exception when any argument is illegal
+     */
+    public Collection<Router> changeUpdateJsonToSub(JsonNode subnode,
+                                                    String routerId)
+                                                            throws Exception {
+        checkNotNull(subnode, JSON_NOT_NULL);
+        checkNotNull(routerId, "routerId should not be null");
+        Map<RouterId, Router> subMap = new HashMap<RouterId, Router>();
+        JsonNode routerNode = subnode.get("router");
+        RouterId id = RouterId.valueOf(routerId);
+        Router sub = nullIsNotFound(get(RouterService.class).getRouter(id),
+                                    NOT_EXIST);
+        TenantId tenantId = sub.tenantId();
+
+        VirtualPortId gwPortId = null;
+        if (routerNode.hasNonNull("gw_port_id")) {
+            gwPortId = VirtualPortId
+                    .portId(routerNode.get("gw_port_id").asText());
+        }
+        Status status = sub.status();
+
+        String routerName = routerNode.get("name").asText();
+
+        checkArgument(routerNode.get("admin_state_up").isBoolean(),
+                      "admin_state_up should be boolean");
+        boolean adminStateUp = routerNode.get("admin_state_up").asBoolean();
+
+        boolean distributed = sub.distributed();
+        if (routerNode.hasNonNull("distributed")) {
+            distributed = routerNode.get("distributed").asBoolean();
+        }
+        RouterGateway gateway = sub.externalGatewayInfo();
+        if (routerNode.hasNonNull("external_gateway_info")) {
+            gateway = jsonNodeToGateway(routerNode
+                    .get("external_gateway_info"));
         }
         List<String> routes = new ArrayList<String>();
         DefaultRouter routerObj = new DefaultRouter(id, routerName,
@@ -368,8 +418,8 @@
         } else if (gateway.get("network_id").asText().isEmpty()) {
             throw new IllegalArgumentException("network_id should not be empty");
         }
-        TenantNetworkId networkId = TenantNetworkId.networkId(gateway
-                .get("network_id").asText());
+        TenantNetworkId networkId = TenantNetworkId
+                .networkId(gateway.get("network_id").asText());
 
         if (!gateway.hasNonNull("enable_snat")) {
             throw new IllegalArgumentException("enable_snat should not be null");
@@ -381,17 +431,14 @@
         boolean enableSnat = gateway.get("enable_snat").asBoolean();
 
         if (!gateway.hasNonNull("external_fixed_ips")) {
-            throw new IllegalArgumentException(
-                                          "external_fixed_ips should not be null");
+            throw new IllegalArgumentException("external_fixed_ips should not be null");
         } else if (gateway.get("external_fixed_ips").isNull()) {
-            throw new IllegalArgumentException(
-                                          "external_fixed_ips should not be empty");
+            throw new IllegalArgumentException("external_fixed_ips should not be empty");
         }
         Collection<FixedIp> fixedIpList = jsonNodeToFixedIp(gateway
                 .get("external_fixed_ips"));
-        RouterGateway gatewayObj = RouterGateway.routerGateway(networkId,
-                                                               enableSnat,
-                                                               fixedIpList);
+        RouterGateway gatewayObj = RouterGateway
+                .routerGateway(networkId, enableSnat, fixedIpList);
         return gatewayObj;
     }
 
@@ -411,15 +458,15 @@
             } else if (node.get("subnet_id").asText().isEmpty()) {
                 throw new IllegalArgumentException("subnet_id should not be empty");
             }
-            SubnetId subnetId = SubnetId.subnetId(node.get("subnet_id")
-                    .asText());
+            SubnetId subnetId = SubnetId
+                    .subnetId(node.get("subnet_id").asText());
             if (!node.hasNonNull("ip_address")) {
                 throw new IllegalArgumentException("ip_address should not be null");
             } else if (node.get("ip_address").asText().isEmpty()) {
                 throw new IllegalArgumentException("ip_address should not be empty");
             }
-            IpAddress ipAddress = IpAddress.valueOf(node.get("ip_address")
-                    .asText());
+            IpAddress ipAddress = IpAddress
+                    .valueOf(node.get("ip_address").asText());
             FixedIp fixedIpObj = FixedIp.fixedIp(subnetId, ipAddress);
 
             fixedIpMaps.putIfAbsent(i, fixedIpObj);