2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api;
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
27 import org.onap.sdc.toscaparser.api.elements.PropertyDef;
28 import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
29 import org.onap.sdc.toscaparser.api.elements.EntityType;
31 public class RelationshipTemplate extends EntityTemplate {
33 private static final String DERIVED_FROM = "derived_from";
34 private static final String PROPERTIES = "properties";
35 private static final String REQUIREMENTS = "requirements";
36 private static final String INTERFACES = "interfaces";
37 private static final String CAPABILITIES = "capabilities";
38 private static final String TYPE = "type";
39 @SuppressWarnings("unused")
40 private static final String SECTIONS[] = {
41 DERIVED_FROM, PROPERTIES, REQUIREMENTS, INTERFACES, CAPABILITIES, TYPE};
44 private NodeTemplate target;
45 private NodeTemplate source;
46 private ArrayList<Property> _properties;
48 public RelationshipTemplate(LinkedHashMap<String, Object> rtrelationshipTemplate,
50 LinkedHashMap<String, Object> rtcustomDef,
51 NodeTemplate rttarget,
52 NodeTemplate rtsource) {
53 this(rtrelationshipTemplate, rtname, rtcustomDef, rttarget, rtsource, null);
56 public RelationshipTemplate(LinkedHashMap<String, Object> rtrelationshipTemplate,
58 LinkedHashMap<String, Object> rtcustomDef,
59 NodeTemplate rttarget,
60 NodeTemplate rtsource, NodeTemplate parentNodeTemplate) {
61 super(rtname, rtrelationshipTemplate, "relationship_type", rtcustomDef, parentNodeTemplate);
69 public ArrayList<Property> getPropertiesObjects() {
70 // Return properties objects for this template
71 if (_properties == null) {
72 _properties = _createRelationshipProperties();
77 @SuppressWarnings({"unchecked", "unused"})
78 public ArrayList<Property> _createRelationshipProperties() {
79 ArrayList<Property> props = new ArrayList<Property>();
80 LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>();
81 LinkedHashMap<String, Object> relationship = (LinkedHashMap<String, Object>) entityTpl.get("relationship");
83 if (relationship == null) {
84 for (Object val : entityTpl.values()) {
85 if (val instanceof LinkedHashMap) {
86 relationship = (LinkedHashMap<String, Object>) ((LinkedHashMap<String, Object>) val).get("relationship");
92 if (relationship != null) {
93 properties = (LinkedHashMap<String, Object>) ((EntityType) typeDefinition).getValue(PROPERTIES, relationship, false);
95 if (properties == null) {
96 properties = new LinkedHashMap<String, Object>();
98 if (properties == null) {
99 properties = (LinkedHashMap<String, Object>) entityTpl.get(PROPERTIES);
101 if (properties == null) {
102 properties = new LinkedHashMap<String, Object>();
105 if (properties != null) {
106 for (Map.Entry<String, Object> me : properties.entrySet()) {
107 String pname = me.getKey();
108 Object pvalue = me.getValue();
109 LinkedHashMap<String, PropertyDef> propsDef = ((StatefulEntityType) typeDefinition).getPropertiesDef();
110 if (propsDef != null && propsDef.get(pname) != null) {
111 if (properties.get(pname) != null) {
112 pvalue = properties.get(name);
114 PropertyDef pd = (PropertyDef) propsDef.get(pname);
115 Property prop = new Property(pname, pvalue, pd.getSchema(), customDef);
120 ArrayList<PropertyDef> pds = ((StatefulEntityType) typeDefinition).getPropertiesDefObjects();
121 for (PropertyDef p : pds) {
122 if (p.getDefault() != null && properties.get(p.getName()) == null) {
123 Property prop = new Property(p.getName(), (LinkedHashMap<String, Object>) p.getDefault(), p.getSchema(), customDef);
130 public void validate() {
131 _validateProperties(entityTpl, (StatefulEntityType) typeDefinition);
135 public NodeTemplate getTarget() {
139 public NodeTemplate getSource() {
143 public void setSource(NodeTemplate nt) {
147 public void setTarget(NodeTemplate nt) {
152 public String toString() {
153 return "RelationshipTemplate{" +
154 "name='" + name + '\'' +
155 ", target=" + target.getName() +
156 ", source=" + source.getName() +
157 ", _properties=" + _properties +
165 from toscaparser.entity_template import EntityTemplate
166 from toscaparser.properties import Property
168 SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS,
169 INTERFACES, CAPABILITIES, TYPE) = \
170 ('derived_from', 'properties', 'requirements', 'interfaces',
171 'capabilities', 'type')
173 log = logging.getLogger('tosca')
176 class RelationshipTemplate(EntityTemplate):
177 '''Relationship template.'''
178 def __init__(self, relationship_template, name, custom_def=None,
179 target=None, source=None):
180 super(RelationshipTemplate, self).__init__(name,
181 relationship_template,
184 self.name = name.lower()
188 def get_properties_objects(self):
189 '''Return properties objects for this template.'''
190 if self._properties is None:
191 self._properties = self._create_relationship_properties()
192 return self._properties
194 def _create_relationship_properties(self):
197 relationship = self.entity_tpl.get('relationship')
200 for value in self.entity_tpl.values():
201 if isinstance(value, dict):
202 relationship = value.get('relationship')
206 properties = self.type_definition.get_value(self.PROPERTIES,
209 properties = self.entity_tpl.get(self.PROPERTIES) or {}
212 for name, value in properties.items():
213 props_def = self.type_definition.get_properties_def()
214 if props_def and name in props_def:
215 if name in properties.keys():
216 value = properties.get(name)
217 prop = Property(name, value,
218 props_def[name].schema, self.custom_def)
220 for p in self.type_definition.get_properties_def_objects():
221 if p.default is not None and p.name not in properties.keys():
222 prop = Property(p.name, p.default, p.schema, self.custom_def)
227 self._validate_properties(self.entity_tpl, self.type_definition)*/