[CORD-2887] Configure subscriber termination in the fabric

Change-Id: I44d6f561840cfaf301378914d6bd980fa5fb8d49
diff --git a/xos/synchronizer/model_policies/model_policy_vsghwserviceinstance.py b/xos/synchronizer/model_policies/model_policy_vsghwserviceinstance.py
index aece04e..18475c9 100644
--- a/xos/synchronizer/model_policies/model_policy_vsghwserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_vsghwserviceinstance.py
@@ -31,21 +31,12 @@
 
     def handle_update(self, service_instance):
         log.info("Handle_update VSG-HW Service Instance", service_instance=service_instance)
-        if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
-            # if the last provided_link has just gone away, then self-destruct
-            self.logger.info("The last provided link has been deleted -- self-destructing.")
-            # TODO: We shouldn't have to call handle_delete ourselves. The model policy framework should handle this
-            #       for us, but it isn't. I think that's happening is that serviceinstance.delete() isn't setting a new
-            #       updated timestamp, since there's no way to pass `always_update_timestamp`, and therefore the
-            #       policy framework doesn't know that the object has changed and needs new policies. For now, the
-            #       workaround is to just call handle_delete ourselves.
+
+        if (service_instance.link_deleted_count > 0) and (not service_instance.provided_links.exists()):
+            # If this instance has no links pointing to it, delete
             self.handle_delete(service_instance)
-            # Note that if we deleted the Instance in handle_delete, then django may have cascade-deleted the service
-            # instance by now. Thus we have to guard our delete, to check that the service instance still exists.
             if VSGHWServiceInstance.objects.filter(id=service_instance.id).exists():
                 service_instance.delete()
-            else:
-                self.logger.info("Tenant %s is already deleted" % service_instance)
             return
 
     def handle_delete(self, service_instance):
diff --git a/xos/synchronizer/steps/sync_vsg_hw_service_instance.py b/xos/synchronizer/steps/sync_vsg_hw_service_instance.py
index 28dfddf..c437610 100644
--- a/xos/synchronizer/steps/sync_vsg_hw_service_instance.py
+++ b/xos/synchronizer/steps/sync_vsg_hw_service_instance.py
@@ -12,31 +12,91 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
-import os
-import sys
 from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
-from synchronizers.new_base.modelaccessor import VSGHWServiceInstance
+from synchronizers.new_base.modelaccessor import model_accessor, VSGHWServiceInstance, ServiceInstance
 
 from xosconfig import Config
 from multistructlog import create_logger
-from time import sleep
 import requests
+from requests.auth import HTTPBasicAuth
 
 log = create_logger(Config().get('logging'))
 
-# parentdir = os.path.join(os.path.dirname(__file__), "..")
-# sys.path.insert(0, parentdir)
-# sys.path.insert(0, os.path.dirname(__file__))
-
 class SyncVSGHWServiceInstance(SyncStep):
     provides = [VSGHWServiceInstance]
 
     observes = VSGHWServiceInstance
 
+    @staticmethod
+    def format_url(url):
+        if 'http' in url:
+            return url
+        else:
+            return 'http://%s' % url
+
+    @staticmethod
+    def get_fabric_onos_info(si):
+
+        # get the vsg-hw service
+        vsg_hw = si.owner
+
+        # get the onos_fabric service
+        fabric_onos = [s.leaf_model for s in vsg_hw.provider_services if "onos" in s.name.lower()]
+
+        if len(fabric_onos) == 0:
+            raise Exception('Cannot find ONOS service in provider_services of vSG-HW')
+
+        fabric_onos = fabric_onos[0]
+
+        return {
+            'url': SyncVSGHWServiceInstance.format_url("%s:%s" % (fabric_onos.rest_hostname, fabric_onos.rest_port)),
+            'user': fabric_onos.rest_username,
+            'pass': fabric_onos.rest_password
+        }
+
     def sync_record(self, o):
         log.info("Sync'ing VSG-HW Service Instance", service_instance=o)
 
+
+        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")
+        c_tag = si.get_westbound_service_instance_properties("c_tag")
+        ip = si.get_westbound_service_instance_properties("ip_address")
+        dpid = si.get_westbound_service_instance_properties("switch_datapath_id")
+        port = si.get_westbound_service_instance_properties("switch_port")
+
+        data = {
+            'hosts': {
+                mac_address + "/" + str(s_tag): {
+                    "basic": {
+                        "ips": [ip],
+                        "locations": ["%s/%s" % (dpid, port)],
+                        "innerVlan": str(c_tag),
+                    }
+                }
+            }
+        }
+
+        # Adding the optional tpid
+        tpid = si.get_westbound_service_instance_properties("outer_tpid")
+        if tpid:
+            data["hosts"][mac_address + "/" + str(s_tag)]["basic"]["outerTpid"] = str(tpid)
+
+        url = onos['url'] + '/onos/v1/network/configuration'
+
+        log.info("Sending requests to ONOS", url=url, body=data)
+
+        r = requests.post(url, json=data, auth=HTTPBasicAuth(onos['user'], onos['pass']))
+
+        if r.status_code != 200:
+            raise Exception("Failed to terminate subscriber in ONOS: %s" % r.text)
+
+        log.info("ONOS response", res=r.text)
+
     def delete_record(self, o):
         log.info("Deleting VSG-HW Service Instance", service_instance=o)
         pass
