Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / PropertyDef.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.LinkedHashMap;
24 import java.util.Map;
25
26 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
27 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
28
29 public class PropertyDef {
30
31     private static final String PROPERTY_KEYNAME_DEFAULT = "default";
32     private static final String PROPERTY_KEYNAME_REQUIRED = "required";
33     private static final String PROPERTY_KEYNAME_STATUS = "status";
34     private static final String VALID_PROPERTY_KEYNAMES[] = {
35             PROPERTY_KEYNAME_DEFAULT,
36             PROPERTY_KEYNAME_REQUIRED,
37             PROPERTY_KEYNAME_STATUS};
38
39     private static final boolean PROPERTY_REQUIRED_DEFAULT = true;
40
41     private static final String VALID_REQUIRED_VALUES[] = {"true", "false"};
42
43     private static final String PROPERTY_STATUS_SUPPORTED = "supported";
44     private static final String PROPERTY_STATUS_EXPERIMENTAL = "experimental";
45     private static final String VALID_STATUS_VALUES[] = {
46             PROPERTY_STATUS_SUPPORTED, PROPERTY_STATUS_EXPERIMENTAL};
47
48     private static final String PROPERTY_STATUS_DEFAULT = PROPERTY_STATUS_SUPPORTED;
49
50     private String name;
51     private Object value;
52     private LinkedHashMap<String, Object> schema;
53     private String _status;
54     private boolean _required;
55
56     public PropertyDef(String pdName, Object pdValue,
57                        LinkedHashMap<String, Object> pdSchema) {
58         name = pdName;
59         value = pdValue;
60         schema = pdSchema;
61         _status = PROPERTY_STATUS_DEFAULT;
62         _required = PROPERTY_REQUIRED_DEFAULT;
63
64         if (schema != null) {
65             // Validate required 'type' property exists
66             if (schema.get("type") == null) {
67                 //msg = (_('Schema definition of "%(pname)s" must have a "type" '
68                 //         'attribute.') % dict(pname=self.name))
69                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE131", String.format(
70                         "InvalidSchemaError: Schema definition of \"%s\" must have a \"type\" attribute", name)));
71             }
72             _loadRequiredAttrFromSchema();
73             _loadStatusAttrFromSchema();
74         }
75     }
76
77     public Object getDefault() {
78         if (schema != null) {
79             for (Map.Entry<String, Object> me : schema.entrySet()) {
80                 if (me.getKey().equals(PROPERTY_KEYNAME_DEFAULT)) {
81                     return me.getValue();
82                 }
83             }
84         }
85         return null;
86     }
87
88     public boolean isRequired() {
89         return _required;
90     }
91
92     private void _loadRequiredAttrFromSchema() {
93         // IF 'required' keyname exists verify it's a boolean,
94         // if so override default
95         Object val = schema.get(PROPERTY_KEYNAME_REQUIRED);
96         if (val != null) {
97             if (val instanceof Boolean) {
98                 _required = (boolean) val;
99             } else {
100                 //valid_values = ', '.join(self.VALID_REQUIRED_VALUES)
101                 //attr = self.PROPERTY_KEYNAME_REQUIRED
102                 //TOSCAException.generate_inv_schema_property_error(self,
103                 //                                                  attr,
104                 //                                                  value,
105                 //                                                  valid_values)
106                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE132", String.format(
107                         "Schema definition of \"%s\" has \"required\" attribute with an invalid value",
108                         name)));
109             }
110         }
111     }
112
113     public String getStatus() {
114         return _status;
115     }
116
117     private void _loadStatusAttrFromSchema() {
118         // IF 'status' keyname exists verify it's a boolean,
119         // if so override default
120         String sts = (String) schema.get(PROPERTY_KEYNAME_STATUS);
121         if (sts != null) {
122             boolean bFound = false;
123             for (String vsv : VALID_STATUS_VALUES) {
124                 if (vsv.equals(sts)) {
125                     bFound = true;
126                     break;
127                 }
128             }
129             if (bFound) {
130                 _status = sts;
131             } else {
132                 //valid_values = ', '.join(self.VALID_STATUS_VALUES)
133                 //attr = self.PROPERTY_KEYNAME_STATUS
134                 //TOSCAException.generate_inv_schema_property_error(self,
135                 //                                                  attr,
136                 //                                                  value,
137                 //                                                  valid_values)
138                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE006", String.format(
139                         "Schema definition of \"%s\" has \"status\" attribute with an invalid value",
140                         name)));
141             }
142         }
143     }
144
145     public String getName() {
146         return name;
147     }
148
149     public LinkedHashMap<String, Object> getSchema() {
150         return schema;
151     }
152
153     public Object getPDValue() {
154         // there's getValue in EntityType...
155         return value;
156     }
157
158 }
159 /*python
160
161 from toscaparser.common.exception import ValidationIssueCollector
162 from toscaparser.common.exception import InvalidSchemaError
163 from toscaparser.common.exception import TOSCAException
164 from toscaparser.utils.gettextutils import _
165
166
167 class PropertyDef(object):
168     '''TOSCA built-in Property type.'''
169
170     VALID_PROPERTY_KEYNAMES = (PROPERTY_KEYNAME_DEFAULT,
171                                PROPERTY_KEYNAME_REQUIRED,
172                                PROPERTY_KEYNAME_STATUS) = \
173         ('default', 'required', 'status')
174
175     PROPERTY_REQUIRED_DEFAULT = True
176
177     VALID_REQUIRED_VALUES = ['true', 'false']
178     VALID_STATUS_VALUES = (PROPERTY_STATUS_SUPPORTED,
179                            PROPERTY_STATUS_EXPERIMENTAL) = \
180         ('supported', 'experimental')
181
182     PROPERTY_STATUS_DEFAULT = PROPERTY_STATUS_SUPPORTED
183
184     def __init__(self, name, value=None, schema=None):
185         self.name = name
186         self.value = value
187         self.schema = schema
188         self._status = self.PROPERTY_STATUS_DEFAULT
189         self._required = self.PROPERTY_REQUIRED_DEFAULT
190
191         # Validate required 'type' property exists
192         try:
193             self.schema['type']
194         except KeyError:
195             msg = (_('Schema definition of "%(pname)s" must have a "type" '
196                      'attribute.') % dict(pname=self.name))
197             ValidationIssueCollector.appendException(
198                 InvalidSchemaError(message=msg))
199
200         if self.schema:
201             self._load_required_attr_from_schema()
202             self._load_status_attr_from_schema()
203
204     @property
205     def default(self):
206         if self.schema:
207             for prop_key, prop_value in self.schema.items():
208                 if prop_key == self.PROPERTY_KEYNAME_DEFAULT:
209                     return prop_value
210         return None
211
212     @property
213     def required(self):
214         return self._required
215
216     def _load_required_attr_from_schema(self):
217         # IF 'required' keyname exists verify it's a boolean,
218         # if so override default
219         if self.PROPERTY_KEYNAME_REQUIRED in self.schema:
220             value = self.schema[self.PROPERTY_KEYNAME_REQUIRED]
221             if isinstance(value, bool):
222                 self._required = value
223             else:
224                 valid_values = ', '.join(self.VALID_REQUIRED_VALUES)
225                 attr = self.PROPERTY_KEYNAME_REQUIRED
226                 TOSCAException.generate_inv_schema_property_error(self,
227                                                                   attr,
228                                                                   value,
229                                                                   valid_values)
230
231     @property
232     def status(self):
233         return self._status
234
235     def _load_status_attr_from_schema(self):
236         # IF 'status' keyname exists verify it's a valid value,
237         # if so override default
238         if self.PROPERTY_KEYNAME_STATUS in self.schema:
239             value = self.schema[self.PROPERTY_KEYNAME_STATUS]
240             if value in self.VALID_STATUS_VALUES:
241                 self._status = value
242             else:
243                 valid_values = ', '.join(self.VALID_STATUS_VALUES)
244                 attr = self.PROPERTY_KEYNAME_STATUS
245                 TOSCAException.generate_inv_schema_property_error(self,
246                                                                   attr,
247                                                                   value,
248                                                                   valid_values)
249 */