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;
26 public class DataType extends StatefulEntityType {
28 LinkedHashMap<String, Object> customDef;
30 public DataType(String _dataTypeName, LinkedHashMap<String, Object> _customDef) {
31 super(_dataTypeName, DATATYPE_NETWORK_PREFIX, _customDef);
33 customDef = _customDef;
36 public DataType getParentType() {
37 // Return a datatype this datatype is derived from
39 String ptype = derivedFrom(defs);
41 return new DataType(ptype, customDef);
47 public String getValueType() {
48 // Return 'type' section in the datatype schema
50 return (String) entityValue(defs, "type");
55 public ArrayList<PropertyDef> getAllPropertiesObjects() {
56 //Return all properties objects defined in type and parent type
57 ArrayList<PropertyDef> propsDef = getPropertiesDefObjects();
58 DataType ptype = getParentType();
59 while (ptype != null) {
60 propsDef.addAll(ptype.getPropertiesDefObjects());
61 ptype = ptype.getParentType();
66 public LinkedHashMap<String, PropertyDef> getAllProperties() {
67 // Return a dictionary of all property definition name-object pairs
68 LinkedHashMap<String, PropertyDef> pno = new LinkedHashMap<>();
69 for (PropertyDef pd : getAllPropertiesObjects()) {
70 pno.put(pd.getName(), pd);
75 public Object getAllPropertyValue(String name) {
76 // Return the value of a given property name
77 LinkedHashMap<String, PropertyDef> propsDef = getAllProperties();
78 if (propsDef != null && propsDef.get(name) != null) {
79 return propsDef.get(name).getPDValue();
84 public LinkedHashMap<String, Object> getDefs() {
92 from toscaparser.elements.statefulentitytype import StatefulEntityType
95 class DataType(StatefulEntityType):
96 '''TOSCA built-in and user defined complex data type.'''
98 def __init__(self, datatypename, custom_def=None):
99 super(DataType, self).__init__(datatypename,
100 self.DATATYPE_NETWORK_PREFIX,
102 self.custom_def = custom_def
105 def parent_type(self):
106 '''Return a datatype this datatype is derived from.'''
107 ptype = self.derived_from(self.defs)
109 return DataType(ptype, self.custom_def)
113 def value_type(self):
114 '''Return 'type' section in the datatype schema.'''
115 return self.entity_value(self.defs, 'type')
117 def get_all_properties_objects(self):
118 '''Return all properties objects defined in type and parent type.'''
119 props_def = self.get_properties_def_objects()
120 ptype = self.parent_type
122 props_def.extend(ptype.get_properties_def_objects())
123 ptype = ptype.parent_type
126 def get_all_properties(self):
127 '''Return a dictionary of all property definition name-object pairs.'''
128 return {prop.name: prop
129 for prop in self.get_all_properties_objects()}
131 def get_all_property_value(self, name):
132 '''Return the value of a given property name.'''
133 props_def = self.get_all_properties()
134 if props_def and name in props_def.key():
135 return props_def[name].value