[CORD-1525] Populating relational fields and saving models
Change-Id: Ied02e7e48efed353ae4be8ebb277aef3b6af4cab
diff --git a/.gitignore b/.gitignore
index 635880e..59910ec 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
*.pyc
.noseids
local_certs.crt
+src/tosca/tmp.yaml
# Test Coverage
.coverage
diff --git a/src/tosca/parser.py b/src/tosca/parser.py
index af70d27..22e26a0 100644
--- a/src/tosca/parser.py
+++ b/src/tosca/parser.py
@@ -115,6 +115,13 @@
ordered_models_templates.append(templates_by_model_name[name])
return ordered_models_templates
+ @staticmethod
+ def populate_dependencies(model, requirements, saved_models):
+ for dep in requirements:
+ class_name = dep.keys()[0]
+ related_model = saved_models[dep[class_name]['node']]
+ setattr(model, "%s_id" % class_name, related_model.id)
+ return model
def __init__(self, recipe):
@@ -124,6 +131,8 @@
self.templates_by_model_name = None
# list of models ordered by requirements
self.ordered_models_name = None
+ # dictionary containing the saved model
+ self.saved_model_by_name = {}
self.ordered_models_template = []
self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml'
@@ -150,13 +159,16 @@
if class_name not in RESOURCES:
raise Exception("Nodetemplate %s's type %s is not a known resource" % (recipe.name, class_name))
model = GRPCModelsAccessor.get_model_from_classname(class_name, data)
- # [] populate model with data
+ # [] populate model with data[[
model = self.populate_model(model, data)
# [] check if the model has requirements
# [] if it has populate them
+ model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name)
# [] save or update
model.save()
+ self.saved_model_by_name[recipe.name] = model
+
except Exception as e:
print e
if e.message:
diff --git a/src/tosca/tmp.yaml b/src/tosca/tmp.yaml
deleted file mode 100644
index e5b9fbd..0000000
--- a/src/tosca/tmp.yaml
+++ /dev/null
@@ -1,39 +0,0 @@
-tosca_definitions_version: tosca_simple_yaml_1_0
-
-description: Persist xos-sample-gui-extension
-
-imports:
- - custom_types/user.yaml
- - custom_types/site.yaml
- - custom_types/xosguiextension.yaml
-
-topology_template:
- node_templates:
-
- # UI Extension
- test:
- type: tosca.nodes.XOSGuiExtension
- properties:
- name: test
- files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
-
- # Site
- onlab:
- type: tosca.nodes.Site
- properties:
- name: Open Networking Lab
- site_url: http://onlab.us/
- hosts_nodes: True
-
- # User
- test@opencord.org:
- type: tosca.nodes.User
- properties:
- password: mypwd
- firstname: User
- lastname: Test
- is_admin: True
- requirements:
- - site:
- node: onlab
- relationship: tosca.relationships.BelongsToOne
\ No newline at end of file
diff --git a/test/test_tosca_parser.py b/test/test_tosca_parser.py
index 9a369fe..6c0a14c 100644
--- a/test/test_tosca_parser.py
+++ b/test/test_tosca_parser.py
@@ -21,4 +21,32 @@
res = TOSCA_Parser.get_tosca_models_by_name(FakeTemplate)
self.assertIsInstance(res['model1'], FakeNode)
- self.assertIsInstance(res['model2'], FakeNode)
\ No newline at end of file
+ self.assertIsInstance(res['model2'], FakeNode)
+
+ def test_populate_dependencies(self):
+ """
+ [TOSCA_Parser] populate_dependencies: if a recipe has dependencies, it should find the ID of the requirements and add it to the model
+ """
+ class FakeRecipe:
+ requirements = [
+ {
+ 'site': {
+ 'node': 'site_onlab',
+ 'relationship': 'tosca.relationship.BelongsToOne'
+ }
+ }
+ ]
+
+ class FakeSite:
+ id = 1
+ name = 'onlab'
+
+ class FakeModel:
+ name = 'test@opencord.org'
+
+ saved_models = {
+ 'site_onlab': FakeSite
+ }
+
+ model = TOSCA_Parser.populate_dependencies(FakeModel, FakeRecipe.requirements, saved_models)
+ self.assertEqual(model.site_id, 1)
\ No newline at end of file
diff --git a/test/tosca/test.yaml b/test/tosca/test.yaml
index e5b9fbd..9fc2246 100644
--- a/test/tosca/test.yaml
+++ b/test/tosca/test.yaml
@@ -18,7 +18,7 @@
files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
# Site
- onlab:
+ site_onlab:
type: tosca.nodes.Site
properties:
name: Open Networking Lab
@@ -26,14 +26,16 @@
hosts_nodes: True
# User
- test@opencord.org:
+ user_test:
type: tosca.nodes.User
properties:
+ username: test@opencord.org
+ email: test@opencord.org
password: mypwd
firstname: User
lastname: Test
is_admin: True
requirements:
- site:
- node: onlab
+ node: site_onlab
relationship: tosca.relationships.BelongsToOne
\ No newline at end of file