Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / StatefulEntityType.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.elements;
22
23 import org.onap.sdc.toscaparser.api.UnsupportedType;
24 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26
27 import java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30
31
32 public class StatefulEntityType extends EntityType {
33     // Class representing TOSCA states
34
35     public static final String[] INTERFACE_NODE_LIFECYCLE_OPERATIONS = {
36             "create", "configure", "start", "stop", "delete"};
37
38     public static final String[] INTERFACE_RELATIONSHIP_CONFIGURE_OPERATIONS = {
39             "post_configure_source", "post_configure_target", "add_target", "remove_target"};
40
41     public StatefulEntityType() {
42         // void constructor for subclasses that don't want super
43     }
44
45     @SuppressWarnings("unchecked")
46     public StatefulEntityType(String entityType, String prefix, LinkedHashMap<String, Object> customDef) {
47
48         String entireEntityType = entityType;
49         if (UnsupportedType.validateType(entireEntityType)) {
50             defs = null;
51         } else {
52             if (entityType.startsWith(TOSCA + ":")) {
53                 entityType = entityType.substring(TOSCA.length() + 1);
54                 entireEntityType = prefix + entityType;
55             }
56             if (!entityType.startsWith(TOSCA)) {
57                 entireEntityType = prefix + entityType;
58             }
59             if (TOSCA_DEF.get(entireEntityType) != null) {
60                 defs = (LinkedHashMap<String, Object>) TOSCA_DEF.get(entireEntityType);
61                 entityType = entireEntityType;
62             } else if (customDef != null && customDef.get(entityType) != null) {
63                 defs = (LinkedHashMap<String, Object>) customDef.get(entityType);
64             } else {
65                 defs = null;
66                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE136", String.format(
67                         "InvalidTypeError: \"%s\" is not a valid type", entityType)));
68             }
69         }
70         type = entityType;
71     }
72
73     @SuppressWarnings("unchecked")
74     public ArrayList<PropertyDef> getPropertiesDefObjects() {
75         // Return a list of property definition objects
76         ArrayList<PropertyDef> properties = new ArrayList<PropertyDef>();
77         LinkedHashMap<String, Object> props = (LinkedHashMap<String, Object>) getDefinition(PROPERTIES);
78         if (props != null) {
79             for (Map.Entry<String, Object> me : props.entrySet()) {
80                 String pdname = me.getKey();
81                 Object to = me.getValue();
82                 if (to == null || !(to instanceof LinkedHashMap)) {
83                     String s = to == null ? "null" : to.getClass().getSimpleName();
84                     ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE137", String.format(
85                             "Unexpected type error: property \"%s\" has type \"%s\" (expected dict)", pdname, s)));
86                     continue;
87                 }
88                 LinkedHashMap<String, Object> pdschema = (LinkedHashMap<String, Object>) to;
89                 properties.add(new PropertyDef(pdname, null, pdschema));
90             }
91         }
92         return properties;
93     }
94
95     public LinkedHashMap<String, PropertyDef> getPropertiesDef() {
96         LinkedHashMap<String, PropertyDef> pds = new LinkedHashMap<String, PropertyDef>();
97         for (PropertyDef pd : getPropertiesDefObjects()) {
98             pds.put(pd.getName(), pd);
99         }
100         return pds;
101     }
102
103     public PropertyDef getPropertyDefValue(String name) {
104         // Return the property definition associated with a given name
105         PropertyDef pd = null;
106         LinkedHashMap<String, PropertyDef> propsDef = getPropertiesDef();
107         if (propsDef != null) {
108             pd = propsDef.get(name);
109         }
110         return pd;
111     }
112
113     public ArrayList<AttributeDef> getAttributesDefObjects() {
114         // Return a list of attribute definition objects
115         @SuppressWarnings("unchecked")
116         LinkedHashMap<String, Object> attrs = (LinkedHashMap<String, Object>) getValue(ATTRIBUTES, null, true);
117         ArrayList<AttributeDef> ads = new ArrayList<>();
118         if (attrs != null) {
119             for (Map.Entry<String, Object> me : attrs.entrySet()) {
120                 String attr = me.getKey();
121                 @SuppressWarnings("unchecked")
122                 LinkedHashMap<String, Object> adschema = (LinkedHashMap<String, Object>) me.getValue();
123                 ads.add(new AttributeDef(attr, null, adschema));
124             }
125         }
126         return ads;
127     }
128
129     public LinkedHashMap<String, AttributeDef> getAttributesDef() {
130         // Return a dictionary of attribute definition name-object pairs
131
132         LinkedHashMap<String, AttributeDef> ads = new LinkedHashMap<>();
133         for (AttributeDef ado : getAttributesDefObjects()) {
134             ads.put(((AttributeDef) ado).getName(), ado);
135         }
136         return ads;
137     }
138
139     public AttributeDef getAttributeDefValue(String name) {
140         // Return the attribute definition associated with a given name
141         AttributeDef ad = null;
142         LinkedHashMap<String, AttributeDef> attrsDef = getAttributesDef();
143         if (attrsDef != null) {
144             ad = attrsDef.get(name);
145         }
146         return ad;
147     }
148
149     public String getType() {
150         return type;
151     }
152 }
153
154 /*python
155
156 from toscaparser.common.exception import InvalidTypeError
157 from toscaparser.elements.attribute_definition import AttributeDef
158 from toscaparser.elements.entity_type import EntityType
159 from toscaparser.elements.property_definition import PropertyDef
160 from toscaparser.unsupportedtype import UnsupportedType
161
162
163 class StatefulEntityType(EntityType):
164     '''Class representing TOSCA states.'''
165
166     interfaces_node_lifecycle_operations = ['create',
167                                             'configure', 'start',
168                                             'stop', 'delete']
169
170     interfaces_relationship_configure_operations = ['post_configure_source',
171                                                     'post_configure_target',
172                                                     'add_target',
173                                                     'remove_target']
174
175     def __init__(self, entitytype, prefix, custom_def=None):
176         entire_entitytype = entitytype
177         if UnsupportedType.validate_type(entire_entitytype):
178             self.defs = None
179         else:
180             if entitytype.startswith(self.TOSCA + ":"):
181                 entitytype = entitytype[(len(self.TOSCA) + 1):]
182                 entire_entitytype = prefix + entitytype
183             if not entitytype.startswith(self.TOSCA):
184                 entire_entitytype = prefix + entitytype
185             if entire_entitytype in list(self.TOSCA_DEF.keys()):
186                 self.defs = self.TOSCA_DEF[entire_entitytype]
187                 entitytype = entire_entitytype
188             elif custom_def and entitytype in list(custom_def.keys()):
189                 self.defs = custom_def[entitytype]
190             else:
191                 self.defs = None
192                 ValidationIssueCollector.appendException(
193                     InvalidTypeError(what=entitytype))
194         self.type = entitytype
195
196     def get_properties_def_objects(self):
197         '''Return a list of property definition objects.'''
198         properties = []
199         props = self.get_definition(self.PROPERTIES)
200         if props:
201             for prop, schema in props.items():
202                 properties.append(PropertyDef(prop, None, schema))
203         return properties
204
205     def get_properties_def(self):
206         '''Return a dictionary of property definition name-object pairs.'''
207         return {prop.name: prop
208                 for prop in self.get_properties_def_objects()}
209
210     def get_property_def_value(self, name):
211         '''Return the property definition associated with a given name.'''
212         props_def = self.get_properties_def()
213         if props_def and name in props_def.keys():
214             return props_def[name].value
215
216     def get_attributes_def_objects(self):
217         '''Return a list of attribute definition objects.'''
218         attrs = self.get_value(self.ATTRIBUTES, parent=True)
219         if attrs:
220             return [AttributeDef(attr, None, schema)
221                     for attr, schema in attrs.items()]
222         return []
223
224     def get_attributes_def(self):
225         '''Return a dictionary of attribute definition name-object pairs.'''
226         return {attr.name: attr
227                 for attr in self.get_attributes_def_objects()}
228
229     def get_attribute_def_value(self, name):
230         '''Return the attribute definition associated with a given name.'''
231         attrs_def = self.get_attributes_def()
232         if attrs_def and name in attrs_def.keys():
233             return attrs_def[name].value
234 */