\ No newline at end of file
diff --git a/xos/synchronizer/steps/test_sync_vsg_hw_service_instance.py b/xos/synchronizer/steps/test_sync_vsg_hw_service_instance.py
new file mode 100644
index 0000000..548e8ab
--- /dev/null
+++ b/xos/synchronizer/steps/test_sync_vsg_hw_service_instance.py
@@ -0,0 +1,176 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import unittest
+
+import functools
+from mock import patch, call, Mock, PropertyMock
+import requests_mock
+
+import os, sys
+
+# Hack to load synchronizer framework
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir = os.path.join(xos_dir, "../../xos_services")
+sys.path.append(xos_dir)
+sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+# END Hack to load synchronizer framework
+
+# generate model from xproto
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+# END generate model from xproto
+
+def mock_get_westbound_service_instance_properties(prop):
+    return prop
+
+def match_json(desired, req):
+    if desired!=req.json():
+        raise Exception("Got request %s, but body is not matching" % req.url)
+        return False
+    return True
+
+class TestSyncOLTDevice(unittest.TestCase):
+
+    def setUp(self):
+        global DeferredException
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+        # Setting up the config module
+        from xosconfig import Config
+        config = os.path.join(test_path, "../test_vsg_hw_config.yaml")
+        Config.clear()
+        Config.init(config, "synchronizer-config-schema.yaml")
+        # END Setting up the config module
+
+        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vsg-hw", "vsg-hw.xproto")])
+        import synchronizers.new_base.modelaccessor
+
+        from sync_vsg_hw_service_instance import SyncVSGHWServiceInstance, model_accessor
+
+        # import all class names to globals
+        for (k, v) in model_accessor.all_model_classes.items():
+            globals()[k] = v
+
+
+        self.sync_step = SyncVSGHWServiceInstance
+
+
+        # mock onos-fabric
+        onos_fabric = Mock()
+        onos_fabric.name = "onos-fabric"
+        onos_fabric.rest_hostname = "onos-fabric"
+        onos_fabric.rest_port = "8181"
+        onos_fabric.rest_username = "onos"
+        onos_fabric.rest_password = "rocks"
+
+        # mock generic service
+        svc = Mock()
+        svc.name = "onos-fabric"
+        svc.leaf_model = onos_fabric
+
+        # mock vsg-hw service
+        self.vsg_service = Mock()
+        self.vsg_service.provider_services = [svc]
+
+        # create a mock vsg-hw service instance
+        o = Mock()
+        o.id = 1
+        o.owner = self.vsg_service
+        o.tologdict.return_value = {}
+
+        si = Mock()
+        si.get_westbound_service_instance_properties = mock_get_westbound_service_instance_properties
+
+        self.o = o
+        self.si = si
+
+
+
+    def tearDown(self):
+        self.o = None
+        sys.path = self.sys_path_save
+
+    @requests_mock.Mocker()
+    def test_sync_success(self, m):
+        expected_conf = {
+            'hosts': {
+                "mac_address/s_tag": {
+                    "basic": {
+                        "ips": ["ip_address"],
+                        "locations": ["switch_datapath_id/switch_port"],
+                        "innerVlan": "c_tag",
+                    }
+                }
+            }
+        }
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration",
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_conf))
+
+        # override the get_westbound_service_instance_properties mock to remove the outer_tpid field
+        def wb_si_prop(prop):
+            if prop == "outer_tpid":
+                return None
+            return prop
+
+        self.si.get_westbound_service_instance_properties = wb_si_prop
+
+        with patch.object(ServiceInstance.objects, "get") as service_instance_mock:
+            service_instance_mock.return_value = self.si
+
+            self.sync_step().sync_record(self.o)
+
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_sync_success_with_tpid(self, m):
+        expected_conf = {
+            'hosts': {
+                "mac_address/s_tag": {
+                    "basic": {
+                        "ips": ["ip_address"],
+                        "locations": ["switch_datapath_id/switch_port"],
+                        "innerVlan": "c_tag",
+                        "outerTpid": "outer_tpid"
+                    }
+                }
+            }
+        }
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration",
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_conf))
+
+        with patch.object(ServiceInstance.objects, "get") as service_instance_mock:
+            service_instance_mock.return_value = self.si
+
+            self.sync_step().sync_record(self.o)
+
+            self.assertTrue(m.called)
\ No newline at end of file
diff --git a/xos/synchronizer/test_vsg_hw_config.yaml b/xos/synchronizer/test_vsg_hw_config.yaml
new file mode 100644
index 0000000..1645391
--- /dev/null
+++ b/xos/synchronizer/test_vsg_hw_config.yaml
@@ -0,0 +1,29 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+name: test-vsg-hw
+accessor:
+  username: xosadmin@opencord.org
+  password: "sample"
+  kind: "testframework"
+logging:
+  version: 1
+  handlers:
+    console:
+      class: logging.StreamHandler
+  loggers:
+    'multistructlog':
+      handlers:
+          - console