[CORD-2550] Fixing bugs

Change-Id: I386148f2b1173f5c646b94d05f629a50531ae468
diff --git a/xos/models/models.py b/xos/models/models.py
index 25a37df..fef3fb1 100644
--- a/xos/models/models.py
+++ b/xos/models/models.py
@@ -75,6 +75,9 @@
         proxy = True
 
     def save(self, *args, **kwargs):
+
+        # TODO we should not allow name changes as name is the mapping with the backend
+
         # NOTE someone is setting owner_id, so just override it for now
         try:
             # NOTE we allow just one ProgranService
@@ -83,6 +86,14 @@
         except IndexError:
             raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
 
+        # name is mandatory
+        if not self.name:
+            raise XOSValidationError("name is mandatory for ProgranServiceInstances")
+
+        # NOTE this check is disabled as when Progran create a profile it fails
+        # if self.DlUeAllocRbRate > self.DlAllocRBRate:
+        #     raise XOSValidationError("DlUeAllocRbRate (%s) cannot be bigger than DlAllocRBRate (%s)" % (self.DlUeAllocRbRate, self.DlAllocRBRate))
+
         # prevent name duplicates
         try:
             instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
@@ -116,6 +127,17 @@
             if total_down > limit:
                 raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
 
+        caller_kind = "xos"
+
+        if "caller_kind" in kwargs:
+            caller_kind = kwargs.pop("caller_kind")
+
+        print "Profile %s has been saved by %s" % (self.name, caller_kind)
+
+        if caller_kind == "xos":
+            print "Setting no_sync to false for profile %s" % self.name
+            self.no_sync = False
+
         super(ProgranServiceInstance, self).save(*args, **kwargs)
 
 
