CORD-727 Implemented port name on OVS in ServicePort API

This port name is used to identify service port object when a new
VM port is detected on OVS.

Change-Id: Id17d846ed745af93423d6eee98ffd9561590dd88
diff --git a/src/main/java/org/opencord/cordvtn/api/net/ServicePort.java b/src/main/java/org/opencord/cordvtn/api/net/ServicePort.java
index 07db2e7..64e9a5a 100644
--- a/src/main/java/org/opencord/cordvtn/api/net/ServicePort.java
+++ b/src/main/java/org/opencord/cordvtn/api/net/ServicePort.java
@@ -34,7 +34,9 @@
     PortId id();
 
     /**
-     * Returns the port name.
+     * Returns the port name on a switch.
+     * This name is used as key for identifying a service port when a port is
+     * added or updated.
      *
      * @return port name
      */
diff --git a/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java b/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java
index a5c5db1..d78a466 100644
--- a/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java
+++ b/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java
@@ -39,7 +39,7 @@
         description = "Lists all VTN ports")
 public class CordVtnPortListCommand extends AbstractShellCommand {
 
-    private static final String FORMAT = "%-40s%-20s%-18s%-8s%s";
+    private static final String FORMAT = "%-40s%-30s%-20s%-18s%-10s%s";
 
     @Argument(name = "networkId", description = "Network ID")
     private String networkId = null;
@@ -61,16 +61,17 @@
                 print("Failed to list networks in JSON format");
             }
         } else {
-            print(FORMAT, "ID", "MAC", "IP", "VLAN", "WAN IPs");
+            print(FORMAT, "ID", "Name", "MAC", "IP", "VLAN", "WAN IPs");
             for (ServicePort port: ports) {
                 List<String> floatingIps = port.addressPairs().stream()
                         .map(ip -> ip.ip().toString())
                         .collect(Collectors.toList());
                 print(FORMAT, port.id(),
-                      port.mac(),
-                      port.ip(),
-                      port.vlanId() != null ? port.vlanId() : "",
-                      floatingIps.isEmpty() ? "" : floatingIps);
+                        port.name(),
+                        port.mac(),
+                        port.ip(),
+                        port.vlanId() != null ? port.vlanId() : "",
+                        floatingIps.isEmpty() ? "" : floatingIps);
             }
         }
     }
@@ -85,13 +86,14 @@
                             .put("mac", pair.mac().toString())));
 
             result.add(mapper().createObjectNode()
-                               .put("id", port.id().id())
-                               .put("networkId", port.networkId().id())
-                               .put("mac", port.mac().toString())
-                               .put("ip", port.ip().toString())
-                               .put("vlan", port.vlanId() != null ?
-                                       port.vlanId().toString() : null)
-                               .set("addressPairs", addrPairs));
+                    .put("id", port.id().id())
+                    .put("name", port.name())
+                    .put("networkId", port.networkId().id())
+                    .put("mac", port.mac().toString())
+                    .put("ip", port.ip().toString())
+                    .put("vlan", port.vlanId() != null ?
+                            port.vlanId().toString() : null)
+                    .set("addressPairs", addrPairs));
         }
         return result;
     }
diff --git a/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncNeutronStatesCommand.java b/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncNeutronStatesCommand.java
index c7dcdeb..397f48c 100644
--- a/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncNeutronStatesCommand.java
+++ b/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncNeutronStatesCommand.java
@@ -15,7 +15,6 @@
  */
 package org.opencord.cordvtn.cli;
 
-import com.google.common.base.Strings;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.onlab.packet.IpAddress;
@@ -63,8 +62,9 @@
             required = true, multiValued = false)
     private String password = null;
 
