Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / DataType.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
26 public class DataType extends StatefulEntityType {
27
28     LinkedHashMap<String, Object> customDef;
29
30     public DataType(String _dataTypeName, LinkedHashMap<String, Object> _customDef) {
31         super(_dataTypeName, DATATYPE_NETWORK_PREFIX, _customDef);
32
33         customDef = _customDef;
34     }
35
36     public DataType getParentType() {
37         // Return a datatype this datatype is derived from
38         if (defs != null) {
39             String ptype = derivedFrom(defs);
40             if (ptype != null) {
41                 return new DataType(ptype, customDef);
42             }
43         }
44         return null;
45     }
46
47     public String getValueType() {
48         // Return 'type' section in the datatype schema
49         if (defs != null) {
50             return (String) entityValue(defs, "type");
51         }
52         return null;
53     }
54
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();
62         }
63         return propsDef;
64     }
65
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);
71         }
72         return pno;
73     }
74
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();
80         }
81         return null;
82     }
83
84     public LinkedHashMap<String, Object> getDefs() {
85         return defs;
86     }
87
88 }
89
90 /*python
91
92 from toscaparser.elements.statefulentitytype import StatefulEntityType
93
94
95 class DataType(StatefulEntityType):
96     '''TOSCA built-in and user defined complex data type.'''
97
98     def __init__(self, datatypename, custom_def=None):
99         super(DataType, self).__init__(datatypename,
100                                        self.DATATYPE_NETWORK_PREFIX,
101                                        custom_def)
102         self.custom_def = custom_def
103
104     @property
105     def parent_type(self):
106         '''Return a datatype this datatype is derived from.'''
107         ptype = self.derived_from(self.defs)
108         if ptype:
109             return DataType(ptype, self.custom_def)
110         return None
111
112     @property
113     def value_type(self):
114         '''Return 'type' section in the datatype schema.'''
115         return self.entity_value(self.defs, 'type')
116
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
121         while ptype:
122             props_def.extend(ptype.get_properties_def_objects())
123             ptype = ptype.parent_type
124         return props_def
125
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()}
130
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
136 */