package org.openecomp.sdc.toscaparser; import org.openecomp.sdc.toscaparser.elements.EntityType; import org.openecomp.sdc.toscaparser.elements.PropertyDef; import org.openecomp.sdc.toscaparser.elements.StatefulEntityType; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; public class RelationshipTemplate extends EntityTemplate { private static final String DERIVED_FROM = "derived_from"; private static final String PROPERTIES = "properties"; private static final String REQUIREMENTS = "requirements"; private static final String INTERFACES = "interfaces"; private static final String CAPABILITIES = "capabilities"; private static final String TYPE = "type"; @SuppressWarnings("unused") private static final String SECTIONS[] = { DERIVED_FROM, PROPERTIES, REQUIREMENTS, INTERFACES, CAPABILITIES, TYPE}; private String name; private NodeTemplate target; private NodeTemplate source; private ArrayList _properties; public RelationshipTemplate(LinkedHashMap rtrelationshipTemplate, String rtname, LinkedHashMap rtcustomDef, NodeTemplate rttarget, NodeTemplate rtsource) { super(rtname,rtrelationshipTemplate,"relationship_type",rtcustomDef); name = rtname; target = rttarget; source = rtsource; _properties = null; } public ArrayList getPropertiesObjects() { // Return properties objects for this template if(_properties == null) { _properties = _createRelationshipProperties(); } return _properties; } @SuppressWarnings({ "unchecked", "unused" }) public ArrayList _createRelationshipProperties() { ArrayList props = new ArrayList (); LinkedHashMap properties = new LinkedHashMap(); LinkedHashMap relationship = (LinkedHashMap)entityTpl.get("relationship"); if(relationship == null) { for(Object val: entityTpl.values()) { if(val instanceof LinkedHashMap) { relationship = (LinkedHashMap)((LinkedHashMap)val).get("relationship"); break; } } } if(relationship != null) { properties = (LinkedHashMap)((EntityType)typeDefinition).getValue(PROPERTIES,relationship,false); } if(properties == null) { properties = new LinkedHashMap(); } if(properties == null) { properties = (LinkedHashMap)entityTpl.get(PROPERTIES); } if(properties == null) { properties = new LinkedHashMap(); } if(properties != null) { for(Map.Entry me: properties.entrySet()) { String pname = me.getKey(); Object pvalue = me.getValue(); LinkedHashMap propsDef = ((StatefulEntityType)typeDefinition).getPropertiesDef(); if(propsDef != null && propsDef.get(pname) != null) { if(properties.get(pname) != null) { pvalue = properties.get(name); } PropertyDef pd = (PropertyDef)propsDef.get(pname); Property prop = new Property(pname,pvalue,pd.getSchema(),customDef); props.add(prop); } } } ArrayList pds = ((StatefulEntityType)typeDefinition).getPropertiesDefObjects(); for(PropertyDef p: pds) { if(p.getDefault() != null && properties.get(p.getName()) == null) { Property prop = new Property(p.getName(), (LinkedHashMap)p.getDefault(), p.getSchema(), customDef); props.add(prop); } } return props; } public void validate() { _validateProperties(entityTpl,(StatefulEntityType)typeDefinition); } // getters/setters public NodeTemplate getTarget() { return target; } public NodeTemplate getSource() { return source; } public void setSource(NodeTemplate nt) { source = nt; } public void setTarget(NodeTemplate nt) { target = nt; } @Override public String toString() { return "RelationshipTemplate{" + "name='" + name + '\'' + ", target=" + target.getName() + ", source=" + source.getName() + ", _properties=" + _properties + '}'; } } /*python from toscaparser.entity_template import EntityTemplate from toscaparser.properties import Property SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS, INTERFACES, CAPABILITIES, TYPE) = \ ('derived_from', 'properties', 'requirements', 'interfaces', 'capabilities', 'type') log = logging.getLogger('tosca') class RelationshipTemplate(EntityTemplate): '''Relationship template.''' def __init__(self, relationship_template, name, custom_def=None, target=None, source=None): super(RelationshipTemplate, self).__init__(name, relationship_template, 'relationship_type', custom_def) self.name = name.lower() self.target = target self.source = source def get_properties_objects(self): '''Return properties objects for this template.''' if self._properties is None: self._properties = self._create_relationship_properties() return self._properties def _create_relationship_properties(self): props = [] properties = {} relationship = self.entity_tpl.get('relationship') if not relationship: for value in self.entity_tpl.values(): if isinstance(value, dict): relationship = value.get('relationship') break if relationship: properties = self.type_definition.get_value(self.PROPERTIES, relationship) or {} if not properties: properties = self.entity_tpl.get(self.PROPERTIES) or {} if properties: for name, value in properties.items(): props_def = self.type_definition.get_properties_def() if props_def and name in props_def: if name in properties.keys(): value = properties.get(name) prop = Property(name, value, props_def[name].schema, self.custom_def) props.append(prop) for p in self.type_definition.get_properties_def_objects(): if p.default is not None and p.name not in properties.keys(): prop = Property(p.name, p.default, p.schema, self.custom_def) props.append(prop) return props def validate(self): self._validate_properties(self.entity_tpl, self.type_definition)*/