-    private static final String NET_FORMAT = "%-40s%-20s%-20s%-8s%-20s%s";
-    private static final String PORT_FORMAT = "%-40s%-20s%-18s%-8s%s";
+    private static final String PORT_NAME_PREFIX = "tap";
+    private static final String NET_FORMAT = "%-40s%-30s%-20s%-8s%-20s%s";
+    private static final String PORT_FORMAT = "%-40s%-30s%-20s%-18s%-10s%s";
 
     @Override
     protected void execute() {
@@ -127,15 +127,13 @@
         });
 
         print("\nSynchronizing service ports...");
-        print(PORT_FORMAT, "ID", "MAC", "IP", "VLAN", "WAN IPs");
+        print(PORT_FORMAT, "ID", "Name", "MAC", "IP", "VLAN", "WAN IPs");
         osClient.networking().port().list().forEach(osPort -> {
             ServicePort.Builder sportBuilder = DefaultServicePort.builder()
                     .id(PortId.of(osPort.getId()))
+                    .name(PORT_NAME_PREFIX + osPort.getId().substring(0, 11))
                     .networkId(NetworkId.of(osPort.getNetworkId()));
 
-            if (!Strings.isNullOrEmpty(osPort.getName())) {
-                sportBuilder.name(osPort.getName());
-            }
             if (osPort.getMacAddress() != null) {
                 sportBuilder.mac(MacAddress.valueOf(osPort.getMacAddress()));
             }
@@ -154,10 +152,11 @@
                     .map(ip -> ip.ip().toString())
                     .collect(Collectors.toList());
             print(PORT_FORMAT, updated.id(),
-                  updated.mac(),
-                  updated.ip(),
-                  updated.vlanId() != null ? updated.vlanId() : "",
-                  floatingIps.isEmpty() ? "" : floatingIps);
+                    updated.name(),
+                    updated.mac(),
+                    updated.ip(),
+                    updated.vlanId() != null ? updated.vlanId() : "",
+                    floatingIps.isEmpty() ? "" : floatingIps);
         });
     }
 }
diff --git a/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncXosStatesCommand.java b/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncXosStatesCommand.java
index b3fc988..a5f5610 100644
--- a/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncXosStatesCommand.java
+++ b/src/main/java/org/opencord/cordvtn/cli/CordVtnSyncXosStatesCommand.java
@@ -47,8 +47,8 @@
             required = true, multiValued = false)
     private String password = null;
 
-    private static final String NET_FORMAT = "%-40s%-20s%-20s%-8s%-20s%s";
-    private static final String PORT_FORMAT = "%-40s%-20s%-18s%-8s%s";
+    private static final String NET_FORMAT = "%-40s%-30s%-20s%-8s%-20s%s";
+    private static final String PORT_FORMAT = "%-40s%-30s%-20s%-18s%-10s%s";
 
     @Override
     protected void execute() {
@@ -80,7 +80,7 @@
 
         // FIXME creating a port fails until XOS service API provides network ID
         print("\nSynchronizing service ports...");
-        print(PORT_FORMAT, "ID", "MAC", "IP", "VLAN", "WAN IPs");
+        print(PORT_FORMAT, "ID", "Name", "MAC", "IP", "VLAN", "WAN IPs");
         client.servicePorts().forEach(sport -> {
             if (snetService.servicePort(sport.id()) != null) {
                 snetService.updateServicePort(sport);
@@ -92,10 +92,11 @@
                     .map(ip -> ip.ip().toString())
                     .collect(Collectors.toList());
             print(PORT_FORMAT, updated.id(),
-                  updated.mac(),
-                  updated.ip(),
-                  updated.vlanId() != null ? updated.vlanId() : "",
-                  floatingIps.isEmpty() ? "" : floatingIps);
+                    updated.name(),
+                    updated.mac(),
+                    updated.ip(),
+                    updated.vlanId() != null ? updated.vlanId() : "",
+                    floatingIps.isEmpty() ? "" : floatingIps);
         });
     }
 }
