[CORD-2715] making sure we are not deleting object with must-exist key set to true, as they are included for reference

Change-Id: I07587e6f57b4558554712d67cd21f161c4fe8df5
(cherry picked from commit 91d7aebf39a6e3fdc374eef59cb9e9536211c296)
diff --git a/src/tosca/parser.py b/src/tosca/parser.py
index 854ff00..9166dd2 100644
--- a/src/tosca/parser.py
+++ b/src/tosca/parser.py
@@ -222,9 +222,15 @@
                     model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name)
                     # [] save, update or delete
 
-                    if self.delete and not model.is_new:
+                    reference_only = False
+                    if 'must-exist' in data:
+                        reference_only = True
+
+                    if self.delete and not model.is_new and not reference_only:
+                        print "[XOS-Tosca] Deleting model %s[%s]" % (class_name, model.id)
                         model.delete()
                     elif not self.delete:
+                        print "[XOS-Tosca] Saving model %s[%s]" % (class_name, model.id)
                         model.save()
 
                     self.saved_model_by_name[recipe.name] = model
@@ -247,6 +253,7 @@
                 exception_msg = e._state.details
             raise Exception(exception_msg)
         except Exception, e:
+            print e
             raise Exception(e)
 
 
diff --git a/test/test_tosca_parser_e2e.py b/test/test_tosca_parser_e2e.py
index 6a44b6f..2d2649f 100644
--- a/test/test_tosca_parser_e2e.py
+++ b/test/test_tosca_parser_e2e.py
@@ -41,6 +41,9 @@
 class FakeUser:
     objects = FakeObj
 
+class FakeNode:
+    objects = FakeObj
+
 USERNAME = "username"
 PASSWORD = "pass"
 
@@ -49,7 +52,8 @@
     'XOSGuiExtension': FakeGuiExt,
     'Site': FakeSite,
     'User': FakeUser,
-    'Instance': FakeInstance
+    'Instance': FakeInstance,
+    'Node': FakeNode
 }
 
 class TOSCA_Parser_E2E(unittest.TestCase):
@@ -97,8 +101,9 @@
 
     @patch.dict(RESOURCES, mock_resources, clear=True)
     @patch.object(FakeGuiExt.objects, 'filter', MagicMock(return_value=[FakeModel]))
+    @patch.object(FakeNode.objects, 'filter', MagicMock(return_value=[FakeModel]))
     @patch.object(FakeModel, 'delete')
-    def test_basic_deletion(self, mock_delete):
+    def test_basic_deletion(self, mock_model):
         """
         [TOSCA_Parser] Should delete models defined in a TOSCA recipe
         """
@@ -108,12 +113,18 @@
     description: Persist xos-sample-gui-extension
 
     imports:
-       - custom_types/xosguiextension.yaml
+        - custom_types/node.yaml
+        - custom_types/xosguiextension.yaml
 
     topology_template:
       node_templates:
 
-        # UI Extension
+        should_stay:
+          type: tosca.nodes.Node
+          properties:
+            name: should_stay
+            must-exist: true
+
         test:
           type: tosca.nodes.XOSGuiExtension
           properties:
@@ -126,10 +137,10 @@
         parser.execute()
 
         # checking that the model has been saved
-        mock_delete.assert_called()
+        mock_model.assert_called_once()
 
         self.assertIsNotNone(parser.templates_by_model_name['test'])
-        self.assertEqual(parser.ordered_models_name, ['test'])
+        self.assertEqual(parser.ordered_models_name, ['test', 'should_stay'])
 
     @patch.dict(RESOURCES, mock_resources, clear=True)
     @patch.object(FakeSite.objects, 'filter', MagicMock(return_value=[FakeModel]))