diff --git a/toscaparser/elements/TOSCA_definition_1_0.yaml b/toscaparser/elements/TOSCA_definition_1_0.yaml index 3e488ca..3a940a0 100644 --- a/toscaparser/elements/TOSCA_definition_1_0.yaml +++ b/toscaparser/elements/TOSCA_definition_1_0.yaml @@ -718,3 +718,32 @@ tosca.artifacts.Deployment.Image.VM.QCOW2: description: Virtual Machine (VM) image in QCOW v2 standard disk format mime_type: application/octet-stream file_ext: [ qcow2 ] + +########################################################################## + # Policy Type. + # TOSCA Policy Types represent logical grouping of TOSCA nodes that have + # an implied relationship and need to be orchestrated or managed together + # to achieve some result. +########################################################################## +tosca.policies.Root: + description: The TOSCA Policy Type all other TOSCA Policy Types derive from. + +tosca.policies.Placement: + derived_from: tosca.policies.Root + description: The TOSCA Policy Type definition that is used to govern + placement of TOSCA nodes or groups of nodes. + +tosca.policies.Scaling: + derived_from: tosca.policies.Root + description: The TOSCA Policy Type definition that is used to govern + scaling of TOSCA nodes or groups of nodes. + +tosca.policies.Update: + derived_from: tosca.policies.Root + description: The TOSCA Policy Type definition that is used to govern + update of TOSCA nodes or groups of nodes. + +tosca.policies.Performance: + derived_from: tosca.policies.Root + description: The TOSCA Policy Type definition that is used to declare + performance requirements for TOSCA nodes or groups of nodes. diff --git a/toscaparser/elements/entity_type.py b/toscaparser/elements/entity_type.py index d4fada3..b719298 100644 --- a/toscaparser/elements/entity_type.py +++ b/toscaparser/elements/entity_type.py @@ -48,6 +48,7 @@ class EntityType(object): CAPABILITY_PREFIX = 'tosca.capabilities.' INTERFACE_PREFIX = 'tosca.interfaces.' ARTIFACT_PREFIX = 'tosca.artifacts.' + POLICY_PREFIX = 'tosca.policies.' # currently the data types are defined only for network # but may have changes in the future. DATATYPE_PREFIX = 'tosca.datatypes.network.' diff --git a/toscaparser/elements/policytype.py b/toscaparser/elements/policytype.py new file mode 100644 index 0000000..aa53c2d --- /dev/null +++ b/toscaparser/elements/policytype.py @@ -0,0 +1,45 @@ +# 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. + +from toscaparser.elements.statefulentitytype import StatefulEntityType + + +class PolicyType(StatefulEntityType): + '''TOSCA built-in policies type.''' + + def __init__(self, ptype, custom_def=None): + super(PolicyType, self).__init__(ptype, self.POLICY_PREFIX, + custom_def) + self.type = ptype + self.properties = None + if self.PROPERTIES in self.defs: + self.properties = self.defs[self.PROPERTIES] + self.parent_policies = self._get_parent_policies() + + def _get_parent_policies(self): + policies = {} + parent_policy = self.parent_type + if parent_policy: + while parent_policy != 'tosca.policies.Root': + policies[parent_policy] = self.TOSCA_DEF[parent_policy] + parent_policy = policies[parent_policy]['derived_from'] + return policies + + @property + def parent_type(self): + '''Return a policy this policy is derived from.''' + return self.derived_from(self.defs) + + def get_policy(self, name): + '''Return the definition of a policy field by name.''' + if name in self.defs: + return self.defs[name] diff --git a/toscaparser/tests/test_toscadef.py b/toscaparser/tests/test_toscadef.py index b4f980a..5f86714 100644 --- a/toscaparser/tests/test_toscadef.py +++ b/toscaparser/tests/test_toscadef.py @@ -14,6 +14,7 @@ from toscaparser.common import exception from toscaparser.elements.artifacttype import ArtifactTypeDef import toscaparser.elements.interfaces as ifaces from toscaparser.elements.nodetype import NodeType +from toscaparser.elements.policytype import PolicyType from toscaparser.tests.base import TestCase compute_type = NodeType('tosca.nodes.Compute') @@ -33,6 +34,11 @@ artif_vm_iso_type = ArtifactTypeDef('tosca.artifacts.' 'Deployment.Image.VM.ISO') artif_vm_qcow2_type = ArtifactTypeDef('tosca.artifacts.' 'Deployment.Image.VM.QCOW2') +policy_root_type = PolicyType('tosca.policies.Root') +policy_placement_type = PolicyType('tosca.policies.Placement') +policy_scaling_type = PolicyType('tosca.policies.Scaling') +policy_update_type = PolicyType('tosca.policies.Update') +policy_performance_type = PolicyType('tosca.policies.Performance') class ToscaDefTest(TestCase): @@ -258,3 +264,52 @@ class ToscaDefTest(TestCase): get_artifact(name) for name in artif_vm_qcow2_type.defs], key=lambda x: str(x))) + + def test_policies(self): + self.assertEqual('tosca.policies.Root', + policy_placement_type.parent_type) + self.assertEqual({}, policy_placement_type.parent_policies) + self.assertEqual(sorted(['tosca.policies.Root', + 'The TOSCA Policy Type definition that is ' + 'used to govern placement of TOSCA nodes or ' + 'groups of nodes.'], + key=lambda x: str(x)), + sorted([policy_placement_type.get_policy(name) + for name in policy_placement_type.defs], + key=lambda x: str(x))) + + self.assertEqual('tosca.policies.Root', + policy_scaling_type.parent_type) + self.assertEqual({}, policy_scaling_type.parent_policies) + self.assertEqual(sorted(['tosca.policies.Root', + 'The TOSCA Policy Type definition that is ' + 'used to govern scaling of TOSCA nodes or ' + 'groups of nodes.'], + key=lambda x: str(x)), + sorted([policy_scaling_type.get_policy(name) + for name in policy_scaling_type.defs], + key=lambda x: str(x))) + + self.assertEqual('tosca.policies.Root', + policy_update_type.parent_type) + self.assertEqual({}, policy_update_type.parent_policies) + self.assertEqual(sorted(['tosca.policies.Root', + 'The TOSCA Policy Type definition that is ' + 'used to govern update of TOSCA nodes or ' + 'groups of nodes.'], + key=lambda x: str(x)), + sorted([policy_update_type.get_policy(name) + for name in policy_update_type.defs], + key=lambda x: str(x))) + + self.assertEqual('tosca.policies.Root', + policy_performance_type.parent_type) + self.assertEqual({}, policy_performance_type.parent_policies) + self.assertEqual(sorted(['tosca.policies.Root', + 'The TOSCA Policy Type definition that is ' + 'used to declare performance requirements ' + 'for TOSCA nodes or groups of nodes.'], + key=lambda x: str(x)), + sorted([policy_performance_type.get_policy(name) + for name in policy_performance_type.defs], + key=lambda x: str(x)))