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.constraints;
23 import java.util.ArrayList;
24 import java.util.Collections;
26 public class ValidValues extends Constraint {
29 protected void setValues() {
30 setConstraintKey(VALID_VALUES);
31 Collections.addAll(validPropTypes, Schema.PROPERTY_TYPES);
35 public ValidValues(String name, String type, Object c) {
39 @SuppressWarnings("unchecked")
40 protected boolean isValid(Object val) {
41 if (!(constraintValue instanceof ArrayList)) {
44 if (val instanceof ArrayList) {
46 for (Object v : (ArrayList<Object>) val) {
47 if (!((ArrayList<Object>) constraintValue).contains(v)) {
54 return ((ArrayList<Object>) constraintValue).contains(val);
57 protected String errMsg(Object value) {
58 return String.format("The value \"%s\" of property \"%s\" is not valid. Expected a value from \"%s\"",
59 value.toString(), propertyName, constraintValue.toString());
66 class ValidValues(Constraint):
67 """Constraint class for "valid_values"
69 Constrains a property or parameter to a value that is in the list of
72 constraint_key = Constraint.VALID_VALUES
74 valid_prop_types = Schema.PROPERTY_TYPES
76 def __init__(self, property_name, property_type, constraint):
77 super(ValidValues, self).__init__(property_name, property_type,
79 if not isinstance(self.constraint_value, collections.Sequence):
80 ValidationIsshueCollector.appendException(
81 InvalidSchemaError(message=_('The property "valid_values" '
84 def _is_valid(self, value):
85 print '*** payton parser validating ',value,' in ',self.constraint_value#GGG
86 if isinstance(value, list):
87 return all(v in self.constraint_value for v in value)
88 return value in self.constraint_value
90 def _err_msg(self, value):
91 allowed = '[%s]' % ', '.join(str(a) for a in self.constraint_value)
92 return (_('The value "%(pvalue)s" of property "%(pname)s" is not '
93 'valid. Expected a value from "%(cvalue)s".') %
94 dict(pname=self.property_name,