[CORD-2214] Fixing Pushing UNI to Global

Change-Id: I22516c30470cc628343b691d3cf8cca5eee158bb
diff --git a/global/config-samples/ecord-global-config.json b/global/config-samples/ecord-global-config.json
index 156c23a..9dba092 100644
--- a/global/config-samples/ecord-global-config.json
+++ b/global/config-samples/ecord-global-config.json
@@ -4,8 +4,7 @@
       "xos" : {
         "username" : "xosadmin@opencord.org",
         "password" : "0Ui8QNJNCdXrjjLpF1U6",
-        "address" : "127.0.0.1",
-        "resource" : "/xosapi/v1/vnaas/usernetworkinterfaces/"
+        "endpoint" : "10.90.1.10:80/xosapi/v1/vnaas/usernetworkinterfaces"
       }
     },
     "org.opencord.ce.global.channel.http" : {
diff --git a/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/VirtualDomainDeviceProvider.java b/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/VirtualDomainDeviceProvider.java
index 36daf56..e67e804 100644
--- a/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/VirtualDomainDeviceProvider.java
+++ b/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/VirtualDomainDeviceProvider.java
@@ -24,6 +24,8 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onosproject.net.config.basics.SubjectFactories;
+import org.onosproject.store.service.AtomicCounter;
+import org.onosproject.store.service.StorageService;
 import org.opencord.ce.api.services.virtualprovider.DomainVirtualDevice;
 import org.opencord.ce.api.services.virtualprovider.EcordDeviceProviderService;
 import org.onlab.packet.ChassisId;
@@ -88,7 +90,7 @@
     private static final String UNKNOWN = "unknown";
     private static final String NO_LLDP = "no-lldp";
     private static final String DOMAIN_ID = "domainId";
-
+    private static final String LATLNG_COUNTER = "ecord-latlng-counter";
 
     private ApplicationId appId;
 
@@ -111,12 +113,16 @@
     protected ClusterService clusterService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected StorageService storageService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected MetroOrchestrationService metroOrchestrationService;
 
     protected DeviceProviderService deviceProviderService;
 
     private EcordDriverConfig ecordDriverConfig;
     private XoSHttpClient xoSHttpClient;
+    private AtomicCounter latlngCounter;
 
     private final NetworkConfigListener configListener = new InternalConfigListener();
 
@@ -148,9 +154,16 @@
     public void activate() {
         appId = coreService.registerApplication(PROVIDER_NAME);
        // configRegistry.registerConfigFactory(diverConfigFactory);
+        latlngCounter = storageService.atomicCounterBuilder()
+                .withName(LATLNG_COUNTER)
+                .build()
+                .asAtomicCounter();
         configRegistry.registerConfigFactory(xosEndpointConfigFactory);
         configService.addListener(configListener);
         deviceProviderService = deviceProviderRegistry.register(this);
+        if (!configService.getSubjects(XosEndPointConfig.class).isEmpty()) {
+            readXoSEndPointConfig();
+        }
         log.info("Started");
     }
 
@@ -264,6 +277,7 @@
         log.info("Notifying ecord virtual ports...");
         deviceProviderService.updatePorts(deviceId, portDescriptions);
        // addGlobalMefLtp(deviceId, portDescriptions);
+        log.info("Notifying XOS of virtual ports...");
         notifyXoS(deviceId, portDescriptions);
     }
 
