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.elements;
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
27 public class CapabilityTypeDef extends StatefulEntityType {
28 // TOSCA built-in capabilities type
30 private static final String TOSCA_TYPEURI_CAPABILITY_ROOT = "tosca.capabilities.Root";
33 private String nodetype;
34 private LinkedHashMap<String, Object> customDef;
35 private LinkedHashMap<String, Object> properties;
36 private LinkedHashMap<String, Object> parentCapabilities;
38 @SuppressWarnings("unchecked")
39 public CapabilityTypeDef(String cname, String ctype, String ntype, LinkedHashMap<String, Object> ccustomDef) {
40 super(ctype, CAPABILITY_PREFIX, ccustomDef);
45 customDef = ccustomDef;
47 properties = (LinkedHashMap<String, Object>) defs.get(PROPERTIES);
49 parentCapabilities = getParentCapabilities(customDef);
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"));
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()));
67 if (parentProperties != null) {
68 for (Map.Entry<String, Object> me : parentProperties.entrySet()) {
69 LinkedHashMap<String, Object> props = (LinkedHashMap<String, Object>) me.getValue();
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));
85 public LinkedHashMap<String, PropertyDef> getPropertiesDef() {
86 LinkedHashMap<String, PropertyDef> pds = new LinkedHashMap<>();
87 for (PropertyDef pd : getPropertiesDefObjects()) {
88 pds.put(pd.getName(), pd);
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();
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));
114 sParentCap = (String) ((LinkedHashMap<String, Object>) capabilities.get(sParentCap)).get("derived_from");
120 public CapabilityTypeDef getParentType() {
121 // Return a capability this capability is derived from
125 String pnode = derivedFrom(defs);
126 if (pnode != null && !pnode.isEmpty()) {
127 return new CapabilityTypeDef(name, pnode, nodetype, customDef);
132 public boolean inheritsFrom(ArrayList<String> typeNames) {
133 // Check this capability is in type_names
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())) {
139 } else if (getParentType() != null) {
140 return getParentType().inheritsFrom(typeNames);
147 public LinkedHashMap<String, Object> getProperties() {
151 public String getName() {
157 from toscaparser.elements.property_definition import PropertyDef
158 from toscaparser.elements.statefulentitytype import StatefulEntityType
161 class CapabilityTypeDef(StatefulEntityType):
162 '''TOSCA built-in capabilities type.'''
163 TOSCA_TYPEURI_CAPABILITY_ROOT = 'tosca.capabilities.Root'
165 def __init__(self, name, ctype, ntype, custom_def=None):
167 super(CapabilityTypeDef, self).__init__(ctype, self.CAPABILITY_PREFIX,
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)
176 def get_properties_def_objects(self):
177 '''Return a list of property definition objects.'''
179 parent_properties = {}
180 if self.parent_capabilities:
181 for type, value in self.parent_capabilities.items():
182 parent_properties[type] = value.get('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))
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()}
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
206 def _get_parent_capabilities(self, custom_def=None):
208 parent_cap = self.parent_type
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']
220 def parent_type(self):
221 '''Return a capability this capability is derived from.'''
222 if not hasattr(self, 'defs'):
224 pnode = self.derived_from(self.defs)
226 return CapabilityTypeDef(self.name, pnode,
227 self.nodetype, self.custom_def)
229 def inherits_from(self, type_names):
230 '''Check this capability is in type_names
232 Check if this capability or some of its parent types
233 are in the list of types: type_names
235 if self.type in type_names:
237 elif self.parent_type:
238 return self.parent_type.inherits_from(type_names)