Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / RelationshipTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api;
22
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26
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;
30
31 public class RelationshipTemplate extends EntityTemplate {
32
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};
42
43     private String name;
44     private NodeTemplate target;
45     private NodeTemplate source;
46     private ArrayList<Property> _properties;
47
48     public RelationshipTemplate(LinkedHashMap<String, Object> rtrelationshipTemplate,
49                                 String rtname,
50                                 LinkedHashMap<String, Object> rtcustomDef,
51                                 NodeTemplate rttarget,
52                                 NodeTemplate rtsource) {
53         this(rtrelationshipTemplate, rtname, rtcustomDef, rttarget, rtsource, null);
54     }
55
56     public RelationshipTemplate(LinkedHashMap<String, Object> rtrelationshipTemplate,
57                                 String rtname,
58                                 LinkedHashMap<String, Object> rtcustomDef,
59                                 NodeTemplate rttarget,
60                                 NodeTemplate rtsource, NodeTemplate parentNodeTemplate) {
61         super(rtname, rtrelationshipTemplate, "relationship_type", rtcustomDef, parentNodeTemplate);
62
63         name = rtname;
64         target = rttarget;
65         source = rtsource;
66         _properties = null;
67     }
68
69     public ArrayList<Property> getPropertiesObjects() {
70         // Return properties objects for this template
71         if (_properties == null) {
72             _properties = _createRelationshipProperties();
73         }
74         return _properties;
75     }
76
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");
82
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");
87                     break;
88                 }
89             }
90         }
91
92         if (relationship != null) {
93             properties = (LinkedHashMap<String, Object>) ((EntityType) typeDefinition).getValue(PROPERTIES, relationship, false);
94         }
95         if (properties == null) {
96             properties = new LinkedHashMap<String, Object>();
97         }
98         if (properties == null) {
99             properties = (LinkedHashMap<String, Object>) entityTpl.get(PROPERTIES);
100         }
101         if (properties == null) {
102             properties = new LinkedHashMap<String, Object>();
103         }
104
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);
113                     }
114                     PropertyDef pd = (PropertyDef) propsDef.get(pname);
115                     Property prop = new Property(pname, pvalue, pd.getSchema(), customDef);
116                     props.add(prop);
117                 }
118             }
119         }
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);
124                 props.add(prop);
125             }
126         }
127         return props;
128     }
129
130     public void validate() {
131         _validateProperties(entityTpl, (StatefulEntityType) typeDefinition);
132     }
133
134     // getters/setters
135     public NodeTemplate getTarget() {
136         return target;
137     }
138
139     public NodeTemplate getSource() {
140         return source;
141     }
142
143     public void setSource(NodeTemplate nt) {
144         source = nt;
145     }
146
147     public void setTarget(NodeTemplate nt) {
148         target = nt;
149     }
150
151     @Override
152     public String toString() {
153         return "RelationshipTemplate{" +
154                 "name='" + name + '\'' +
155                 ", target=" + target.getName() +
156                 ", source=" + source.getName() +
157                 ", _properties=" + _properties +
158                 '}';
159     }
160
161 }
162
163 /*python
164
165 from toscaparser.entity_template import EntityTemplate
166 from toscaparser.properties import Property
167
168 SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS,
169             INTERFACES, CAPABILITIES, TYPE) = \
170            ('derived_from', 'properties', 'requirements', 'interfaces',
171             'capabilities', 'type')
172
173 log = logging.getLogger('tosca')
174
175
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,
182                                                    'relationship_type',
183                                                    custom_def)
184         self.name = name.lower()
185         self.target = target
186         self.source = source
187
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
193
194     def _create_relationship_properties(self):
195         props = []
196         properties = {}
197         relationship = self.entity_tpl.get('relationship')
198
199         if not relationship:
200             for value in self.entity_tpl.values():
201                 if isinstance(value, dict):
202                     relationship = value.get('relationship')
203                     break
204
205         if relationship:
206             properties = self.type_definition.get_value(self.PROPERTIES,
207                                                         relationship) or {}
208         if not properties:
209             properties = self.entity_tpl.get(self.PROPERTIES) or {}
210
211         if properties:
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)
219                     props.append(prop)
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)
223                 props.append(prop)
224         return props
225
226     def validate(self):
227         self._validate_properties(self.entity_tpl, self.type_definition)*/