[1/3] incubator-ariatosca git commit: ARIA-174 Refactor instantiation phase [Forced Update!]
Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-174-Refactor-instantiation-phase 4ad587422 -> df2b916e6 (forced update) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/df2b916e/aria/orchestrator/topology/topology.py -- diff --git a/aria/orchestrator/topology/topology.py b/aria/orchestrator/topology/topology.py new file mode 100644 index 000..8ee33d1 --- /dev/null +++ b/aria/orchestrator/topology/topology.py @@ -0,0 +1,223 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +from ...parser.validation import issue +from ...modeling import models +from ...utils import console +from . import ( +template_handler, +instance_handler, +common +) + + +class Topology(issue.ReporterMixin): + +_init_map = { +models.ServiceTemplate: models.Service, +models.ArtifactTemplate: models.Artifact, +models.CapabilityTemplate: models.Capability, +models.GroupTemplate: models.Group, +models.InterfaceTemplate: models.Interface, +models.NodeTemplate: models.Node, +models.PolicyTemplate: models.Policy, +models.SubstitutionTemplate: models.Substitution, +models.RelationshipTemplate: models.Relationship, +models.OperationTemplate: models.Operation, +models.SubstitutionTemplateMapping: models.SubstitutionMapping, + +# Common +models.Metadata: models.Metadata, +models.Attribute: models.Attribute, +models.Property: models.Property, +models.Input: models.Input, +models.Output: models.Output, +models.Configuration: models.Configuration, +models.Argument: models.Argument, +models.Type: models.Type +} + +def __init__(self, *args, **kwargs): +super(Topology, self).__init__(*args, **kwargs) +self._model_cls_to_handler = dict(self._init_handlers(instance_handler), + **self._init_handlers(template_handler)) + +@staticmethod +def _init_handlers(module_): +""" +Register handlers from a handler module to the models + +:param module_: The module to look for handlers +:return: a dict where the key is the models class, and the value is the handler class +associated with it from the provided module +""" +handlers = {} +for attribute_name in dir(module_): +if attribute_name.startswith('_'): +continue +attribute = getattr(module_, attribute_name) +if isinstance(attribute, type) and issubclass(attribute, common.HandlerBase): +handlers[getattr(models, attribute_name)] = attribute +return handlers + +def instantiate(self, model, **kwargs): +""" +instantiate the provided model + +:param model: +:param kwargs: +:return: +""" +if isinstance(model, dict): +return dict((name, self.instantiate(value, **kwargs)) +for name, value in model.iteritems()) +elif isinstance(model, list): +return list(self.instantiate(value, **kwargs) for value in model) +elif model is not None: +_handler = self._model_cls_to_handler[model.__class__] +model_instance_cls = self._init_map[model.__class__] +return _handler(self, model).instantiate(model_instance_cls, **kwargs) + +def validate(self, model, **kwargs): +if isinstance(model, dict): +return self.validate(model.values(), **kwargs) +elif isinstance(model, list): +return all(self.validate(value, **kwargs) for value in model) +elif model is not None: +_handler = self._model_cls_to_handler[model.__class__] +return _handler(self, model).validate(**kwargs) + +def dump(self, model, out_stream=None, title=None, **kwargs): +out_stream = out_stream or console.TopologyStylizer() + +# if model is empty, no need to print out the section name +if model and title: +out_stream.write('{0}:'.format(title)) + +if isinstance(model, dict): +
[1/3] incubator-ariatosca git commit: ARIA-174 Refactor instantiation phase [Forced Update!]
Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-174-Refactor-instantiation-phase b7f7ff7a8 -> 4ad587422 (forced update) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/4ad58742/aria/orchestrator/topology/topology.py -- diff --git a/aria/orchestrator/topology/topology.py b/aria/orchestrator/topology/topology.py new file mode 100644 index 000..8ee33d1 --- /dev/null +++ b/aria/orchestrator/topology/topology.py @@ -0,0 +1,223 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +from ...parser.validation import issue +from ...modeling import models +from ...utils import console +from . import ( +template_handler, +instance_handler, +common +) + + +class Topology(issue.ReporterMixin): + +_init_map = { +models.ServiceTemplate: models.Service, +models.ArtifactTemplate: models.Artifact, +models.CapabilityTemplate: models.Capability, +models.GroupTemplate: models.Group, +models.InterfaceTemplate: models.Interface, +models.NodeTemplate: models.Node, +models.PolicyTemplate: models.Policy, +models.SubstitutionTemplate: models.Substitution, +models.RelationshipTemplate: models.Relationship, +models.OperationTemplate: models.Operation, +models.SubstitutionTemplateMapping: models.SubstitutionMapping, + +# Common +models.Metadata: models.Metadata, +models.Attribute: models.Attribute, +models.Property: models.Property, +models.Input: models.Input, +models.Output: models.Output, +models.Configuration: models.Configuration, +models.Argument: models.Argument, +models.Type: models.Type +} + +def __init__(self, *args, **kwargs): +super(Topology, self).__init__(*args, **kwargs) +self._model_cls_to_handler = dict(self._init_handlers(instance_handler), + **self._init_handlers(template_handler)) + +@staticmethod +def _init_handlers(module_): +""" +Register handlers from a handler module to the models + +:param module_: The module to look for handlers +:return: a dict where the key is the models class, and the value is the handler class +associated with it from the provided module +""" +handlers = {} +for attribute_name in dir(module_): +if attribute_name.startswith('_'): +continue +attribute = getattr(module_, attribute_name) +if isinstance(attribute, type) and issubclass(attribute, common.HandlerBase): +handlers[getattr(models, attribute_name)] = attribute +return handlers + +def instantiate(self, model, **kwargs): +""" +instantiate the provided model + +:param model: +:param kwargs: +:return: +""" +if isinstance(model, dict): +return dict((name, self.instantiate(value, **kwargs)) +for name, value in model.iteritems()) +elif isinstance(model, list): +return list(self.instantiate(value, **kwargs) for value in model) +elif model is not None: +_handler = self._model_cls_to_handler[model.__class__] +model_instance_cls = self._init_map[model.__class__] +return _handler(self, model).instantiate(model_instance_cls, **kwargs) + +def validate(self, model, **kwargs): +if isinstance(model, dict): +return self.validate(model.values(), **kwargs) +elif isinstance(model, list): +return all(self.validate(value, **kwargs) for value in model) +elif model is not None: +_handler = self._model_cls_to_handler[model.__class__] +return _handler(self, model).validate(**kwargs) + +def dump(self, model, out_stream=None, title=None, **kwargs): +out_stream = out_stream or console.TopologyStylizer() + +# if model is empty, no need to print out the section name +if model and title: +out_stream.write('{0}:'.format(title)) + +if isinstance(model, dict): +