diff --git a/xos/models/progran.xproto b/xos/models/progran.xproto
index 9c76944..a6935de 100644
--- a/xos/models/progran.xproto
+++ b/xos/models/progran.xproto
@@ -21,14 +21,14 @@
 
 message Handover (XOSBase){
     option verbose_name = "Handover";
-    required int32 A3offset = 1 [default = 2, db_index = False, null = False, blank = False];
-    required int32 HysteresisA3 = 2 [default = 1, db_index = False, null = False, blank = False];
+    required int32 A3offset = 1 [default = 1, db_index = False, null = False, blank = False];
+    required int32 HysteresisA3 = 2 [default = 0, db_index = False, null = False, blank = False];
     required int32 A3TriggerQuantity = 3 [default = 0, db_index = False, null = False, blank = False];
     required int32 A5TriggerType = 4 [default = 0, db_index = False, null = False, blank = False];
-    required int32 A5Thresh1Rsrp = 5 [default = -97, db_index = False, null = False, blank = False];
-    required int32 A5Thresh1Rsrq = 6 [default = -10, db_index = False, null = False, blank = False];
-    required int32 A5Thresh2Rsrp = 7 [default = -95, db_index = False, null = False, blank = False];
-    required int32 A5Thresh2Rsrq = 8 [default = -8, db_index = False, null = False, blank = False];
+    required int32 A5Thresh1Rsrp = 5 [default = 74, db_index = False, null = False, blank = False];
+    required int32 A5Thresh1Rsrq = 6 [default = 10, db_index = False, null = False, blank = False];
+    required int32 A5Thresh2Rsrp = 7 [default = 78, db_index = False, null = False, blank = False];
+    required int32 A5Thresh2Rsrq = 8 [default = 10, db_index = False, null = False, blank = False];
     required int32 HysteresisA5 = 9 [default = 1, db_index = False, null = False, blank = False];
     required int32 A5TriggerQuantity = 10 [default = 0, db_index = False, null = False, blank = False];
     optional string created_by = 11 [null = True, blank = True, gui_hidden = True];
@@ -38,18 +38,18 @@
     option verbose_name = "Progran Service Instance";
     option description = "Represent a Profile in the Progran ONOS Application";
 
-    required string DlSchedType = 1 [default = "vm", choices = "(('RR', 'Round Robin'),)", max_length = 30, blank = False, null = False, db_index = False];
-    required int32 DlAllocRBRate = 2 [db_index = False, null = False, blank = False];
-    required string UlSchedType = 3 [default = "vm", choices = "(('RR', 'Round Robin'),)", max_length = 30, blank = False, null = False, db_index = False];
-    required int32 UlAllocRBRate = 4 [db_index = False, null = False, blank = False];
+    required string DlSchedType = 1 [default = "RR", choices = "(('RR', 'Round Robin'), ('PF', 'Proportional Fairness'), ('MAXCI', 'Maximum C/I'))", max_length = 30, blank = False, null = False, db_index = False];
+    required int32 DlAllocRBRate = 2 [default = "0", db_index = False, null = False, blank = False];
+    required string UlSchedType = 3 [default = "RR", choices = "(('RR', 'Round Robin'), ('PF', 'Proportional Fairness'), ('MAXCI', 'Maximum C/I'))", max_length = 30, blank = False, null = False, db_index = False];
+    required int32 UlAllocRBRate = 4 [default = "0", db_index = False, null = False, blank = False];
     required string start = 5 [content_type = "date", null = True, blank = True];
     required string end = 6 [content_type = "date", null = True, blank = True];
     required int32 AdmControl = 7 [default = "0", choices = "(('0', 'ALL'), ('1', 'Voice Only'), ('2', 'Data Only'))", blank = False, null = False, db_index = False];
     required int32 CellIndividualOffset = 8 [db_index = False, null = False, blank = False];
-    required string mmeip = 9 [db_index = False, max_length = 256, null = False, blank = False];
-    required string mmeport = 10 [db_index = False, max_length = 256, null = False, blank = False];
+    required string mmeip = 9 [db_index = False, default = "0.0.0.0", max_length = 256, null = False, blank = False];
+    required string mmeport = 10 [db_index = False, default = "8080", max_length = 256, null = False, blank = False];
     required int32 DlWifiRate = 11 [default = 100, db_index = False, null = False, blank = False];
-    required int32 DlUeAllocRbRate = 12 [default = 100, db_index = False, null = False, blank = False];
+    required int32 DlUeAllocRbRate = 12 [default = "0", help_text = "DL Per UE allocation", db_index = False, null = True, blank = True];
     required string SubsProfile = 13 [ db_index = False, null = True, blank = True];
     optional bool Status = 14 [ db_index = False, null = False, blank = True, default = False];
     optional manytoone enodeb->ENodeB:profiles = 15 [null = True, blank = True];
diff --git a/xos/synchronizer/steps/sync_progranserviceinstance.py b/xos/synchronizer/steps/sync_progranserviceinstance.py
index 69a860e..656cae1 100644
--- a/xos/synchronizer/steps/sync_progranserviceinstance.py
+++ b/xos/synchronizer/steps/sync_progranserviceinstance.py
@@ -50,10 +50,10 @@
         log.debug("Sync'ing profile with data", request_data=data)
 
         if o.previously_sync == False:
-            log.debug("Sending POST")
+            log.debug("Sending POST", url=profile_url, data=json.dumps(data))
             r = requests.post(profile_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
         else:
-            log.debug("Sending PUT")
+            log.debug("Sending PUT", url=profile_url, data=json.dumps(data))
             r = requests.put(profile_url, data=json.dumps(data),
                               auth=HTTPBasicAuth(onos['username'], onos['password']))
 
diff --git a/xos/synchronizer/steps/sync_progranserviceinstance_back.py b/xos/synchronizer/steps/sync_progranserviceinstance_back.py
index bc94976..8b38d74 100644
--- a/xos/synchronizer/steps/sync_progranserviceinstance_back.py
+++ b/xos/synchronizer/steps/sync_progranserviceinstance_back.py
@@ -25,7 +25,6 @@
 
 from xosconfig import Config
 from multistructlog import create_logger
-import json
 import requests
 from requests.auth import HTTPBasicAuth
 
@@ -89,6 +88,13 @@
             try:
                 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()
 
@@ -124,6 +130,8 @@
             si.no_sync = True
             si.previously_sync = True
 
+            si.enacted = time.mktime(datetime.datetime.now().timetuple())
+
             si.save()
 
             updated_profiles.append(si.name)
@@ -138,4 +146,5 @@
                 if si.created_by == 'XOS' and si.previously_sync == False:
                     # don't delete if the profile has been created by XOS and it hasn't been sync'ed yet
                     continue
+                # TODO delete also the associated Handover
                 si.delete()