[CORD-2550] Fixing profile/enodeb update and adding debug info
[CORD-2714] Not adding profiles to the same handover

Change-Id: I7f84362c8de43753f9712f53fbf1bead3ece58f3
diff --git a/xos/models/models.py b/xos/models/models.py
index babf8b5..25a37df 100644
--- a/xos/models/models.py
+++ b/xos/models/models.py
@@ -1,3 +1,17 @@
+# 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 xos.exceptions import XOSValidationError
 
 from models_decl import ProgranService_decl
@@ -5,12 +19,6 @@
 from models_decl import Handover_decl
 from models_decl import ProgranServiceInstance_decl
 
-
-
-
-
-
-
 class ProgranService(ProgranService_decl):
     class Meta:
         proxy = True
@@ -43,6 +51,10 @@
         except self.DoesNotExist:
             pass
 
+        if self.is_new and not self.created_by:
+            # NOTE if created_by is null it has been created by XOS
+            self.created_by = "XOS"
+
         super(ENodeB, self).save(*args, **kwargs)
 
 
@@ -50,6 +62,13 @@
     class Meta:
         proxy = True
 
+    def save(self, *args, **kwargs):
+        if self.is_new and not self.created_by:
+            # NOTE if created_by is null it has been created by XOS
+            self.created_by = "XOS"
+        super(Handover, self).save(*args, **kwargs)
+
+
 
 class ProgranServiceInstance(ProgranServiceInstance_decl):
     class Meta:
@@ -73,24 +92,30 @@
         except self.DoesNotExist:
             pass
 
+        if self.is_new and not self.created_by:
+            # NOTE if created_by is null it has been created by XOS
+            self.created_by = "XOS"
+
+
         # check that the sum of upload and download rate for a single enodeb is not greater than 95
-        limit = 95
-        same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id)
+        if not self.deleted:
+            limit = 95
+            same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id)
 
-        total_up = self.UlAllocRBRate
-        total_down = self.DlAllocRBRate
+            total_up = self.UlAllocRBRate
+            total_down = self.DlAllocRBRate
 
-        for p in same_enodeb:
-            total_up = total_up + p.UlAllocRBRate
-            total_down = total_down + p.DlAllocRBRate
+            for p in same_enodeb:
+                if p.pk != self.pk:
+                    total_up = total_up + p.UlAllocRBRate
+                    total_down = total_down + p.DlAllocRBRate
 
-        if total_up > limit:
-            raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
+            if total_up > limit:
+                raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
 
-        if total_down > limit:
-            raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
+            if total_down > limit:
+                raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
 
-        # TODO when saving set status to "in progress"
         super(ProgranServiceInstance, self).save(*args, **kwargs)
 
 
diff --git a/xos/models/progran.xproto b/xos/models/progran.xproto
index b7e5c92..9c76944 100644
--- a/xos/models/progran.xproto
+++ b/xos/models/progran.xproto
@@ -15,6 +15,8 @@
     required string description = 1 [db_index = False, max_length = 256, null = False, blank = False];
     required string enbId = 2 [help_text = "ID of this enodeb", db_index = False, max_length = 256, null = False, blank = False];
     required string ipAddr = 3 [help_text = "IP address of this enodeb", db_index = False, max_length = 256, null = False, blank = False];
+    optional string created_by = 4 [null = True, blank = True, gui_hidden = True];
+    optional bool previously_sync = 5 [null = False, blank = True, default=False, gui_hidden = True];
 }
 
 message Handover (XOSBase){
@@ -29,6 +31,7 @@
     required int32 A5Thresh2Rsrq = 8 [default = -8, 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];
 }
 
 message ProgranServiceInstance (ServiceInstance){
@@ -52,6 +55,8 @@
     optional manytoone enodeb->ENodeB:profiles = 15 [null = True, blank = True];
     required manytoone handover->Handover:profiles = 16 [null = False, blank = False];
     optional int32 active_enodeb_id = 17 [null = True, blank = True, gui_hidden = True];
+    optional string created_by = 17 [null = True, blank = True, gui_hidden = True];
+    optional bool previously_sync = 18 [null = False, blank = True, default=False, gui_hidden = True];
 }