Exemple #1
0
    def test_type_hierarchy(self):
        super_type = Type(variant='variant', name='super')
        sub_type = Type(variant='variant', parent=super_type, name='sub')
        additional_type = Type(variant='variant', name='non_related')

        assert super_type.hierarchy == [super_type]
        assert sub_type.hierarchy == [sub_type, super_type]
        assert additional_type.hierarchy == [additional_type]

        super_type.parent = additional_type

        assert super_type.hierarchy == [super_type, additional_type]
        assert sub_type.hierarchy == [sub_type, super_type, additional_type]
def create_types(context, root, types):
    if types is None:
        return

    def added_all():
        for name in types:
            if root.get_descendant(name) is None:
                return False
        return True

    while not added_all():
        for name, the_type in types.iteritems():
            if root.get_descendant(name) is None:
                parent_type = the_type._get_parent(context)
                model = Type(name=the_type._name,
                             role=the_type._get_extension('role'))
                if the_type.description:
                    model.description = the_type.description.value
                if parent_type is None:
                    model.parent = root
                    model.variant = root.variant
                    root.children.append(model)
                else:
                    container = root.get_descendant(parent_type._name)
                    if container is not None:
                        model.parent = container
                        model.variant = container.variant
                        container.children.append(model)
def create_service_template_model(context):  # pylint: disable=too-many-locals,too-many-branches
    model = ServiceTemplate(created_at=datetime.now(),
                            main_file_name=os.path.basename(
                                str(context.presentation.location)))

    model.description = context.presentation.get('service_template',
                                                 'description', 'value')

    # Metadata
    metadata = context.presentation.get('service_template', 'metadata')
    if metadata is not None:
        create_metadata_models(context, model, metadata)

    # Types
    model.node_types = Type(variant='node')
    create_types(context, model.node_types,
                 context.presentation.get('service_template', 'node_types'))
    model.group_types = Type(variant='group')
    create_types(context, model.group_types,
                 context.presentation.get('service_template', 'group_types'))
    model.policy_types = Type(variant='policy')
    create_types(context, model.policy_types,
                 context.presentation.get('service_template', 'policy_types'))
    model.relationship_types = Type(variant='relationship')
    create_types(
        context, model.relationship_types,
        context.presentation.get('service_template', 'relationship_types'))
    model.capability_types = Type(variant='capability')
    create_types(
        context, model.capability_types,
        context.presentation.get('service_template', 'capability_types'))
    model.interface_types = Type(variant='interface')
    create_types(
        context, model.interface_types,
        context.presentation.get('service_template', 'interface_types'))
    model.artifact_types = Type(variant='artifact')
    create_types(
        context, model.artifact_types,
        context.presentation.get('service_template', 'artifact_types'))

    # Topology template
    topology_template = context.presentation.get('service_template',
                                                 'topology_template')
    if topology_template is not None:
        create_parameter_models_from_values(
            model.inputs,
            topology_template._get_input_values(context),
            model_cls=Input)
        create_parameter_models_from_values(
            model.outputs,
            topology_template._get_output_values(context),
            model_cls=Output)

    # Plugin specifications
    policies = context.presentation.get('service_template',
                                        'topology_template', 'policies')
    if policies:
        for policy in policies.itervalues():
            role = model.policy_types.get_descendant(policy.type).role
            if role == 'plugin':
                plugin_specification = create_plugin_specification_model(
                    context, policy)
                model.plugin_specifications[
                    plugin_specification.name] = plugin_specification
            elif role == 'workflow':
                operation_template = create_workflow_operation_template_model(
                    context, model, policy)
                model.workflow_templates[
                    operation_template.name] = operation_template

    # Node templates
    node_templates = context.presentation.get('service_template',
                                              'topology_template',
                                              'node_templates')
    if node_templates:
        for node_template in node_templates.itervalues():
            node_template_model = create_node_template_model(
                context, model, node_template)
            model.node_templates[
                node_template_model.name] = node_template_model
        for node_template in node_templates.itervalues():
            fix_node_template_model(context, model, node_template)

    # Group templates
    groups = context.presentation.get('service_template', 'topology_template',
                                      'groups')
    if groups:
        for group in groups.itervalues():
            group_template_model = create_group_template_model(
                context, model, group)
            model.group_templates[
                group_template_model.name] = group_template_model

    # Policy templates
    policies = context.presentation.get('service_template',
                                        'topology_template', 'policies')
    if policies:
        for policy in policies.itervalues():
            policy_template_model = create_policy_template_model(
                context, model, policy)
            model.policy_templates[
                policy_template_model.name] = policy_template_model

    # Substitution template
    substitution_mappings = context.presentation.get('service_template',
                                                     'topology_template',
                                                     'substitution_mappings')
    if substitution_mappings:
        model.substitution_template = create_substitution_template_model(
            context, model, substitution_mappings)

    return model