@@ -279,12 +293,25 @@
                 ConnectPoint cp = new ConnectPoint(deviceId, port.portNumber());
                 switch (type) {
                     case UNI:
+                        log.debug("Port descriptions {}", portDescriptions);
+                        //If XoSClient null create new one
+                        if (xoSHttpClient == null) {
+                            readXoSEndPointConfig();
+                        }
                         ObjectNode body = xoSHttpClient.mapper().createObjectNode();
                         body.put("tenant", port.annotations().value(DOMAIN_ID));
                         body.put("name", "UNI:" + cp.toString());
-                        body.put("latlng", "[" + port.annotations().value(LATITUDE) + ", " +
-                                port.annotations().value(LONGITUDE) + "]");
+                        String latitude = port.annotations().value(LATITUDE);
+                        String longitude = port.annotations().value(LONGITUDE);
+                        if (latitude == null || longitude == null) {
+                            //Something went wrong in the local, lets reset them both
+                            longitude = String.valueOf(latlngCounter.get());
+                            latitude = String.valueOf(latlngCounter.getAndAdd(10L));
+                        }
+                        body.put("latlng", "[" + latitude + ", " +
+                                longitude + "]");
                         body.put("cpe_id", cp.toString());
+                        log.debug("Node {}", body.toString());
                         xoSHttpClient.restPost(body.toString());
                         break;
                     case ENNI:
@@ -348,8 +375,12 @@
     }
 
     private void readXoSEndPointConfig() {
-        xoSHttpClient =
-                new XoSHttpClient(configRegistry.getConfig(appId, XosEndPointConfig.class).xos());
+        XosEndPointConfig xosEndPointConfig = configRegistry.getConfig(appId, XosEndPointConfig.class);
+        if (xosEndPointConfig != null) {
+            xoSHttpClient = new XoSHttpClient(xosEndPointConfig.xos());
+        } else {
+            log.warn("Null configuration for XOS, can't push UNIs");
+        }
     }
 
     private void readDriverFromConfig() {
@@ -360,13 +391,9 @@
     private class InternalConfigListener implements NetworkConfigListener {
         @Override
         public void event(NetworkConfigEvent event) {
-            if (!event.configClass().equals(EcordDriverConfig.class) ||
-                    !event.configClass().equals(XosEndPointConfig.class)) {
-                return;
-            }
             switch (event.type()) {
                 case CONFIG_ADDED:
-                    log.info("Network configuration added");
+                    log.debug("Network configuration added");
                     if (event.configClass().equals(EcordDriverConfig.class)) {
                         eventExecutor.execute(VirtualDomainDeviceProvider.this::readDriverFromConfig);
                     } else {
@@ -375,7 +402,7 @@
 
                     break;
                 case CONFIG_UPDATED:
-                    log.info("Network configuration updated");
+                    log.debug("Network configuration updated");
                     if (event.configClass().equals(EcordDriverConfig.class)) {
                         eventExecutor.execute(VirtualDomainDeviceProvider.this::readDriverFromConfig);
                     } else {
@@ -386,5 +413,11 @@
                     break;
             }
         }
+
+        @Override
+        public boolean isRelevant(NetworkConfigEvent event) {
+            return event.configClass().equals(EcordDriverConfig.class)
+                    || event.configClass().equals(XosEndPointConfig.class);
+        }
     }
 }
diff --git a/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/XoSHttpClient.java b/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/XoSHttpClient.java
index fdd8570..59c544f 100644
--- a/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/XoSHttpClient.java
+++ b/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/XoSHttpClient.java
@@ -44,7 +44,10 @@
     private XosEndPoint xosEndPoint;
     private final Client client = ClientBuilder.newClient();
 
-
+    /**
+     * Creates a Client to contact XOS from this ONOS instance.
+     * @param xosEndPoint class containing info of the endpoint of XOS
+     */
     public XoSHttpClient(XosEndPoint xosEndPoint) {
         this.xosEndPoint = xosEndPoint;
         // Setup credentials
@@ -59,17 +62,21 @@
         mapper().enable(SerializationFeature.INDENT_OUTPUT);
     }
 
+    /**
+     * Enables to make a post request to XOS.
+     * @param body the body to post to XOS
+     */
     public void restPost(String body) {
-        WebTarget webTarget = client.target("http://" + xosEndPoint.ipAddress().toString() +
-                 xosEndPoint.resource() + "/");
+        log.info("URL {}", "http://" + xosEndPoint.endpoint());
+        WebTarget webTarget = client.target("http://" + xosEndPoint.endpoint());
         Response response;
         response = webTarget.request(MediaType.APPLICATION_JSON)
                 .post(Entity.entity(body, MediaType.APPLICATION_JSON));
         try {
             if (response.getStatus() != HTTP_OK) {
-                log.warn("Failed to put resource {}", xosEndPoint.resource());
+                log.warn("Failed to post resource {}, response {}", xosEndPoint.endpoint(), response);
             } else {
-                log.info("Post to XoS successful: {}", response.getStatus());
+                log.info("Post UNIs to XoS successful: {}", response.getStatus());
             }
         } catch (javax.ws.rs.ProcessingException e) {
             log.error("Javax process exception in XoSClientHttp: {}", e.getMessage());
diff --git a/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/config/XosEndPointConfig.java b/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/config/XosEndPointConfig.java
index df0c59e..c64aaa9 100644
--- a/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/config/XosEndPointConfig.java
+++ b/global/virtualprovider/src/main/java/org/opencord/ce/global/virtualdomain/config/XosEndPointConfig.java
@@ -16,46 +16,43 @@
 
 package org.opencord.ce.global.virtualdomain.config;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import org.onlab.packet.IpAddress;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.config.Config;
 
 /**
- * Global XoS endpoint.
+ * Global XoS endpoint containing all information from XOS.
  */
 public class XosEndPointConfig extends Config<ApplicationId> {
+
     private static final String USERNAME = "username";
     private static final String PASSWD = "password";
-    private static final String XOS = "xos";
-    private static final String ADDRESS = "address";
-    private static final String RESOURCE = "resource";
+    private static final String ENDPOINT = "endpoint";
 
+    /**
+     * Gets the enpoint from the Configuration.
+     * @return a class containing all the info to connect
+     */
     public XosEndPoint xos() {
-        JsonNode xosNode = object.path(XOS);
-        IpAddress ipAddress = IpAddress.valueOf(xosNode.get(ADDRESS).asText());
-        String username = xosNode.get(USERNAME).asText();
-        String password = xosNode.get(PASSWD).asText();
-        String resource = xosNode.get(RESOURCE).asText();
-        return new XosEndPoint(ipAddress, username, password, resource);
+        String endpoint = node.get(ENDPOINT).asText();
+        String username = node.get(USERNAME).asText();
+        String password = node.get(PASSWD).asText();
+        return new XosEndPoint(endpoint, username, password);
     }
 
     public static class XosEndPoint {
-        private IpAddress ipAddress;
+        private String endpoint;
         private String username;
         private String password;
-        private String resource;
 
-        public XosEndPoint(IpAddress ipAddress, String username,
-                           String password, String resource) {
-            this.ipAddress = ipAddress;
+        public XosEndPoint(String endpoint, String username,
+                           String password) {
+            this.endpoint = endpoint;
             this.username = username;
             this.password = password;
-            this.resource = resource;
         }
 
-        public IpAddress ipAddress() {
-            return ipAddress;
+        public String endpoint() {
+            return endpoint;
         }
 
         public String username() {
@@ -66,9 +63,6 @@
             return password;
         }
 
-        public String resource() {
-            return resource;
-        }
     }
 
 }