[CORD-2640] Creating and deleting eNodeB

Change-Id: I5acc72415acfb29e096337cff450441878383e6a
diff --git a/samples/profile.json b/samples/profile.json
index eb39e1d..fe026c1 100644
--- a/samples/profile.json
+++ b/samples/profile.json
@@ -9,6 +9,6 @@
   "DlAllocRBRate":1,
   "name":"test-profile",
   "UlAllocRBRate":12,
-  "enodeb_id":2,
+  "enodeb_id":1,
   "handover_id":1
 }
\ No newline at end of file
diff --git a/xos/models/models.py b/xos/models/models.py
index 2e7df7b..594255a 100644
--- a/xos/models/models.py
+++ b/xos/models/models.py
@@ -15,6 +15,14 @@
 class ProgranService(ProgranService_decl):
     class Meta:
         proxy = True
+    def save(self, *args, **kwargs):
+
+        existing_services = ProgranService.objects.all()
+
+        if len(existing_services) > 0 and not self.delete:
+            raise XOSValidationError("A ProgranService already exists, you should not have more than one")
+
+        super(ProgranService, self).save(*args, **kwargs)
 
 
 class ENodeB(ENodeB_decl):
@@ -33,19 +41,18 @@
 
     def save(self, *args, **kwargs):
         # NOTE someone is setting owner_id, so just override it for now
-        # if not self.owner_id:
-        services = Service.objects.all()
-        services = [s for s in services if s.name.lower() == 'progran']
-
-        # NOTE select the correct owner
         try:
-            progran_service = services[0]
+            # NOTE we allow just one ProgranService
+            progran_service = ProgranService.objects.all()[0]
             self.owner_id = progran_service.id
         except IndexError:
             raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
 
         # NOTE if the instance is new, check that the name is not duplicated
         instances_with_same_name = None
+        # FIXME This may leave us vulnerable to someone changing the name at a later time and causing a conflict.
+        # If that's important to prevent, we could prevent that case when `self.pk!=None`,
+        # filter for ProgranServiceInstance with the same name but `pk!=self.pk`.
         if self.pk is None:
             try:
                 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
diff --git a/xos/models/progran.xproto b/xos/models/progran.xproto
index 14206ac..909f6e1 100644
--- a/xos/models/progran.xproto
+++ b/xos/models/progran.xproto
@@ -43,8 +43,8 @@
     required string end = 6 [content_type = "date", null = False, blank = True];
     required string AdmControl = 7 [default = "0", choices = "(('0', 'ALL'), ('1', 'Voice Only'), ('2', 'Data Only'))", max_length = 1, blank = False, null = False, db_index = False];
     required int32 CellIndividualOffset = 8 [db_index = False, null = False, blank = False];
-    required manytoone enodeb->ENodeB:profiles = 9 [db_index = True, null = False, blank = False];
-    required manytoone handover->Handover:profiles = 10 [db_index = True, null = True, blank = False];
+    required manytoone enodeb->ENodeB:profiles = 9 [null = True, blank = True];
+    required manytoone handover->Handover:profiles = 10 [null = False, blank = False];
 }
 
 
