Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / CapabilityTypeDef.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 java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26
27 public class CapabilityTypeDef extends StatefulEntityType {
28     // TOSCA built-in capabilities type
29
30     private static final String TOSCA_TYPEURI_CAPABILITY_ROOT = "tosca.capabilities.Root";
31
32     private String name;
33     private String nodetype;
34     private LinkedHashMap<String, Object> customDef;
35     private LinkedHashMap<String, Object> properties;
36     private LinkedHashMap<String, Object> parentCapabilities;
37
38     @SuppressWarnings("unchecked")
39     public CapabilityTypeDef(String cname, String ctype, String ntype, LinkedHashMap<String, Object> ccustomDef) {
40         super(ctype, CAPABILITY_PREFIX, ccustomDef);
41
42         name = cname;
43         nodetype = ntype;
44         properties = null;
45         customDef = ccustomDef;
46         if (defs != null) {
47             properties = (LinkedHashMap<String, Object>) defs.get(PROPERTIES);
48         }
49         parentCapabilities = getParentCapabilities(customDef);
50     }
51
52     @SuppressWarnings("unchecked")
53     public ArrayList<PropertyDef> getPropertiesDefObjects() {
54         // Return a list of property definition objects
55         ArrayList<PropertyDef> propsdefs = new ArrayList<>();
56         LinkedHashMap<String, Object> parentProperties = new LinkedHashMap<>();
57         if (parentCapabilities != null) {
58             for (Map.Entry<String, Object> me : parentCapabilities.entrySet()) {
59                 parentProperties.put(me.getKey(), ((LinkedHashMap<String, Object>) me.getValue()).get("properties"));
60             }
61         }
62         if (properties != null) {
63             for (Map.Entry<String, Object> me : properties.entrySet()) {
64                 propsdefs.add(new PropertyDef(me.getKey(), null, (LinkedHashMap<String, Object>) me.getValue()));
65             }
66         }
67         if (parentProperties != null) {
68             for (Map.Entry<String, Object> me : parentProperties.entrySet()) {
69                 LinkedHashMap<String, Object> props = (LinkedHashMap<String, Object>) me.getValue();
70                 if (props != null) {
71                     for (Map.Entry<String, Object> pe : props.entrySet()) {
72                         String prop = pe.getKey();
73                         LinkedHashMap<String, Object> schema = (LinkedHashMap<String, Object>) pe.getValue();
74                         // add parent property if not overridden by children type
75                         if (properties == null || properties.get(prop) == null) {
76                             propsdefs.add(new PropertyDef(prop, null, schema));
77                         }
78                     }
79                 }
80             }
81         }
82         return propsdefs;
83     }
84
85     public LinkedHashMap<String, PropertyDef> getPropertiesDef() {
86         LinkedHashMap<String, PropertyDef> pds = new LinkedHashMap<>();
87         for (PropertyDef pd : getPropertiesDefObjects()) {
88             pds.put(pd.getName(), pd);
89         }
90         return pds;
91     }
92
93     public PropertyDef getPropertyDefValue(String pdname) {
94         // Return the definition of a given property name
95         LinkedHashMap<String, PropertyDef> propsDef = getPropertiesDef();
96         if (propsDef != null && propsDef.get(pdname) != null) {
97             return (PropertyDef) propsDef.get(pdname).getPDValue();
98         }
99         return null;
100     }
101
102     @SuppressWarnings("unchecked")
103     private LinkedHashMap<String, Object> getParentCapabilities(LinkedHashMap<String, Object> customDef) {
104         LinkedHashMap<String, Object> capabilities = new LinkedHashMap<>();
105         CapabilityTypeDef parentCap = getParentType();
106         if (parentCap != null) {
107             String sParentCap = parentCap.getType();
108             while (!sParentCap.equals(TOSCA_TYPEURI_CAPABILITY_ROOT)) {
109                 if (TOSCA_DEF.get(sParentCap) != null) {
110                     capabilities.put(sParentCap, TOSCA_DEF.get(sParentCap));
111                 } else if (customDef != null && customDef.get(sParentCap) != null) {
112                     capabilities.put(sParentCap, customDef.get(sParentCap));
113                 }
114                 sParentCap = (String) ((LinkedHashMap<String, Object>) capabilities.get(sParentCap)).get("derived_from");
115             }
116         }
117         return capabilities;
118     }
119
120     public CapabilityTypeDef getParentType() {
121         // Return a capability this capability is derived from
122         if (defs == null) {
123             return null;
124         }
125         String pnode = derivedFrom(defs);
126         if (pnode != null && !pnode.isEmpty()) {
127             return new CapabilityTypeDef(name, pnode, nodetype, customDef);
128         }
129         return null;
130     }
131
132     public boolean inheritsFrom(ArrayList<String> typeNames) {
133         // Check this capability is in type_names
134
135         // Check if this capability or some of its parent types
136         // are in the list of types: type_names
137         if (typeNames.contains(getType())) {
138             return true;
139         } else if (getParentType() != null) {
140             return getParentType().inheritsFrom(typeNames);
141         }
142         return false;
143     }
144
145     // getters/setters
146
147     public LinkedHashMap<String, Object> getProperties() {
148         return properties;
149     }
150
151     public String getName() {
152         return name;
153     }
154 }
155
156 /*python
157 from toscaparser.elements.property_definition import PropertyDef
158 from toscaparser.elements.statefulentitytype import StatefulEntityType
159
160
161 class CapabilityTypeDef(StatefulEntityType):
162     '''TOSCA built-in capabilities type.'''
163     TOSCA_TYPEURI_CAPABILITY_ROOT = 'tosca.capabilities.Root'
164
165     def __init__(self, name, ctype, ntype, custom_def=None):
166         self.name = name
167         super(CapabilityTypeDef, self).__init__(ctype, self.CAPABILITY_PREFIX,
168                                                 custom_def)
169         self.nodetype = ntype
170         self.properties = None
171         self.custom_def = custom_def
172         if self.PROPERTIES in self.defs:
173             self.properties = self.defs[self.PROPERTIES]
174         self.parent_capabilities = self._get_parent_capabilities(custom_def)
175
176     def get_properties_def_objects(self):
177         '''Return a list of property definition objects.'''
178         properties = []
179         parent_properties = {}
180         if self.parent_capabilities:
181             for type, value in self.parent_capabilities.items():
182                 parent_properties[type] = value.get('properties')
183         if self.properties:
184             for prop, schema in self.properties.items():
185                 properties.append(PropertyDef(prop, None, schema))
186         if parent_properties:
187             for parent, props in parent_properties.items():
188                 for prop, schema in props.items():
189                     # add parent property if not overridden by children type
190                     if not self.properties or \
191                             prop not in self.properties.keys():
192                         properties.append(PropertyDef(prop, None, schema))
193         return properties
194
195     def get_properties_def(self):
196         '''Return a dictionary of property definition name-object pairs.'''
197         return {prop.name: prop
198                 for prop in self.get_properties_def_objects()}
199
200     def get_property_def_value(self, name):
201         '''Return the definition of a given property name.'''
202         props_def = self.get_properties_def()
203         if props_def and name in props_def:
204             return props_def[name].value
205
206     def _get_parent_capabilities(self, custom_def=None):
207         capabilities = {}
208         parent_cap = self.parent_type
209         if parent_cap:
210             parent_cap = parent_cap.type
211             while parent_cap != self.TOSCA_TYPEURI_CAPABILITY_ROOT:
212                 if parent_cap in self.TOSCA_DEF.keys():
213                     capabilities[parent_cap] = self.TOSCA_DEF[parent_cap]
214                 elif custom_def and parent_cap in custom_def.keys():
215                     capabilities[parent_cap] = custom_def[parent_cap]
216                 parent_cap = capabilities[parent_cap]['derived_from']
217         return capabilities
218
219     @property
220     def parent_type(self):
221         '''Return a capability this capability is derived from.'''
222         if not hasattr(self, 'defs'):
223             return None
224         pnode = self.derived_from(self.defs)
225         if pnode:
226             return CapabilityTypeDef(self.name, pnode,
227                                      self.nodetype, self.custom_def)
228
229     def inherits_from(self, type_names):
230         '''Check this capability is in type_names
231
232            Check if this capability or some of its parent types
233            are in the list of types: type_names
234         '''
235         if self.type in type_names:
236             return True
237         elif self.parent_type:
238             return self.parent_type.inherits_from(type_names)
239         else:
240             return False*/