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