Repository: xlab-si/xopera-opera

Issue:
node_templates section defined in imported service templates
## Description
In some cases, it would be useful to import a service template that already contains some node_templates defined. For example, in a situation, when a service template describing existing infastruture that cannot be modified externally (e.g. HPC nodes) is automatically generated by some service and later imported into other templates.  
## Prerequisites
- `opera` installed
- A valid TOSCA Service Template file `service.yaml` that contains an import definition of another valid TOSCA Service Template file with **topology_template** and **node_templates** sections defined.
## Steps
- User runs deploy `opera deploy service.yaml`
## Current behaviour
- xOpera returns an error `Duplicate keys 'node_templates' found in service.yml`
## Expected behavior
- Service template is deployed without error.


Comments:

Patch:
diff --git a/src/opera/parser/tosca/v_1_3/topology_template.py b/src/opera/parser/tosca/v_1_3/topology_template.py
index 1af5075..ce17793 100644
--- a/src/opera/parser/tosca/v_1_3/topology_template.py
+++ b/src/opera/parser/tosca/v_1_3/topology_template.py
@@ -64,3 +64,20 @@ class TopologyTemplate(Entity):
                 ),
             ) for name, definition in self.get("outputs", {}).items()
         }
+
+    def merge(self, other):
+        for key in (
+                "inputs",
+                "node_templates",
+                "data_types",
+                "relationship_templates",
+                "groups",
+                "policies",
+                "outputs"
+        ):
+            if key not in other.data:
+                continue
+            if key in self.data:
+                self.data[key].merge(other.data[key])
+            else:
+                self.data[key] = other.data[key]


Test patch:
diff --git a/tests/unit/opera/parser/test_tosca.py b/tests/unit/opera/parser/test_tosca.py
index 88741eb..15607c7 100644
--- a/tests/unit/opera/parser/test_tosca.py
+++ b/tests/unit/opera/parser/test_tosca.py
@@ -140,3 +140,68 @@ class TestLoad:
         ))
 
         tosca.load(tmp_path, name)
+
+    def test_merge_topology_template(self, tmp_path, yaml_text):
+        name = pathlib.PurePath("template.yaml")
+        (tmp_path / name).write_text(yaml_text(
+            """
+            tosca_definitions_version: tosca_simple_yaml_1_3
+            imports:
+              - merge.yaml
+            topology_template:
+              inputs:
+                some-input:
+                  type: string
+              node_templates:
+                my_node:
+                  type: tosca.nodes.SoftwareComponent              
+            """
+        ))
+        (tmp_path / "merge.yaml").write_text(yaml_text(
+            """
+            tosca_definitions_version: tosca_simple_yaml_1_3
+            topology_template:
+              inputs:
+                other-input:
+                  type: string
+              node_templates:
+                other_node:
+                  type: tosca.nodes.SoftwareComponent 
+            """
+        ))
+        tosca.load(tmp_path, name)
+
+    def test_merge_duplicate_node_templates_invalid(self, tmp_path, yaml_text):
+        name = pathlib.PurePath("template.yaml")
+        (tmp_path / name).write_text(yaml_text(
+            """
+            tosca_definitions_version: tosca_simple_yaml_1_3
+            imports:
+              - merge1.yaml
+              - merge2.yaml
+            topology_template:
+              node_templates:
+                my_node:
+                  type: tosca.nodes.SoftwareComponent              
+            """
+        ))
+        (tmp_path / "merge1.yaml").write_text(yaml_text(
+            """
+            tosca_definitions_version: tosca_simple_yaml_1_3
+            topology_template:
+              node_templates:
+                other_node:
+                  type: tosca.nodes.SoftwareComponent 
+            """
+        ))
+        (tmp_path / "merge2.yaml").write_text(yaml_text(
+            """
+            tosca_definitions_version: tosca_simple_yaml_1_3
+            topology_template:
+              node_templates:
+                other_node:
+                  type: tosca.nodes.SoftwareComponent 
+            """
+        ))
+        with pytest.raises(ParseError):        
+            tosca.load(tmp_path, name)      
\ No newline at end of file