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 org.onap.sdc.toscaparser.api.UnsupportedType;
24 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
27 import java.util.ArrayList;
28 import java.util.LinkedHashMap;
32 public class StatefulEntityType extends EntityType {
33 // Class representing TOSCA states
35 public static final String[] INTERFACE_NODE_LIFECYCLE_OPERATIONS = {
36 "create", "configure", "start", "stop", "delete"};
38 public static final String[] INTERFACE_RELATIONSHIP_CONFIGURE_OPERATIONS = {
39 "post_configure_source", "post_configure_target", "add_target", "remove_target"};
41 public StatefulEntityType() {
42 // void constructor for subclasses that don't want super
45 @SuppressWarnings("unchecked")
46 public StatefulEntityType(String entityType, String prefix, LinkedHashMap<String, Object> customDef) {
48 String entireEntityType = entityType;
49 if (UnsupportedType.validateType(entireEntityType)) {
52 if (entityType.startsWith(TOSCA + ":")) {
53 entityType = entityType.substring(TOSCA.length() + 1);
54 entireEntityType = prefix + entityType;
56 if (!entityType.startsWith(TOSCA)) {
57 entireEntityType = prefix + entityType;
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);
66 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE136", String.format(
67 "InvalidTypeError: \"%s\" is not a valid type", entityType)));
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);
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)));
88 LinkedHashMap<String, Object> pdschema = (LinkedHashMap<String, Object>) to;
89 properties.add(new PropertyDef(pdname, null, pdschema));
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);
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);
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<>();
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));
129 public LinkedHashMap<String, AttributeDef> getAttributesDef() {
130 // Return a dictionary of attribute definition name-object pairs
132 LinkedHashMap<String, AttributeDef> ads = new LinkedHashMap<>();
133 for (AttributeDef ado : getAttributesDefObjects()) {
134 ads.put(((AttributeDef) ado).getName(), ado);
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);
149 public String getType() {
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
163 class StatefulEntityType(EntityType):
164 '''Class representing TOSCA states.'''
166 interfaces_node_lifecycle_operations = ['create',
167 'configure', 'start',
170 interfaces_relationship_configure_operations = ['post_configure_source',
171 'post_configure_target',
175 def __init__(self, entitytype, prefix, custom_def=None):
176 entire_entitytype = entitytype
177 if UnsupportedType.validate_type(entire_entitytype):
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]
192 ValidationIssueCollector.appendException(
193 InvalidTypeError(what=entitytype))
194 self.type = entitytype
196 def get_properties_def_objects(self):
197 '''Return a list of property definition objects.'''
199 props = self.get_definition(self.PROPERTIES)
201 for prop, schema in props.items():
202 properties.append(PropertyDef(prop, None, schema))
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()}
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
216 def get_attributes_def_objects(self):
217 '''Return a list of attribute definition objects.'''
218 attrs = self.get_value(self.ATTRIBUTES, parent=True)
220 return [AttributeDef(attr, None, schema)
221 for attr, schema in attrs.items()]
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()}
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