From 13adc548facb5a74292f6305aacc0a0f0677c646 Mon Sep 17 00:00:00 2001 From: Nandini Date: Fri, 15 Jul 2016 16:21:40 +0530 Subject: [PATCH] Added Support for Unsupported Types Checks for the unsupported type in the template. Raises an error if a type is a valid TOSCA type but mappting to HOT is not yet provided in the translator. Co-Authored-By: Sahdev Zala Change-Id: If6e821b1b2db4f01dd302ff068f3d4f0b73661b2 Partially-Implements: blueprint unsupported-type-validation --- translator/common/exception.py | 5 +++++ translator/hot/translate_node_templates.py | 5 +++++ .../tests/data/test_tosca_unsupported_type.yaml | 15 +++++++++++++++ translator/tests/test_tosca_hot_translation.py | 15 +++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 translator/tests/data/test_tosca_unsupported_type.yaml diff --git a/translator/common/exception.py b/translator/common/exception.py index be861169..f16d3d7d 100644 --- a/translator/common/exception.py +++ b/translator/common/exception.py @@ -43,6 +43,11 @@ class ToscaClassImportError(TOSCAException): 'exists and has no language definition errors.') +class UnsupportedTypeError(TOSCAException): + msg_fmt = _('Type "%(type)s" is valid TOSCA type but translation ' + 'support is not yet available.') + + class ToscaClassAttributeError(TOSCAException): msg_fmt = _('Class attribute referenced not found. ' '%(message)s. Check to see that it is defined.') diff --git a/translator/hot/translate_node_templates.py b/translator/hot/translate_node_templates.py index 8b9ed99d..6f967bec 100644 --- a/translator/hot/translate_node_templates.py +++ b/translator/hot/translate_node_templates.py @@ -27,6 +27,7 @@ from toscaparser.utils.gettextutils import _ from translator.common.exception import ToscaClassAttributeError from translator.common.exception import ToscaClassImportError from translator.common.exception import ToscaModImportError +from translator.common.exception import UnsupportedTypeError from translator.common import utils from translator.conf.config import ConfigProvider as translatorConfig from translator.hot.syntax.hot_resource import HotResource @@ -180,6 +181,8 @@ class TranslateNodeTemplates(object): # Copy the TOSCA graph: nodetemplate for node in self.nodetemplates: base_type = HotResource.get_base_type_str(node.type_definition) + if base_type not in TOSCA_TO_HOT_TYPE: + raise UnsupportedTypeError(type=_('%s') % base_type) hot_node = TOSCA_TO_HOT_TYPE[base_type](node) self.hot_resources.append(hot_node) self.hot_lookup[node] = hot_node @@ -220,6 +223,8 @@ class TranslateNodeTemplates(object): for policy in self.policies: policy_type = policy.type_definition + if policy_type.type not in TOSCA_TO_HOT_TYPE: + raise UnsupportedTypeError(type=_('%s') % policy_type.type) policy_node = TOSCA_TO_HOT_TYPE[policy_type.type](policy) self.hot_resources.append(policy_node) diff --git a/translator/tests/data/test_tosca_unsupported_type.yaml b/translator/tests/data/test_tosca_unsupported_type.yaml new file mode 100644 index 00000000..fdbb7719 --- /dev/null +++ b/translator/tests/data/test_tosca_unsupported_type.yaml @@ -0,0 +1,15 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: Template to test unsupported translation. Load Balancer is a + > valid TOSCA type but not supported in translator yet. + +topology_template: + node_templates: + simple_load_balancer: + type: tosca.nodes.LoadBalancer + capabilities: + client: + properties: + network_name: PUBLIC + floating: true + dns_name: http://mycompany.com/ \ No newline at end of file diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index facff401..f243a3b9 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -16,8 +16,11 @@ import os from toscaparser.common.exception import ExceptionCollector from toscaparser.common.exception import URLException from toscaparser.common.exception import ValidationError +from toscaparser.tosca_template import ToscaTemplate from toscaparser.utils.gettextutils import _ +from translator.common.exception import UnsupportedTypeError from translator.common.utils import TranslationUtils +from translator.hot.tosca_translator import TOSCATranslator from translator.tests.base import TestCase @@ -495,3 +498,15 @@ class ToscaHotTranslationTest(TestCase): hot_file = '../tests/data/hot_output/hot_autoscaling.yaml' params = {} self._test_successful_translation(tosca_file, hot_file, params) + + def test_translate_unsupported_tosca_type(self): + tosca_file = '../tests/data/test_tosca_unsupported_type.yaml' + tosca_tpl = os.path.normpath(os.path.join( + os.path.dirname(os.path.abspath(__file__)), tosca_file)) + params = {} + expected_msg = _('Type "tosca.nodes.LoadBalancer" is valid TOSCA ' + 'type but translation support is not yet available.') + tosca = ToscaTemplate(tosca_tpl, params, True) + err = self.assertRaises(UnsupportedTypeError, + TOSCATranslator(tosca, params).translate) + self.assertEqual(expected_msg, err.__str__())