diff --git a/xos/synchronizer/steps/helpers.py b/xos/synchronizer/steps/helpers.py
new file mode 100644
index 0000000..6795068
--- /dev/null
+++ b/xos/synchronizer/steps/helpers.py
@@ -0,0 +1,40 @@
+# 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.
+
+from xosconfig import Config
+from multistructlog import create_logger
+from synchronizers.new_base.modelaccessor import ProgranService
+
+log = create_logger(Config().get('logging'))
+
+class ProgranHelpers():
+
+    @staticmethod
+    def get_onos_info_from_si(service_instance):
+        progran_service = service_instance.owner.leaf_model
+        return ProgranHelpers.get_onos_info_from_service(progran_service)
+
+    @staticmethod
+    def get_progran_onos_info():
+        progran_service = ProgranService.objects.all()[0]
+        return ProgranHelpers.get_onos_info_from_service(progran_service)
+
+    @staticmethod
+    def get_onos_info_from_service(progran_service):
+        return {
+            'url': progran_service.onos_address,
+            'port': progran_service.onos_port,
+            'username': progran_service.onos_username,
+            'password': progran_service.onos_password,
+        }
diff --git a/xos/synchronizer/steps/sync_enodeb.py b/xos/synchronizer/steps/sync_enodeb.py
new file mode 100644
index 0000000..8d65974
--- /dev/null
+++ b/xos/synchronizer/steps/sync_enodeb.py
@@ -0,0 +1,84 @@
+
+# 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 ENodeB
+
+from xosconfig import Config
+from multistructlog import create_logger
+import json
+
+from helpers import ProgranHelpers
+
+log = create_logger(Config().get('logging'))
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+class SyncProgranEnodeB(SyncInstanceUsingAnsible):
+    provides = [ENodeB]
+
+    observes = ENodeB
+
+    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_progran_enodeb_field(self, o):
+
+        enodeb = {
+            "eNBId": o.enbId,
+	        "Description": o.description,
+	        "IpAddr": o.ipAddr
+        }
+        enodeb = json.dumps(enodeb)
+        return enodeb
+
+    def get_extra_attributes(self, o):
+        onos = ProgranHelpers.get_progran_onos_info()
+        fields = {
+            'onos_url': onos['url'],
+            'onos_username': onos['username'],
+            'onos_password': onos['password'],
+            'onos_port': onos['port'],
+            'endpoint': 'enodeb',
+            'profile': self.get_progran_enodeb_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 = ProgranHelpers.get_progran_onos_info()
+        fields = {
+            'onos_url': onos['url'],
+            'onos_username': onos['username'],
+            'onos_password': onos['password'],
+            'onos_port': onos['port'],
+            'endpoint': 'enodeb/%s' % o.enbId,
+            'profile': '',
+            'method': 'DELETE'
+        }
+        res = self.run_playbook(o, fields)
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_progranserviceinstance.py b/xos/synchronizer/steps/sync_progranserviceinstance.py
index 6a0d84c..b8cbd3b 100644
--- a/xos/synchronizer/steps/sync_progranserviceinstance.py
+++ b/xos/synchronizer/steps/sync_progranserviceinstance.py
@@ -18,12 +18,14 @@
 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 synchronizers.new_base.modelaccessor import ProgranServiceInstance
 
 from xosconfig import Config
 from multistructlog import create_logger
 import json
 
+from helpers import ProgranHelpers
+
 log = create_logger(Config().get('logging'))
 
 parentdir = os.path.join(os.path.dirname(__file__), "..")
@@ -34,17 +36,6 @@
 
     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
@@ -81,19 +72,51 @@
         profile = json.dumps(profile)
         return profile
 
-    def get_extra_attributes(self, o):
-        onos = self.get_onos_info(o)
-        fields = {
+    def sync_record(self, o):
+        # NOTE overriding the default sync_record as we need to execute the playbook 2 times (profile and enodeb)
+
+        log.info("sync'ing profile", object=str(o), **o.tologdict())
+        onos = ProgranHelpers.get_onos_info_from_si(o)
+
+        # common field for both operations
+        base_field = {
             'onos_url': onos['url'],
             'onos_username': onos['username'],
             'onos_password': onos['password'],
             'onos_port': onos['port'],
+        }
+
+        # progran profile specific fields
+        profile_fields = {
             'endpoint': 'profile',
             'profile': self.get_progran_profile_field(o),
             'method': 'POST'
         }
+        profile_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id))
+        profile_fields.update(base_field)
+        self.run_playbook(o, profile_fields)
 
-        return fields
+        # progran enodeb specific fields
+        if o.enodeb:
+            log.info("adding profile to enodeb", object=str(o), **o.tologdict())
+            enodeb_fields = {
+                'profile': json.dumps({
+                    "ProfileArray": [
+                        o.name
+                    ]
+                }),
+                'method': 'POST',
+                'endpoint': 'enodeb/%s/profile' % o.enodeb.enbId
+            }
+            enodeb_fields["ansible_tag"] =  o.__class__.__name__ + "_" + str(o.id) + "_enodeb_to_profile"
+            enodeb_fields.update(base_field)
+            self.run_playbook(o, enodeb_fields)
+        else:
+            log.warn("IMPLEMENT THE CALL TO REMOVE A PROFILE FROM ENODEB")
+
+
+        o.save()
+
 
     # FIXME we need to override this as the default expect to ssh into a VM
     def run_playbook(self, o, fields):
@@ -101,7 +124,7 @@
 
     def delete_record(self, o):
         log.info("deleting object", object=str(o), **o.tologdict())
-        onos = self.get_onos_info(o)
+        onos = ProgranHelpers.get_onos_info_from_si(o)
         fields = {
             'onos_url': onos['url'],
             'onos_username': onos['username'],