[CORD-2642][CORD-2643] Add/remove IMSI from/to profile

Change-Id: Ic3725c49015d20e6a14dbc1bbcde09c1744c9f46
diff --git a/samples/README.md b/samples/README.md
index 0d3c2cf..373c0a7 100644
--- a/samples/README.md
+++ b/samples/README.md
@@ -28,4 +28,13 @@
 bash progran-curl.sh progran/progranserviceinstances profile.json
 ```
 
-curl -U xosadmin@opencord.org:bash -X PUT --data-binary @profile.json http://127.0.0.1:9101/xosapi/v1/progran/progranserviceinstances/4
\ No newline at end of file
+Attach an IMSI to a profile: note you'll need to set the correct ids
+```bash
+bash progran-curl.sh core/serviceinstancelinks imsi_to_profile.json
+```
+
+Update a profile: make changes to profile.json first
+
+```bash
+curl -U xosadmin@opencord.org:bash -X PUT --data-binary @profile.json http://127.0.0.1:9101/xosapi/v1/progran/progranserviceinstances/<profile_id>
+```
diff --git a/samples/imsi_to_profile.json b/samples/imsi_to_profile.json
new file mode 100644
index 0000000..a14f040
--- /dev/null
+++ b/samples/imsi_to_profile.json
@@ -0,0 +1,4 @@
+{
+  "provider_service_instance_id": 4,
+  "subscriber_service_instance_id": 3
+}
\ No newline at end of file
diff --git a/samples/profile.json b/samples/profile.json
index 3ebb1c4..ffc3e22 100644
--- a/samples/profile.json
+++ b/samples/profile.json
@@ -11,7 +11,5 @@
   "SubsProfile":"",
   "DlWifiRate":13,
   "DlUeAllocRbRate":12,
-  "handover_id": 1,
-  "id": 4,
-  "enodeb_id": 0
+  "handover_id": 1
 }
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_imsi_link.py b/xos/synchronizer/steps/sync_imsi_link.py
index 059b789..3b1ba98 100644
--- a/xos/synchronizer/steps/sync_imsi_link.py
+++ b/xos/synchronizer/steps/sync_imsi_link.py
@@ -16,13 +16,15 @@
 
 import os
 import sys
-from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
 from synchronizers.new_base.ansible_helper import run_template
-from synchronizers.new_base.modelaccessor import MCordSubscriberInstance, ServiceInstanceLink
+from synchronizers.new_base.modelaccessor import MCordSubscriberInstance, ServiceInstanceLink, ProgranServiceInstance
 
 from xosconfig import Config
 from multistructlog import create_logger
 import json
+import requests
+from requests.auth import HTTPBasicAuth
 
 
 log = create_logger(Config().get('logging'))
@@ -32,66 +34,57 @@
 sys.path.insert(0, os.path.dirname(__file__))
 from helpers import ProgranHelpers
 
-class SyncProgranIMSILink(SyncInstanceUsingAnsible):
+class SyncProgranIMSILink(SyncStep):
     provides = [ServiceInstanceLink]
 
     observes = ServiceInstanceLink
 
-    def skip_ansible_fields(self, o):
-        # FIXME This model does not have an instance, this is a workaroung to make it work,
-        # but it need to be cleaned up creating a general SyncUsingAnsible base class
-        return True
+    # NOTE Override the default fetch_pending method to receive on links between MCordSubscriberInstances and ProgranServiceInstances
+    def fetch_pending(self, deleted):
 
-    def get_progran_imsi_field(self, o):
+        objs = super(SyncProgranIMSILink, self).fetch_pending(deleted)
+        objs = list(objs)
 
-        imsi = {
-            "IMSIRuleArray": [
-                '123' # TODO retrieve it service_instance_link.subscriber_service_instance.imsi_number
-            ]
-        }
-        imsi = json.dumps(imsi)
-        return imsi
+        to_be_sync = []
 
-    def get_extra_attributes(self, o):
+        for link in objs:
+            if link.provider_service_instance.leaf_model_name == "ProgranServiceInstance" and link.subscriber_service_instance.leaf_model_name == "MCordSubscriberInstance":
+                to_be_sync.append(link)
 
-
-        return fields
+        return to_be_sync
 
     def sync_record(self, o):
-        log.info("sync'ing profile", object=str(o), **o.tologdict())
-        # onos = ProgranHelpers.get_progran_onos_info()
-        # profile_name = 'foo' # TODO retrieve it service_instance_link.subscriber_service_instance.imsi_number
-        #
-        # fields = {
-        #     'onos_url': onos['url'],
-        #     'onos_username': onos['username'],
-        #     'onos_password': onos['password'],
-        #     'onos_port': onos['port'],
-        #     'endpoint': 'profile/%s/imsi' % profile_name,
-        #     'body': self.get_progran_imsi_field(o),
-        #     'method': 'POST'
-        # }
-        # fields = {}
-        # self.run_playbook(o, fields)
-        # o.save()
-        print o
 
-    # FIXME we need to override this as the default expect to ssh into a VM
-    def run_playbook(self, o, fields):
-        run_template("progran_curl.yaml", fields, object=o)
+        if o.provider_service_instance.leaf_model_name == "ProgranServiceInstance" and o.subscriber_service_instance.leaf_model_name ==  "MCordSubscriberInstance":
+            log.info("sync'ing link", object=str(o), **o.tologdict())
+
+            onos = ProgranHelpers.get_progran_onos_info()
+
+            profile_name = o.provider_service_instance.name
+            imsi_number =  o.subscriber_service_instance.leaf_model.imsi_number
+
+            data = {
+                "IMSIRuleArray": [
+                    imsi_number
+                ]
+            }
+
+            url = "http://%s:%s/onos/progran/profile/%s/imsi" % (onos['url'], onos['port'], profile_name)
+
+            r = requests.post(url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
+            print r.json()
 
     def delete_record(self, o):
-        log.info("deleting object", object=str(o), **o.tologdict())
-        # onos = ProgranHelpers.get_progran_onos_info()
-        # profile_name = 'foo'
-        # imsi_number = 'bar'
-        # fields = {
-        #     'onos_url': onos['url'],
-        #     'onos_username': onos['username'],
-        #     'onos_password': onos['password'],
-        #     'onos_port': onos['port'],
-        #     'endpoint': 'profile/{profile_name}/{imsi}' % (profile_name, imsi_number),
-        #     'body': '',
-        #     'method': 'DELETE'
-        # }
-        # self.run_playbook(o, fields)
\ No newline at end of file
+
+        if o.provider_service_instance.leaf_model_name == "ProgranServiceInstance" and o.subscriber_service_instance.leaf_model_name ==  "MCordSubscriberInstance":
+            log.info("deleting link", object=str(o), **o.tologdict())
+
+            onos = ProgranHelpers.get_progran_onos_info()
+
+            profile_name = o.provider_service_instance.name
+            imsi_number =  o.subscriber_service_instance.leaf_model.imsi_number
+
+            url = "http://%s:%s/onos/progran/profile/%s/%s" % (onos['url'], onos['port'], profile_name, imsi_number)
+
+            r = requests.delete(url, auth=HTTPBasicAuth(onos['username'], onos['password']))
+            print r.json()
\ No newline at end of file