[CORD-2639] Moving Progran to 5.0

Change-Id: Ia838ade6b7cd8e7b4c04ae939e27d5b2dbf16896
diff --git a/xos/synchronizer/steps/progran_curl.yaml b/xos/synchronizer/steps/progran_curl.yaml
new file mode 100644
index 0000000..f567a3b
--- /dev/null
+++ b/xos/synchronizer/steps/progran_curl.yaml
@@ -0,0 +1,37 @@
+# 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.
+
+---
+- hosts: 127.0.0.1
+  connection: local
+  vars:
+    - profile: '{{ profile }}'
+    - endpoint: '{{ endpoint }}'
+
+  tasks:
+    - debug:
+        msg: "{{ '{{' }} profile {{ '}}' }}"
+    # NOTE that the task has a generic name as it's planned to be used for all the requests
+    - name: Call onos Progran
+      uri:
+        url: "http://{{ onos_url }}:{{ onos_port }}/onos/progran/{{ '{{' }} endpoint {{ '}}' }}"
+        method: "{{ method }}"
+        user: "{{ onos_username }}"
+        password: "{{ onos_password }}"
+        force_basic_auth: yes
+        status_code: 200
+        {% if method == "POST" or method == "PUT" -%}
+        body: "{{ '{{' }} profile {{ '}}' }}"
+        body_format: json
+        {%- endif -%}
diff --git a/xos/synchronizer/steps/sync_imsi.py b/xos/synchronizer/steps/sync_imsi.py
deleted file mode 100644
index 0149702..0000000
--- a/xos/synchronizer/steps/sync_imsi.py
+++ /dev/null
@@ -1,76 +0,0 @@
-
-# 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 os
-import sys
-import requests
-import json
-from django.db.models import Q, F
-from services.progran.models import *
-from synchronizers.base.syncstep import SyncStep
-from xos.logger import Logger, logging
-
-# from core.models import Service
-from requests.auth import HTTPBasicAuth
-
-parentdir = os.path.join(os.path.dirname(__file__), "..")
-sys.path.insert(0, parentdir)
-
-logger = Logger(level=logging.INFO)
-
-
-class SyncVImsiApp(SyncStep):
-    provides = [VProgranImsi]
-
-    observes = VProgranImsi
-
-    requested_interval = 0
-
-    def __init__(self, *args, **kwargs):
-        super(SyncVImsiApp, self).__init__(*args, **kwargs)
-
-    def get_onos_progran_addr(self):
-
-        return "http://%s:%s/onos/" % ("10.6.0.1", "8183")
-
-    def get_onos_progran_auth(self):
-
-        return HTTPBasicAuth("onos", "rocks")
-
-    def sync_record(self, app):
-
-        logger.info("Sync'ing Edited vProgran Imsi")
-
-        onos_addr = self.get_onos_progran_addr()
-
-        data = {}
-        data["imsi"] = app.imsi
-        data["profile"] = app.profile
-
-
-        url = onos_addr + "progran/mwc/connect"
-
-        print "POST %s for app %s" % (url, "Progran Imsi")
-
-        auth = self.get_onos_progran_auth()
-        r = requests.post(url, data=json.dumps(data), auth=auth)
-        if (r.status_code != 200):
-            print r
-            raise Exception("Received error from progran app update (%d)" % r.status_code)
-
-    def delete_record(self, app):
-        logger.info("Deletion is not supported yet")
-
diff --git a/xos/synchronizer/steps/sync_profile.py b/xos/synchronizer/steps/sync_profile.py
deleted file mode 100644
index 68c144d..0000000
--- a/xos/synchronizer/steps/sync_profile.py
+++ /dev/null
@@ -1,77 +0,0 @@
-
-# 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 os
-import sys
-import requests
-import json
-from django.db.models import Q, F
-from services.progran.models import *
-from synchronizers.base.syncstep import SyncStep
-from xos.logger import Logger, logging
-
-# from core.models import Service
-from requests.auth import HTTPBasicAuth
-
-parentdir = os.path.join(os.path.dirname(__file__), "..")
-sys.path.insert(0, parentdir)
-
-logger = Logger(level=logging.INFO)
-
-
-class SyncVProfileApp(SyncStep):
-    provides = [VProgranProfile]
-
-    observes = VProgranProfile
-
-    requested_interval = 0
-
-    def __init__(self, *args, **kwargs):
-        super(SyncVProfileApp, self).__init__(*args, **kwargs)
-
-    def get_onos_progran_addr(self):
-
-        return "http://%s:%s/onos/" % ("10.6.0.1", "8183")
-
-    def get_onos_progran_auth(self):
-
-        return HTTPBasicAuth("onos", "rocks")
-
-    def sync_record(self, app):
-
-        logger.info("Sync'ing Edited vProgran Profile ")
-
-        onos_addr = self.get_onos_progran_addr()
-
-        data = {}
-        data["profile"] = app.profile
-        data["dlrate"] = app.dlrate
-        data["ulrate"] = app.ulrate
-
-
-        url = onos_addr + "progran/mwc/profile"
-
-        print "POST %s for app %s" % (url, "Progran Imsi")
-
-        auth = self.get_onos_progran_auth()
-        r = requests.post(url, data=json.dumps(data), auth=auth)
-        if (r.status_code != 200):
-            print r
-            raise Exception("Received error from progran app update (%d)" % r.status_code)
-
-    def delete_record(self, app):
-        logger.info("Deletion is not supported yet")
-
diff --git a/xos/synchronizer/steps/sync_progranserviceinstance.py b/xos/synchronizer/steps/sync_progranserviceinstance.py
new file mode 100644
index 0000000..6a0d84c
--- /dev/null
+++ b/xos/synchronizer/steps/sync_progranserviceinstance.py
@@ -0,0 +1,114 @@
+
+# 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 os
+import sys
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from synchronizers.new_base.ansible_helper import run_template
+from synchronizers.new_base.modelaccessor import ProgranService, ProgranServiceInstance
+
+from xosconfig import Config
+from multistructlog import create_logger
+import json
+
+log = create_logger(Config().get('logging'))
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+class SyncProgranServiceInstance(SyncInstanceUsingAnsible):
+    provides = [ProgranServiceInstance]
+
+    observes = ProgranServiceInstance
+
+    def get_onos_info(self, si):
+
+        progran_service = si.owner.leaf_model
+
+        return {
+            'url': progran_service.onos_address,
+            'port': progran_service.onos_port,
+            'username': progran_service.onos_username,
+            'password': progran_service.onos_password,
+        }
+
+    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
+
+    def get_handover_for_profile(self, o):
+        return {
+            "A3Hysteresis": o.handover.HysteresisA3,
+            "A3TriggerQuantity": o.handover.A3TriggerQuantity,
+            "A3offset": o.handover.A3offset,
+            "A5Hysteresis": o.handover.HysteresisA5,
+            "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp,
+            "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq,
+            "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp,
+            "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq,
+            "A5TriggerQuantity": o.handover.A5TriggerQuantity,
+        }
+
+    def get_progran_profile_field(self, o):
+
+        # basic information that we have in the service instance itself
+        profile = {
+            'AdmControl': o.AdmControl,
+            "DlSchedType": o.DlSchedType,
+            "Start": o.start,
+            "UlSchedType": o.UlSchedType,
+            "End": o.end,
+            "CellIndividualOffset": o.CellIndividualOffset,
+            "DlAllocRBRate": o.DlAllocRBRate,
+            "Name": o.name,
+            "UlAllocRBRate": o.UlAllocRBRate,
+            "Handover": self.get_handover_for_profile(o),
+        }
+        profile = json.dumps(profile)
+        return profile
+
+    def get_extra_attributes(self, o):
+        onos = self.get_onos_info(o)
+        fields = {
+            'onos_url': onos['url'],
+            'onos_username': onos['username'],
+            'onos_password': onos['password'],
+            'onos_port': onos['port'],
+            'endpoint': 'profile',
+            'profile': self.get_progran_profile_field(o),
+            'method': 'POST'
+        }
+
+        return fields
+
+    # 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)
+
+    def delete_record(self, o):
+        log.info("deleting object", object=str(o), **o.tologdict())
+        onos = self.get_onos_info(o)
+        fields = {
+            'onos_url': onos['url'],
+            'onos_username': onos['username'],
+            'onos_password': onos['password'],
+            'onos_port': onos['port'],
+            'endpoint': 'profile/%s' % o.name,
+            'profile': '',
+            'method': 'DELETE'
+        }
+        res = self.run_playbook(o, fields)
\ No newline at end of file