Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / constraints / ValidValues.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.constraints;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25
26 public class ValidValues extends Constraint {
27
28
29     protected void setValues() {
30         setConstraintKey(VALID_VALUES);
31         Collections.addAll(validPropTypes, Schema.PROPERTY_TYPES);
32     }
33
34
35     public ValidValues(String name, String type, Object c) {
36         super(name, type, c);
37     }
38
39     @SuppressWarnings("unchecked")
40     protected boolean isValid(Object val) {
41         if (!(constraintValue instanceof ArrayList)) {
42             return false;
43         }
44         if (val instanceof ArrayList) {
45             boolean bAll = true;
46             for (Object v : (ArrayList<Object>) val) {
47                 if (!((ArrayList<Object>) constraintValue).contains(v)) {
48                     bAll = false;
49                     break;
50                 }
51             }
52             return bAll;
53         }
54         return ((ArrayList<Object>) constraintValue).contains(val);
55     }
56
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());
60     }
61
62 }
63
64 /*python
65
66 class ValidValues(Constraint):
67 """Constraint class for "valid_values"
68
69 Constrains a property or parameter to a value that is in the list of
70 declared values.
71 """
72 constraint_key = Constraint.VALID_VALUES
73
74 valid_prop_types = Schema.PROPERTY_TYPES
75
76 def __init__(self, property_name, property_type, constraint):
77     super(ValidValues, self).__init__(property_name, property_type,
78                                       constraint)
79     if not isinstance(self.constraint_value, collections.Sequence):
80         ValidationIsshueCollector.appendException(
81             InvalidSchemaError(message=_('The property "valid_values" '
82                                          'expects a list.')))
83
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
89
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,
95                  pvalue=value,
96                  cvalue=allowed))
97
98
99 */