re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / validators / StringValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.be.model.tosca.validators;
22
23 import org.openecomp.sdc.be.config.Configuration.ToscaValidatorsConfig;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.openecomp.sdc.be.model.DataTypeDefinition;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
27 import org.openecomp.sdc.common.util.ValidationUtils;
28
29 import java.util.Map;
30
31 public class StringValidator implements PropertyTypeValidator {
32
33     public static final int DEFAULT_STRING_MAXIMUM_LENGTH = 2500;
34
35     private static int STRING_MAXIMUM_LENGTH = DEFAULT_STRING_MAXIMUM_LENGTH;
36
37     private static final Logger log = Logger.getLogger(StringValidator.class.getName());
38
39     private static StringValidator stringValidator = new StringValidator();
40
41     private StringValidator() {
42         if (ConfigurationManager.getConfigurationManager() != null) {
43             ToscaValidatorsConfig toscaValidators = ConfigurationManager.getConfigurationManager().getConfiguration()
44                     .getToscaValidators();
45             log.debug("toscaValidators= {}", toscaValidators);
46             if (toscaValidators != null) {
47                 Integer stringMaxLength = toscaValidators.getStringMaxLength();
48                 if (stringMaxLength != null) {
49                     STRING_MAXIMUM_LENGTH = stringMaxLength;
50                 }
51             }
52         }
53     }
54
55     public static StringValidator getInstance() {
56         return stringValidator;
57     }
58
59     @Override
60     public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
61
62         if (value == null || value.isEmpty()) {
63             return true;
64         }
65
66         if (value.length() > STRING_MAXIMUM_LENGTH) {
67             log.debug("parameter String length {} is higher than configured({})", value.length(), STRING_MAXIMUM_LENGTH);
68             return false;
69         }
70         String converted = ValidationUtils.removeNoneUtf8Chars(value);
71         boolean isValid = ValidationUtils.validateIsAscii(converted);
72
73         if (!isValid && log.isDebugEnabled()) {
74             log.debug("parameter String value {} is not an ascii string.", value.substring(0, Math.min(value.length(), 20)));
75         }
76
77         return isValid;
78     }
79
80     @Override
81     public boolean isValid(String value, String innerType) {
82         return isValid(value, innerType, null);
83     }
84 }