[CORD-3168] Removing subscriber termination when the subscriber status change

Change-Id: I3b4494539d12f577e7d093cce01317e759e6a40c
diff --git a/xos/synchronizer/steps/sync_vsg_hw_service_instance.py b/xos/synchronizer/steps/sync_vsg_hw_service_instance.py
index 05b16cb..6f79ca2 100644
--- a/xos/synchronizer/steps/sync_vsg_hw_service_instance.py
+++ b/xos/synchronizer/steps/sync_vsg_hw_service_instance.py
@@ -17,6 +17,7 @@
 
 from xosconfig import Config
 from multistructlog import create_logger
+import urllib
 import requests
 from requests.auth import HTTPBasicAuth
 
@@ -83,7 +84,15 @@
             if not port:
                 raise ValueError("switch_port")
         except ValueError as e:
-            raise DeferredException("Skipping synchronization for VSG-HW Service Instance with id %s as %s is not available" % (o.id, e.message))
+            raise Exception("Skipping synchronization for VSG-HW Service Instance with id %s as westbound value %s is not available" % (o.id, e.message))
+
+        subscriber_status = si.get_westbound_service_instance_properties("status")
+
+        if subscriber_status != "enabled":
+            if o.enacted:
+                return self.delete_record(o)
+            else:
+                raise DeferredException("Deferring synchronization for VSG-HW Service Instance with id %s as subscriber is not enabled" % o.id)
 
         data = {
             'hosts': {
@@ -115,4 +124,23 @@
 
     def delete_record(self, o):
         self.log.info("Deleting VSG-HW Service Instance", service_instance=o)
+        if o.enacted:
+            onos = SyncVSGHWServiceInstance.get_fabric_onos_info(o)
+
+            si = ServiceInstance.objects.get(id=o.id)
+
+            mac_address = si.get_westbound_service_instance_properties("mac_address")
+            s_tag = si.get_westbound_service_instance_properties("s_tag")
+
+            key = "%s/%s" % (mac_address, str(s_tag))
+            key = urllib.quote(key, safe='')
+
+            url = onos['url'] + '/onos/v1/network/configuration/hosts/%s' % key
+
+            r = requests.delete(url, auth=HTTPBasicAuth(onos['user'], onos['pass']))
+
+            if r.status_code != 200:
+                raise Exception("Failed to remove subscriber termination in ONOS: %s" % r.text)
+
+            self.log.info("ONOS response", res=r.text)
         pass
\ No newline at end of file