def _create_interfaces(self): interfaces = [] type_interfaces = None if isinstance(self.type_definition, RelationshipType): if isinstance(self.entity_tpl, dict): for rel_def, value in self.entity_tpl.items(): if rel_def != 'type': rel_def = self.entity_tpl.get(rel_def) rel = None if isinstance(rel_def, dict): rel = rel_def.get('relationship') if rel: if self.INTERFACES in rel: type_interfaces = rel[self.INTERFACES] break else: type_interfaces = self.type_definition.get_value( self.INTERFACES, self.entity_tpl) if type_interfaces: for interface_type, value in type_interfaces.items(): for op, op_def in value.items(): iface = InterfacesDef(self.type_definition, interfacetype=interface_type, node_template=self, name=op, value=op_def) interfaces.append(iface) return interfaces
def lifecycle_operations(self): '''Return available life cycle operations if found.''' ops = None interfaces = self.interfaces if interfaces: i = InterfacesDef(self.type, ifaces.LIFECYCLE) ops = i.lifecycle_ops return ops
def _get_interface_operations_from_type(node_type, node, lifecycle_name): operations = {} if node_type.interfaces and lifecycle_name in node_type.interfaces: for name, elems in node_type.interfaces[lifecycle_name].items(): # ignore empty operations (only type) # ignore global interface inputs, # concrete inputs are on the operations themselves if name != 'type' and name != 'inputs': operations[name] = InterfacesDef(node_type, lifecycle_name, node, name, elems) return operations
def _get_interface_operations_from_type(node_type, node, lifecycle_name): operations = {} if isinstance(node_type, str) or \ node_type.is_derived_from("tosca.policies.Root"): # node_type.type == "tosca.policies.Placement" or \ # node_type.type == "tosca.policies.Placement.Colocate" or \ # node_type.type == "tosca.policies.Placement.Antilocate": return operations if node_type.interfaces and lifecycle_name in node_type.interfaces: for name, elems in node_type.interfaces[lifecycle_name].items(): # ignore empty operations (only type) # ignore global interface inputs, # concrete inputs are on the operations themselves if name != 'type' and name != 'inputs': operations[name] = InterfacesDef(node_type, lifecycle_name, node, name, elems) return operations
def _create_operations(self, interfacesDefs): interfaces = [] defaults = interfacesDefs.pop('defaults', {}) for interface_type, value in interfacesDefs.items(): # merge in shared: # shared inputs inputs = value.get('inputs') defaultInputs = defaults.get('inputs') if inputs and defaultInputs: # merge shared inputs inputs = dict(defaultInputs, **inputs) else: inputs = inputs or defaultInputs # shared outputs outputs = value.get('outputs') defaultOutputs = defaults.get('outputs') if outputs and defaultOutputs: # merge shared inputs outputs = dict(defaultOutputs, **outputs) else: outputs = outputs or defaultOutputs # shared implementation implementation = value.get('implementation') or defaults.get( 'implementation') # create an InterfacesDef for each operation _source = value.pop('_source', None) if 'operations' in value: defs = value.get('operations') or {} else: defs = value for op in list(defs): op_def = defs[op] if op in INTERFACE_DEF_RESERVED_WORDS: continue if not isinstance(op_def, dict): op_def = dict(implementation=op_def or implementation) elif implementation and not op_def.get('implementation'): op_def['implementation'] = implementation if _source: op_def['_source'] = _source iface = InterfacesDef( self.type_definition, interface_type, node_template=self, name=op, value=op_def, inputs=inputs.copy() if inputs else None, outputs=outputs.copy() if outputs else None) interfaces.append(iface) # add a "default" operation that has the shared inputs and implementation if inputs or implementation: iface = InterfacesDef(self.type_definition, interface_type, node_template=self, name='default', value=dict(implementation=implementation, _source=_source), inputs=inputs, outputs=outputs) interfaces.append(iface) return interfaces