ariatosca-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject incubator-ariatosca git commit: Refactor capability validation
Date Wed, 16 Aug 2017 15:55:31 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-348-validate-substitution-mapping [created] b1d59e798


Refactor capability validation


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/b1d59e79
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/b1d59e79
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/b1d59e79

Branch: refs/heads/ARIA-348-validate-substitution-mapping
Commit: b1d59e79899e003a2e683a9ef79a6e7af2d6a27c
Parents: df2b916
Author: Avia Efrat <avia@gigaspaces.com>
Authored: Wed Aug 16 18:54:57 2017 +0300
Committer: Avia Efrat <avia@gigaspaces.com>
Committed: Wed Aug 16 18:54:57 2017 +0300

----------------------------------------------------------------------
 .../aria_extension_tosca/simple_v1_0/misc.py    |  4 +-
 .../modeling/substitution_mappings.py           | 85 ++++++++++++--------
 2 files changed, 53 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b1d59e79/extensions/aria_extension_tosca/simple_v1_0/misc.py
----------------------------------------------------------------------
diff --git a/extensions/aria_extension_tosca/simple_v1_0/misc.py b/extensions/aria_extension_tosca/simple_v1_0/misc.py
index a65ff41..e3e1ca5 100644
--- a/extensions/aria_extension_tosca/simple_v1_0/misc.py
+++ b/extensions/aria_extension_tosca/simple_v1_0/misc.py
@@ -26,7 +26,7 @@ from aria.parser.presentation import (AsIsPresentation, has_fields, allow_unknow
 from .modeling.data_types import (get_data_type, get_data_type_value, get_property_constraints,
                                   apply_constraint_to_value)
 from .modeling.substitution_mappings import (validate_subtitution_mappings_requirement,
-                                             validate_subtitution_mappings_capability)
+                                             validate_substitution_mappings_capability)
 from .presentation.extensible import ExtensiblePresentation
 from .presentation.field_validators import (constraint_clause_field_validator,
                                             constraint_clause_in_range_validator,
@@ -400,7 +400,7 @@ class SubstitutionMappingsCapability(AsIsPresentation):
 
     def _validate(self, context):
         super(SubstitutionMappingsCapability, self)._validate(context)
-        validate_subtitution_mappings_capability(context, self)
+        validate_substitution_mappings_capability(context, self)
 
 
 @has_fields

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b1d59e79/extensions/aria_extension_tosca/simple_v1_0/modeling/substitution_mappings.py
----------------------------------------------------------------------
diff --git a/extensions/aria_extension_tosca/simple_v1_0/modeling/substitution_mappings.py
b/extensions/aria_extension_tosca/simple_v1_0/modeling/substitution_mappings.py
index 8f7ec4c..812013c 100644
--- a/extensions/aria_extension_tosca/simple_v1_0/modeling/substitution_mappings.py
+++ b/extensions/aria_extension_tosca/simple_v1_0/modeling/substitution_mappings.py
@@ -21,8 +21,9 @@ def validate_subtitution_mappings_requirement(context, presentation):
     if not validate_format(context, presentation, 'requirement'):
         return
 
-    node_template = get_node_template(context, presentation, 'requirement')
+    node_template = get_node_template(context, presentation)
     if node_template is None:
+        report_missing_node_template(context, presentation, field_name='requirement')
         return
 
     node_type = presentation._container._get_type(context)
@@ -52,53 +53,63 @@ def validate_subtitution_mappings_requirement(context, presentation):
 
     if requirement is None:
         context.validation.report(
-            'substitution mappings requirement "%s" refers to an unknown requirement of node
'
+            'substitution mapping requirement "%s" refers to an unknown requirement of node
'
             'template "%s": %s'
             % (presentation._name, node_template._name, safe_repr(requirement_name)),
             locator=presentation._locator, level=Issue.BETWEEN_TYPES)
         return
 
 
-def validate_subtitution_mappings_capability(context, presentation):
+def validate_substitution_mappings_capability(context, presentation):
     if not validate_format(context, presentation, 'capability'):
         return
 
-    node_template = get_node_template(context, presentation, 'capability')
+    node_template = get_node_template(context, presentation)
     if node_template is None:
+        report_missing_node_template(context, presentation, field_name='capability')
         return
 
-    node_type = presentation._container._get_type(context)
-    if node_type is None:
+    # validate that the capability in substitution_mapping has the same name as the capability
in
+    # the substitution node type.
+    substitution_node_type = presentation._container._get_type(context)
+    if substitution_node_type is None:
         return
-
-    capabilities = node_type._get_capabilities(context)
-    type_capability = capabilities.get(presentation._name)
-    if type_capability is None:
+    substitution_type_capabilities = substitution_node_type._get_capabilities(context)
+    substitution_type_capability = substitution_type_capabilities.get(presentation._name)
+    if substitution_type_capability is None:
         context.validation.report(
-            'substitution mappings capability "%s" is not declared in node type "%s"'
-            % (presentation._name, node_type._name), locator=presentation._locator,
-            level=Issue.BETWEEN_TYPES)
+            'substitution mapping capability "{0}" '
+            'is not declared in node type "{substitution_type}"'.format(
+                presentation._name, substitution_type=substitution_node_type._name),
+            locator=presentation._locator, level=Issue.BETWEEN_TYPES)
         return
 
