Merge "Separate TOSCA capabilities definition from assignment"
This commit is contained in:
35
translator/toscalib/capabilities.py
Normal file
35
translator/toscalib/capabilities.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# 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 translator.toscalib.properties import Property
|
||||||
|
|
||||||
|
|
||||||
|
class Capability(object):
|
||||||
|
'''TOSCA built-in capabilities type.'''
|
||||||
|
|
||||||
|
def __init__(self, name, properties, definition):
|
||||||
|
self.name = name
|
||||||
|
self._properties = properties
|
||||||
|
self.definition = definition
|
||||||
|
|
||||||
|
@property
|
||||||
|
def properties(self):
|
||||||
|
'''Return a list of property objects.'''
|
||||||
|
properties = []
|
||||||
|
props = self._properties
|
||||||
|
if props:
|
||||||
|
for name, value in props.items():
|
||||||
|
for p in self.definition.properties_def:
|
||||||
|
if p.name == name:
|
||||||
|
properties.append(Property(name, value, p.schema))
|
||||||
|
break
|
||||||
|
return properties
|
||||||
@@ -25,26 +25,34 @@ class CapabilityTypeDef(StatefulEntityType):
|
|||||||
self.defs = {}
|
self.defs = {}
|
||||||
if ntype:
|
if ntype:
|
||||||
self.defs = self.TOSCA_DEF[ctype]
|
self.defs = self.TOSCA_DEF[ctype]
|
||||||
|
self.parent_capabilities = self._get_parent_capabilities()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def properties_def(self):
|
def properties_def(self):
|
||||||
'''Return a list of property objects.'''
|
'''Return a list of property definition objects.'''
|
||||||
properties = []
|
properties = []
|
||||||
props = self.entity_value(self.defs, 'properties')
|
parent_properties = {}
|
||||||
if props:
|
if self.parent_capabilities:
|
||||||
if isinstance(props, dict):
|
for type, value in self.parent_capabilities.items():
|
||||||
for prop, schema in props.items():
|
parent_properties[type] = value.get('properties')
|
||||||
prop_val = None
|
|
||||||
for k, v in schema.items():
|
|
||||||
if k == 'default':
|
|
||||||
prop_val = v
|
|
||||||
properties.append(PropertyDef(prop,
|
|
||||||
prop_val, schema))
|
|
||||||
if self.properties:
|
if self.properties:
|
||||||
for prop, value in self.properties.items():
|
for prop, schema in self.properties.items():
|
||||||
properties.append(PropertyDef(prop, value, None))
|
properties.append(PropertyDef(prop, None, schema))
|
||||||
|
if parent_properties:
|
||||||
|
for parent, props in parent_properties.items():
|
||||||
|
for prop, schema in props.items():
|
||||||
|
properties.append(PropertyDef(prop, None, schema))
|
||||||
return properties
|
return properties
|
||||||
|
|
||||||
|
def _get_parent_capabilities(self):
|
||||||
|
capabilities = {}
|
||||||
|
parent_cap = self.parent_type
|
||||||
|
if parent_cap:
|
||||||
|
while parent_cap != 'tosca.capabilities.Root':
|
||||||
|
capabilities[parent_cap] = self.TOSCA_DEF[parent_cap]
|
||||||
|
parent_cap = capabilities[parent_cap]['derived_from']
|
||||||
|
return capabilities
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent_type(self):
|
def parent_type(self):
|
||||||
'''Return a capability this capability is derived from.'''
|
'''Return a capability this capability is derived from.'''
|
||||||
|
|||||||
@@ -10,10 +10,9 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from translator.toscalib.capabilities import Capability
|
||||||
from translator.toscalib.common.exception import MissingRequiredFieldError
|
from translator.toscalib.common.exception import MissingRequiredFieldError
|
||||||
from translator.toscalib.common.exception import UnknownFieldError
|
from translator.toscalib.common.exception import UnknownFieldError
|
||||||
from translator.toscalib.elements.capabilitytype import CapabilityTypeDef
|
|
||||||
from translator.toscalib.elements.interfaces import InterfacesDef
|
from translator.toscalib.elements.interfaces import InterfacesDef
|
||||||
from translator.toscalib.elements.nodetype import NodeType
|
from translator.toscalib.elements.nodetype import NodeType
|
||||||
from translator.toscalib.elements.relationshiptype import RelationshipType
|
from translator.toscalib.elements.relationshiptype import RelationshipType
|
||||||
@@ -76,19 +75,13 @@ class EntityTemplate(object):
|
|||||||
|
|
||||||
def _create_capabilities(self):
|
def _create_capabilities(self):
|
||||||
capability = []
|
capability = []
|
||||||
properties = {}
|
|
||||||
cap_type = None
|
|
||||||
caps = self.type_definition.get_value(self.CAPABILITIES,
|
caps = self.type_definition.get_value(self.CAPABILITIES,
|
||||||
self.entity_tpl)
|
self.entity_tpl)
|
||||||
if caps:
|
if caps:
|
||||||
for name, value in caps.items():
|
for name, props in caps.items():
|
||||||
for val in value.values():
|
|
||||||
properties = val
|
|
||||||
for c in self.type_definition.capabilities:
|
for c in self.type_definition.capabilities:
|
||||||
if c.name == name:
|
if c.name == name:
|
||||||
cap_type = c.type
|
cap = Capability(name, props['properties'], c)
|
||||||
cap = CapabilityTypeDef(name, cap_type,
|
|
||||||
self.name, properties)
|
|
||||||
capability.append(cap)
|
capability.append(cap)
|
||||||
return capability
|
return capability
|
||||||
|
|
||||||
|
|||||||
@@ -258,7 +258,12 @@ class GetProperty(Function):
|
|||||||
"""Gets a node template capability property."""
|
"""Gets a node template capability property."""
|
||||||
for cap in node_template.capabilities:
|
for cap in node_template.capabilities:
|
||||||
if cap.name == capability_name:
|
if cap.name == capability_name:
|
||||||
if property_name not in cap.properties:
|
property = None
|
||||||
|
for p in cap.properties:
|
||||||
|
if property_name == p.name:
|
||||||
|
property = p.value
|
||||||
|
break
|
||||||
|
if not property:
|
||||||
raise KeyError(_(
|
raise KeyError(_(
|
||||||
"Property '{0}' not found in capability '{1}' of node"
|
"Property '{0}' not found in capability '{1}' of node"
|
||||||
" template '{2}' referenced from node template"
|
" template '{2}' referenced from node template"
|
||||||
@@ -266,7 +271,7 @@ class GetProperty(Function):
|
|||||||
capability_name,
|
capability_name,
|
||||||
node_template.name,
|
node_template.name,
|
||||||
self.context.name))
|
self.context.name))
|
||||||
return cap.properties[property_name]
|
return property
|
||||||
msg = _("Requirement/Capability '{0}' referenced from '{1}' node "
|
msg = _("Requirement/Capability '{0}' referenced from '{1}' node "
|
||||||
"template not found in '{2}' node template.").format(
|
"template not found in '{2}' node template.").format(
|
||||||
capability_name,
|
capability_name,
|
||||||
|
|||||||
Reference in New Issue
Block a user