# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
from toscaparser.common import exception
from toscaparser.dataentity import DataEntity
from toscaparser import functions
from toscaparser.groups import Group
from toscaparser.nodetemplate import NodeTemplate
from toscaparser.parameters import Input
from toscaparser.parameters import Output
from toscaparser.policy import Policy
from toscaparser.relationship_template import RelationshipTemplate
from toscaparser.substitution_mappings import SubstitutionMappings
from toscaparser.tpl_relationship_graph import ToscaGraph
from toscaparser.utils.gettextutils import _
# Topology template key names
SECTIONS = (DESCRIPTION, INPUTS, NODE_TEMPLATES,
RELATIONSHIP_TEMPLATES, OUTPUTS, GROUPS,
SUBSTITUION_MAPPINGS, POLICIES) = \
('description', 'inputs', 'node_templates',
'relationship_templates', 'outputs', 'groups',
'substitution_mappings', 'policies')
log = logging.getLogger("tosca.model")
class TopologyTemplate(object):
'''Load the template data.'''
def __init__(self, template, custom_defs,
rel_types=None, parsed_params=None,
sub_mapped_node_template=None):
self.tpl = template
self.sub_mapped_node_template = sub_mapped_node_template
if self.tpl:
self.custom_defs = custom_defs
self.rel_types = rel_types
self.parsed_params = parsed_params
self._validate_field()
self.description = self._tpl_description()
self.inputs = self._inputs()
self.relationship_templates = self._relationship_templates()
self.nodetemplates = self._nodetemplates()
self.outputs = self._outputs()
if hasattr(self, 'nodetemplates'):
self.graph = ToscaGraph(self.nodetemplates)
self.groups = self._groups()
self.policies = self._policies()
self._process_intrinsic_functions()
self.substitution_mappings = self._substitution_mappings()
def _inputs(self):
inputs = []
for name, attrs in self._tpl_inputs().items():
input = Input(name, attrs)
if self.parsed_params and name in self.parsed_params:
input.validate(self.parsed_params[name])
else:
default = input.default
if default:
input.validate(default)
if (self.parsed_params and input.name not in self.parsed_params
or self.parsed_params is None) and input.required \
and input.default is None:
exception.ExceptionCollector.appendException(
exception.MissingRequiredParameterError(
what='Template',
input_name=input.name))
inputs.append(input)