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