aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/RelationshipTemplate.java
blob: fe44d50da9857e22ef86edd2af34196dcdd18b71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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<Property> _properties;
	
	public RelationshipTemplate(LinkedHashMap<String,Object> rtrelationshipTemplate,
								String rtname,
								LinkedHashMap<String,Object> rtcustomDef,
								NodeTemplate rttarget,
								NodeTemplate rtsource) {
		super(rtname,rtrelationshipTemplate,"relationship_type",rtcustomDef);
		
		name = rtname;
		target = rttarget;
		source = rtsource;
		_properties = null;
	}

	public ArrayList<Property> getPropertiesObjects() {
		// Return properties objects for this template
		if(_properties == null) {
            _properties = _createRelationshipProperties();
		}
        return _properties;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	public ArrayList<Property> _createRelationshipProperties() {
		ArrayList<Property> props = new ArrayList<Property> ();
		LinkedHashMap<String,Object> properties = new LinkedHashMap<String,Object>();
		LinkedHashMap<String,Object> relationship = (LinkedHashMap<String,Object>)entityTpl.get("relationship");
		
		if(relationship == null) {
			for(Object val: entityTpl.values()) {
				if(val instanceof LinkedHashMap) {
					relationship = (LinkedHashMap<String,Object>)((LinkedHashMap<String,Object>)val).get("relationship");
					break;
				}
			}
		}
		
		if(relationship != null) {
            properties = (LinkedHashMap<String,Object>)((EntityType)typeDefinition).getValue(PROPERTIES,relationship,false);
		}
		if(properties == null) {
			properties = new LinkedHashMap<String,Object>();
		}
		if(properties == null) {
			properties = (LinkedHashMap<String,Object>)entityTpl.get(PROPERTIES);
		}
		if(properties == null) {
			properties = new LinkedHashMap<String,Object>();
		}
		
		if(properties != null) {
			for(Map.Entry<String,Object> me: properties.entrySet()) {
				String pname = me.getKey();
				Object pvalue = me.getValue();
				LinkedHashMap<String,PropertyDef> 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<PropertyDef> pds = ((StatefulEntityType)typeDefinition).getPropertiesDefObjects();
		for(PropertyDef p: pds) {
			if(p.getDefault() != null && properties.get(p.getName()) == null) {
                Property prop = new Property(p.getName(), (LinkedHashMap<String,Object>)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)*/