tosco-parser supports importing the other service with 71/15171/2
authorshangxdy <shang.xiaodong@zte.com.cn>
Sat, 4 Jun 2016 17:37:11 +0000 (01:37 +0800)
committershangxdy <shang.xiaodong@zte.com.cn>
Sat, 4 Jun 2016 17:43:32 +0000 (01:43 +0800)
topology template

As a tosca-parser developer;
I want to implement the function of substitution_mapping;
So that must support the import topology template firstly

Change-Id: Ie26aab712e7870e1ec345ad654460bb84014f207
JIRA: PARSER-48
Signed-off-by: shangxdy <shang.xiaodong@zte.com.cn>
tosca2heat/tosca-parser/toscaparser/imports.py
tosca2heat/tosca-parser/toscaparser/tests/test_topology_template.py
tosca2heat/tosca-parser/toscaparser/tosca_template.py

index 5149382..62748bd 100644 (file)
@@ -36,6 +36,7 @@ class ImportsLoader(object):
                  tpl=None):
         self.importslist = importslist
         self.custom_defs = {}
+        self.nested_topo_tpls = []
         if not path and not tpl:
             msg = _('Input tosca template is not provided.')
             log.warning(msg)
@@ -55,6 +56,9 @@ class ImportsLoader(object):
     def get_custom_defs(self):
         return self.custom_defs
 
+    def get_nested_topo_tpls(self):
+        return self.nested_topo_tpls
+
     def _validate_and_load_imports(self):
         imports_names = set()
 
@@ -76,8 +80,8 @@ class ImportsLoader(object):
                             ValidationError(message=msg))
                     imports_names.add(import_name)
 
-                    custom_type = self._load_import_template(import_name,
-                                                             import_uri)
+                    full_file_name, custom_type = self._load_import_template(
+                        import_name, import_uri)
                     namespace_prefix = None
                     if isinstance(import_uri, dict):
                         namespace_prefix = import_uri.get(
@@ -86,13 +90,15 @@ class ImportsLoader(object):
                         TypeValidation(custom_type, import_def)
                         self._update_custom_def(custom_type, namespace_prefix)
             else:  # old style of imports
-                custom_type = self._load_import_template(None,
-                                                         import_def)
+                full_file_name, custom_type = self._load_import_template(
+                    None, import_def)
                 if custom_type:
                     TypeValidation(
                         custom_type, import_def)
                     self._update_custom_def(custom_type, None)
 
+            self._update_nested_topo_tpls(full_file_name, custom_type)
+
     def _update_custom_def(self, custom_type, namespace_prefix):
         outer_custom_types = {}
         for type_def in self.type_definition_list:
@@ -112,6 +118,11 @@ class ImportsLoader(object):
                     else:
                         self.custom_defs.update(outer_custom_types)
 
+    def _update_nested_topo_tpls(self, full_file_name, custom_tpl):
+        if full_file_name and custom_tpl:
+            topo_tpl = {full_file_name: custom_tpl}
+            self.nested_topo_tpls.append(topo_tpl)
+
     def _validate_import_keys(self, import_name, import_uri_def):
         if self.FILE not in import_uri_def.keys():
             log.warning(_('Missing keyname "file" in import "%(name)s".')
@@ -170,7 +181,7 @@ class ImportsLoader(object):
             return
 
         if toscaparser.utils.urlutils.UrlUtils.validate_url(file_name):
-            return YAML_LOADER(file_name, False)
+            return file_name, YAML_LOADER(file_name, False)
         elif not repository:
             import_template = None
             if self.path:
@@ -234,7 +245,7 @@ class ImportsLoader(object):
                     ImportError(_('Import "%s" is not valid.') %
                                 import_uri_def))
                 return
-            return YAML_LOADER(import_template, a_file)
+            return import_template, YAML_LOADER(import_template, a_file)
 
         if short_import_notation:
             log.error(_('Import "%(name)s" is not valid.') % import_uri_def)
@@ -261,7 +272,7 @@ class ImportsLoader(object):
                 return
 
         if toscaparser.utils.urlutils.UrlUtils.validate_url(full_url):
-            return YAML_LOADER(full_url, False)
+            return full_url, YAML_LOADER(full_url, False)
         else:
             msg = (_('repository url "%(n_uri)s" is not valid in import '
                      'definition "%(tpl)s".')
index 0f1a33e..cbd4e3b 100644 (file)
@@ -34,6 +34,11 @@ class TopologyTemplateTest(TestCase):
         self.topo = TopologyTemplate(self.topo_tpl,
                                      self._get_all_custom_def())
 
+        self.tosca_system_tpl_path = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/topology_template/system.yaml")
+        self.system_template = ToscaTemplate(self.tosca_system_tpl_path)
+
     def _get_custom_def(self, type_definition):
         custom_defs = {}
         for definition in self.imports:
index ba056da..1bad6e9 100644 (file)
@@ -69,6 +69,7 @@ class ToscaTemplate(object):
         self.input_path = None
         self.path = None
         self.tpl = None
+        self.nested_tosca_template = None
         if path:
             self.input_path = path
             self.path = self._get_path(path)
@@ -177,9 +178,14 @@ class ToscaTemplate(object):
             imports = self._tpl_imports()
 
         if imports:
-            custom_defs = toscaparser.imports.\
+            custom_service = toscaparser.imports.\
                 ImportsLoader(imports, self.path,
-                              type_defs, self.tpl).get_custom_defs()
+                              type_defs, self.tpl)
+
+            nested_topo_tpls = custom_service.get_nested_topo_tpls()
+            self._handle_nested_topo_tpls(nested_topo_tpls)
+
+            custom_defs = custom_service.get_custom_defs()
             if not custom_defs:
                 return
 
@@ -191,6 +197,14 @@ class ToscaTemplate(object):
                     custom_defs.update(inner_custom_types)
         return custom_defs
 
+    def _handle_nested_topo_tpls(self, nested_topo_tpls):
+        for tpl in nested_topo_tpls:
+            if tpl.get(TOPOLOGY_TEMPLATE):
+                nested_tosca_template = ToscaTemplate(
+                    path=self.path, parsed_params=self.parsed_params,
+                    yaml_dict_tpl=nested_topo_tpls)
+                self.nested_tosca_template.apend(nested_tosca_template)
+
     def _validate_field(self):
         version = self._tpl_version()
         if not version: