[CORD-2550] Enodeb update and reading IMSI <-> Profile relation from ONOS

Change-Id: If8cc7c7d7a848c6fcedba672e66ffe0c6f8761b7
diff --git a/xos/synchronizer/progran_config.yml b/xos/synchronizer/progran_config.yml
index 3e5fd2c..e894bb2 100644
--- a/xos/synchronizer/progran_config.yml
+++ b/xos/synchronizer/progran_config.yml
@@ -47,4 +47,4 @@
       handlers:
           - console
           - file
-      level: INFO
\ No newline at end of file
+      level: DEBUG
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_enodeb.py b/xos/synchronizer/steps/sync_enodeb.py
index 7579763..b0e4172 100644
--- a/xos/synchronizer/steps/sync_enodeb.py
+++ b/xos/synchronizer/steps/sync_enodeb.py
@@ -58,10 +58,13 @@
         log.debug("Sync'ing enodeb with data", request_data=data)
 
         if o.previously_sync == False:
-            log.debug("Sending POST")
+            log.debug("Sending POST", url=enodeb_url, data=json.dumps(data))
             r = requests.post(enodeb_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
         else:
-            log.debug("Sending PUT")
+            data = {
+                "EnodeB": data
+            }
+            log.debug("Sending PUT", url=enodeb_url, data=json.dumps(data))
             r = requests.put(enodeb_url, data=json.dumps(data),
                               auth=HTTPBasicAuth(onos['username'], onos['password']))
 
diff --git a/xos/synchronizer/steps/sync_imsi.py b/xos/synchronizer/steps/sync_imsi.py
index 3ad0250..94fdb6f 100644
--- a/xos/synchronizer/steps/sync_imsi.py
+++ b/xos/synchronizer/steps/sync_imsi.py
@@ -57,7 +57,7 @@
 
     def delete_record(self, o):
         log.info("deleting imsi", object=str(o), **o.tologdict())
-        onos = ProgranHelpers.get_onos_info_from_si(o)
+        onos = ProgranHelpers.get_progran_onos_info(o)
         profile_url = "http://%s:%s/onos/progran/imsi/%s" % (onos['url'], onos['port'], o.imsi_number)
         r = requests.delete(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password']))
         o.active_enodeb_id = 0  # removing the value because it has been deleted
diff --git a/xos/synchronizer/steps/sync_progranserviceinstance_back.py b/xos/synchronizer/steps/sync_progranserviceinstance_back.py
index 8b38d74..87b758c 100644
--- a/xos/synchronizer/steps/sync_progranserviceinstance_back.py
+++ b/xos/synchronizer/steps/sync_progranserviceinstance_back.py
@@ -21,7 +21,7 @@
 import time
 
 from synchronizers.new_base.syncstep import SyncStep
-from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover
+from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover, ServiceInstanceLink, MCordSubscriberInstance
 
 from xosconfig import Config
 from multistructlog import create_logger
@@ -89,12 +89,6 @@
                 si = ProgranServiceInstance.objects.get(name=p['Name'])
                 log.debug("Profile %s already exists, updating it" % p['Name'])
 
-                # if the model has not been synchronizer yet, skip it
-                if si.no_sync is True:
-                    log.info("Skipping profile %s as not synchronized" % p['Name'])
-                    # NOTE add it to the removed profiles to avoid deletion (this is ugly, I know)
-                    updated_profiles.append(si.name)
-                    continue
             except IndexError:
                 si = ProgranServiceInstance()
 
@@ -102,6 +96,44 @@
 
                 log.debug("Profile %s is new, creating it" % p['Name'])
 
+            if not si.is_new:
+                # update IMSI association
+                xos_imsis_for_profile = [i.subscriber_service_instance.leaf_model for i in si.provided_links.all()]
+                progran_imsis_for_profile = p['IMSIRuleArray']
+
+                log.debug("List of imsis for profile %s in XOS" % p["Name"], imsis=xos_imsis_for_profile)
+                log.debug("List of imsis for profile %s in ONOS" % p["Name"], imsis=progran_imsis_for_profile)
+
+                for i in xos_imsis_for_profile:
+                    if not i.imsi_number in progran_imsis_for_profile:
+                        log.debug("Removing Imsi %s from profile %s" % (i.imsi_number, p['Name']))
+
+                        imsi_link = ServiceInstanceLink.objects.get(subscriber_service_instance_id=i.id)
+
+                        # NOTE: this model has already been removed from the backend, no need to synchronize
+                        imsi_link.backend_need_delete = False
+                        imsi_link.no_sync = True
+                        imsi_link.save() # we need to save it to avoid a synchronization loop
+
+                        imsi_link.delete()
+                    else:
+                        # remove from imsi list coming from progran everything we already know about
+                        progran_imsis_for_profile.remove(i.imsi_number)
+
+                for i in progran_imsis_for_profile:
+                    log.debug("Adding Imsi %s to profile %s" % (i, p['Name']))
+                    imsi = MCordSubscriberInstance.objects.get(imsi_number=i)
+                    imsi_to_profile = ServiceInstanceLink(provider_service_instance=si,
+                                                          subscriber_service_instance=imsi)
+                    imsi_to_profile.save()
+
+            # if the model has not been synchronized yet, skip it
+            if not si.is_new and si.no_sync is False:
+                log.info("Skipping profile %s as not synchronized" % p['Name'])
+                # NOTE add it to the removed profiles to avoid deletion (this is ugly, I know)
+                updated_profiles.append(si.name)
+                continue
+
             si = ProgranHelpers.update_fields(si, p, field_mapping, field_transformations)
 
             # checking for handovers