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.LinkedHashMap;
26 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
27 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
29 public class PropertyDef {
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};
39 private static final boolean PROPERTY_REQUIRED_DEFAULT = true;
41 private static final String VALID_REQUIRED_VALUES[] = {"true", "false"};
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};
48 private static final String PROPERTY_STATUS_DEFAULT = PROPERTY_STATUS_SUPPORTED;
52 private LinkedHashMap<String, Object> schema;
53 private String _status;
54 private boolean _required;
56 public PropertyDef(String pdName, Object pdValue,
57 LinkedHashMap<String, Object> pdSchema) {
61 _status = PROPERTY_STATUS_DEFAULT;
62 _required = PROPERTY_REQUIRED_DEFAULT;
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)));
72 _loadRequiredAttrFromSchema();
73 _loadStatusAttrFromSchema();
77 public Object getDefault() {
79 for (Map.Entry<String, Object> me : schema.entrySet()) {
80 if (me.getKey().equals(PROPERTY_KEYNAME_DEFAULT)) {
88 public boolean isRequired() {
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);
97 if (val instanceof Boolean) {
98 _required = (boolean) val;
100 //valid_values = ', '.join(self.VALID_REQUIRED_VALUES)
101 //attr = self.PROPERTY_KEYNAME_REQUIRED
102 //TOSCAException.generate_inv_schema_property_error(self,
106 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE132", String.format(
107 "Schema definition of \"%s\" has \"required\" attribute with an invalid value",
113 public String getStatus() {
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);
122 boolean bFound = false;
123 for (String vsv : VALID_STATUS_VALUES) {
124 if (vsv.equals(sts)) {
132 //valid_values = ', '.join(self.VALID_STATUS_VALUES)
133 //attr = self.PROPERTY_KEYNAME_STATUS
134 //TOSCAException.generate_inv_schema_property_error(self,
138 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE006", String.format(
139 "Schema definition of \"%s\" has \"status\" attribute with an invalid value",
145 public String getName() {
149 public LinkedHashMap<String, Object> getSchema() {
153 public Object getPDValue() {
154 // there's getValue in EntityType...
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 _
167 class PropertyDef(object):
168 '''TOSCA built-in Property type.'''
170 VALID_PROPERTY_KEYNAMES = (PROPERTY_KEYNAME_DEFAULT,
171 PROPERTY_KEYNAME_REQUIRED,
172 PROPERTY_KEYNAME_STATUS) = \
173 ('default', 'required', 'status')
175 PROPERTY_REQUIRED_DEFAULT = True
177 VALID_REQUIRED_VALUES = ['true', 'false']
178 VALID_STATUS_VALUES = (PROPERTY_STATUS_SUPPORTED,
179 PROPERTY_STATUS_EXPERIMENTAL) = \
180 ('supported', 'experimental')
182 PROPERTY_STATUS_DEFAULT = PROPERTY_STATUS_SUPPORTED
184 def __init__(self, name, value=None, schema=None):
188 self._status = self.PROPERTY_STATUS_DEFAULT
189 self._required = self.PROPERTY_REQUIRED_DEFAULT
191 # Validate required 'type' property exists
195 msg = (_('Schema definition of "%(pname)s" must have a "type" '
196 'attribute.') % dict(pname=self.name))
197 ValidationIssueCollector.appendException(
198 InvalidSchemaError(message=msg))
201 self._load_required_attr_from_schema()
202 self._load_status_attr_from_schema()
207 for prop_key, prop_value in self.schema.items():
208 if prop_key == self.PROPERTY_KEYNAME_DEFAULT:
214 return self._required
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
224 valid_values = ', '.join(self.VALID_REQUIRED_VALUES)
225 attr = self.PROPERTY_KEYNAME_REQUIRED
226 TOSCAException.generate_inv_schema_property_error(self,
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:
243 valid_values = ', '.join(self.VALID_STATUS_VALUES)
244 attr = self.PROPERTY_KEYNAME_STATUS
245 TOSCAException.generate_inv_schema_property_error(self,