diff --git a/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java b/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java
index 80f21ee..e8983c6 100644
--- a/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java
+++ b/src/main/java/org/opencord/cordvtn/impl/InstanceManager.java
@@ -216,7 +216,7 @@
     private ServicePort getServicePortByPortName(String portName) {
         Optional<ServicePort> sport = snetService.servicePorts()
                 .stream()
-                .filter(p -> p.id().id().contains(portName.substring(3)))
+                .filter(p -> Objects.equals(p.name(), portName))
                 .findFirst();
         return sport.isPresent() ? sport.get() : null;
     }
diff --git a/src/main/java/org/opencord/cordvtn/impl/ServiceNetworkManager.java b/src/main/java/org/opencord/cordvtn/impl/ServiceNetworkManager.java
index aaa74e5..1ac5f16 100644
--- a/src/main/java/org/opencord/cordvtn/impl/ServiceNetworkManager.java
+++ b/src/main/java/org/opencord/cordvtn/impl/ServiceNetworkManager.java
@@ -77,6 +77,7 @@
     private static final String ERR_NULL_SERVICE_NET_TYPE  = "Service network type cannot be null";
     private static final String ERR_NULL_SERVICE_PORT = "Service port cannot be null";
     private static final String ERR_NULL_SERVICE_PORT_ID = "Service port ID cannot be null";
+    private static final String ERR_NULL_SERVICE_PORT_NAME = "Service port name cannot be null";
     private static final String ERR_NULL_SERVICE_PORT_NET_ID = "Service port network ID cannot be null";
 
     private static final String ERR_NOT_FOUND = " does not exist";
@@ -218,6 +219,7 @@
     public void createServicePort(ServicePort sport) {
         checkNotNull(sport, ERR_NULL_SERVICE_PORT);
         checkNotNull(sport.id(), ERR_NULL_SERVICE_PORT_ID);
+        checkNotNull(sport.id(), ERR_NULL_SERVICE_PORT_NAME);
         checkNotNull(sport.networkId(), ERR_NULL_SERVICE_PORT_NET_ID);
         synchronized (this) {
             ServiceNetwork existing = snetStore.serviceNetwork(sport.networkId());
diff --git a/src/main/java/org/opencord/cordvtn/rest/NeutronMl2PortsWebResource.java b/src/main/java/org/opencord/cordvtn/rest/NeutronMl2PortsWebResource.java
index 8776e39..4e11c1f 100644
--- a/src/main/java/org/opencord/cordvtn/rest/NeutronMl2PortsWebResource.java
+++ b/src/main/java/org/opencord/cordvtn/rest/NeutronMl2PortsWebResource.java
@@ -16,7 +16,6 @@
 package org.opencord.cordvtn.rest;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import com.google.common.base.Strings;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
@@ -61,6 +60,7 @@
 
     private static final String MESSAGE = "Received ports %s request";
     private static final String PORTS = "ports";
+    private static final String PORT_NAME_PREFIX = "tap";
 
     private final ServiceNetworkAdminService adminService =
             DefaultServiceDirectory.getService(ServiceNetworkAdminService.class);
@@ -105,8 +105,8 @@
     public Response updatePort(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE, "UPDATE " + id));
 
-        final ServicePort port = readPort(input);
-        adminService.updateServicePort(port);
+        final ServicePort sport = readPort(input);
+        adminService.updateServicePort(sport);
 
         return status(OK).build();
     }
@@ -138,11 +138,9 @@
 
             ServicePort.Builder sportBuilder = DefaultServicePort.builder()
                     .id(PortId.of(osPort.getId()))
+                    .name(PORT_NAME_PREFIX + osPort.getId().substring(0, 11))
                     .networkId(NetworkId.of(osPort.getNetworkId()));
 
-            if (!Strings.isNullOrEmpty(osPort.getName())) {
-                sportBuilder.name(osPort.getName());
-            }
             if (osPort.getMacAddress() != null) {
                 sportBuilder.mac(MacAddress.valueOf(osPort.getMacAddress()));
             }