CORD-539 Implemented purge and sync states methods

Change-Id: I96008ec5023f08c526291141883a2bf145261ac0
diff --git a/src/main/java/org/opencord/cordvtn/api/core/CordVtnAdminService.java b/src/main/java/org/opencord/cordvtn/api/core/CordVtnAdminService.java
index fbe743b..2411628 100644
--- a/src/main/java/org/opencord/cordvtn/api/core/CordVtnAdminService.java
+++ b/src/main/java/org/opencord/cordvtn/api/core/CordVtnAdminService.java
@@ -34,6 +34,16 @@
         ServiceNetworkService {
 
     /**
+     * Purges internal network states.
+     */
+    void purgeStates();
+
+    /**
+     * Synchronizes internal network states with external services.
+     */
+    void syncStates();
+
+    /**
      * Creates a service port with the given information.
      *
      * @param servicePort the new service port
diff --git a/src/main/java/org/opencord/cordvtn/api/core/CordVtnStore.java b/src/main/java/org/opencord/cordvtn/api/core/CordVtnStore.java
index 200358a..78a60a4 100644
--- a/src/main/java/org/opencord/cordvtn/api/core/CordVtnStore.java
+++ b/src/main/java/org/opencord/cordvtn/api/core/CordVtnStore.java
@@ -34,6 +34,11 @@
 public interface CordVtnStore extends Store<VtnNetworkEvent, CordVtnStoreDelegate> {
 
     /**
+     * Purges vtn store.
+     */
+    void clear();
+
+    /**
      * Creates vtn network.
      *
      * @param vtnNet vtn network
diff --git a/src/main/java/org/opencord/cordvtn/impl/CordVtnManager.java b/src/main/java/org/opencord/cordvtn/impl/CordVtnManager.java
index 43cf3d8..a74635f 100644
--- a/src/main/java/org/opencord/cordvtn/impl/CordVtnManager.java
+++ b/src/main/java/org/opencord/cordvtn/impl/CordVtnManager.java
@@ -22,10 +22,17 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
 import org.onosproject.event.ListenerRegistry;
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
+import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.host.HostService;
+import org.opencord.cordvtn.api.Constants;
+import org.opencord.cordvtn.api.config.CordVtnConfig;
+import org.opencord.cordvtn.api.config.OpenStackConfig;
+import org.opencord.cordvtn.api.config.XosConfig;
 import org.opencord.cordvtn.api.core.CordVtnAdminService;
 import org.opencord.cordvtn.api.core.CordVtnService;
 import org.opencord.cordvtn.api.core.CordVtnStore;
@@ -42,6 +49,8 @@
 import org.opencord.cordvtn.api.net.VtnNetworkEvent;
 import org.opencord.cordvtn.api.net.VtnNetworkListener;
 import org.opencord.cordvtn.api.net.VtnPort;
+import org.opencord.cordvtn.impl.external.OpenStackNetworking;
+import org.opencord.cordvtn.impl.external.XosServiceNetworking;
 import org.openstack4j.model.network.Network;
 import org.openstack4j.model.network.Port;
 import org.openstack4j.model.network.Subnet;
@@ -96,15 +105,23 @@
     private static final String PROVIDER = "provider ";
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigService configService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected HostService hostService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected CordVtnStore store;
 
-    private CordVtnStoreDelegate delegate = new InternalCordVtnStoreDelegate();
+    private final CordVtnStoreDelegate delegate = new InternalCordVtnStoreDelegate();
+    private ApplicationId appId;
 
     @Activate
     protected void activate() {
+        appId = coreService.registerApplication(Constants.CORDVTN_APP_ID);
         store.setDelegate(delegate);
         log.info("Started");
     }
@@ -116,6 +133,17 @@
     }
 
     @Override
+    public void purgeStates() {
+        store.clear();
+    }
+
+    @Override
+    public void syncStates() {
+        syncNetwork();
+        syncServiceNetwork();
+    }
+
+    @Override
     public void createServiceNetwork(ServiceNetwork serviceNet) {
         checkNotNull(serviceNet, ERR_NULL_SERVICE_NET);
         synchronized (this) {
@@ -407,6 +435,44 @@
         return store.subnets();
     }
 
+    private void syncNetwork() {
+        CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class);
+        if (config == null) {
+            final String error = "Failed to read network configurations";
+            throw new IllegalArgumentException(error);
+        }
+
+        OpenStackConfig osConfig = config.openStackConfig();
+        NetworkService netService = OpenStackNetworking.builder()
+                .endpoint(osConfig.endpoint())
+                .tenant(osConfig.tenant())
+                .user(osConfig.user())
+                .password(osConfig.password())
+                .build();
+
+        netService.networks().forEach(this::createNetwork);
+        netService.subnets().forEach(this::createSubnet);
+        netService.ports().forEach(this::createPort);
+    }
+
+    private void syncServiceNetwork() {
+        CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class);
+        if (config == null) {
+            final String error = "Failed to read network configurations";
+            throw new IllegalArgumentException(error);
+        }
+
+        XosConfig xosConfig = config.xosConfig();
+        ServiceNetworkService snetService = XosServiceNetworking.builder()
+                .endpoint(xosConfig.endpoint())
+                .user(xosConfig.user())
+                .password(xosConfig.password())
+                .build();
+
+        snetService.serviceNetworks().forEach(this::createServiceNetwork);
+        snetService.servicePorts().forEach(this::createServicePort);
+    }
+
     private Instance getInstance(PortId portId) {
         VtnPort vtnPort = vtnPort(portId);
         if (vtnPort == null) {
diff --git a/src/main/java/org/opencord/cordvtn/impl/DistributedCordVtnStore.java b/src/main/java/org/opencord/cordvtn/impl/DistributedCordVtnStore.java
index 6846b6f..0d69369 100644
--- a/src/main/java/org/opencord/cordvtn/impl/DistributedCordVtnStore.java
+++ b/src/main/java/org/opencord/cordvtn/impl/DistributedCordVtnStore.java
@@ -187,6 +187,17 @@
     }
 
     @Override
+    public void clear() {
+        synchronized (this) {
+            vtnNetworkStore.clear();
+            vtnPortStore.clear();
+            networkStore.clear();
+            portStore.clear();
+            subnetStore.clear();
+        }
+    }
+
+    @Override
     public void createVtnNetwork(VtnNetwork vtnNet) {
         vtnNetworkStore.compute(vtnNet.id(), (id, existing) -> {
             final String error = ERR_SYNC + vtnNet.id().id() + ERR_DUPLICATE;