-    capability_name = presentation._raw[1]
-    capabilities = node_template._get_capabilities(context)
-    capability = capabilities.get(capability_name)
+    # validate that the capability in substitution_mapping is declared in the corresponding
+    # node template
+    mapped_capability_name = presentation._raw[1]
+    node_template_capability = node_template._get_capabilities(context).get(mapped_capability_name)
 
-    if capability is None:
+    if node_template_capability is None:
         context.validation.report(
-            'substitution mappings capability "%s" refers to an unknown capability of node
template'
-            ' "%s": %s'
-            % (presentation._name, node_template._name, safe_repr(capability_name)),
+            'substitution mapping capability "{0}" refers to an unknown '
+            'capability of node template "{1}": {mapped_capability_name}'.format(
+                presentation._name, node_template._name,
+                mapped_capability_name=safe_repr(mapped_capability_name)),
             locator=presentation._locator, level=Issue.BETWEEN_TYPES)
         return
 
-    type_capability_type = type_capability._get_type(context)
-    capability_type = capability._get_type(context)
+    # validate that the capability type in substitution_mapping is derived from the capability
type
+    # in the corresponding node template
+    substitution_type_capability_type = substitution_type_capability._get_type(context)
+    node_template_capability_type = node_template_capability._get_type(context)
 
-    if not type_capability_type._is_descendant(context, capability_type):
+    if not substitution_type_capability_type._is_descendant(context, node_template_capability_type):
         context.validation.report(
-            'type "%s" of substitution mappings capability "%s" is not a descendant of "%s"'
-            % (capability_type._name, presentation._name, type_capability_type._name),
+            'substitution mapping capability "{0}" of type "{1}" is not a descendant '
+            'of "{node_template_capability_type}"'.format(
+                presentation._name,
+                node_template_capability_type._name,
+                node_template_capability_type=substitution_type_capability_type._name),
             locator=presentation._locator, level=Issue.BETWEEN_TYPES)
 
 
@@ -107,9 +118,10 @@ def validate_subtitution_mappings_capability(context, presentation):
 #
 
 def validate_format(context, presentation, name):
-    if (not isinstance(presentation._raw, list)) or (len(presentation._raw) != 2) \
-        or (not isinstance(presentation._raw[0], basestring)) \
-        or (not isinstance(presentation._raw[1], basestring)):
+    if any((not isinstance(presentation._raw, list),
+            len(presentation._raw) != 2,
+            not isinstance(presentation._raw[0], basestring),
+            not isinstance(presentation._raw[1], basestring))):
         context.validation.report(
             'substitution mappings %s "%s" is not a list of 2 strings: %s'
             % (name, presentation._name, safe_repr(presentation._raw)),
@@ -118,13 +130,18 @@ def validate_format(context, presentation, name):
     return True
 
 
-def get_node_template(context, presentation, name):
+def get_node_template(context, presentation):
     node_template_name = presentation._raw[0]
     node_template = context.presentation.get_from_dict('service_template', 'topology_template',
                                                        'node_templates', node_template_name)
-    if node_template is None:
-        context.validation.report(
-            'substitution mappings %s "%s" refers to an unknown node template: %s'
-            % (name, presentation._name, safe_repr(node_template_name)),
-            locator=presentation._locator, level=Issue.FIELD)
     return node_template
+
+
+def report_missing_node_template(context, presentation, field_name):
+    context.validation.report(
+        'substitution mappings {field_name} "{node_template_mapping}" '
+        'refers to an unknown node template: {node_template_name}'.format(
+            field_name=field_name,
+            node_template_mapping=presentation._name,
+            node_template_name=safe_repr(presentation._raw[0])),
+        locator=presentation._locator, level=Issue.FIELD)


Mime
View raw message