2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.features.model.validator;
\r
20 import java.util.HashMap;
\r
21 import java.util.Map;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.onap.ccsdk.features.model.ConfigModelException;
\r
24 import org.onap.ccsdk.features.model.ValidTypes;
\r
25 import org.onap.ccsdk.features.model.data.DataType;
\r
26 import org.onap.ccsdk.features.model.data.PropertyDefinition;
\r
27 import org.onap.ccsdk.features.model.data.ServiceTemplate;
\r
30 * DataTypeValidator.java Purpose: Provide Configuration Generator DataTypeValidator
\r
34 public class DataTypeValidator {
\r
36 private StringBuilder message;
\r
37 private Map<String, DataType> stDataTypes;
\r
38 private ServiceTemplate serviceTemplate;
\r
39 private PropertyDefinitionValidator propertyDefinitionValidator;
\r
42 * This is a DataTypeValidator
\r
44 * @param serviceTemplate
\r
45 * @throws ConfigModelException
\r
47 public DataTypeValidator(ServiceTemplate serviceTemplate, StringBuilder message) throws ConfigModelException {
\r
48 this.serviceTemplate = serviceTemplate;
\r
49 this.message = message;
\r
50 propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);
\r
51 stDataTypes = new HashMap<>();
\r
56 private void loadInitial() {
\r
57 if (serviceTemplate != null && serviceTemplate.getDataTypes() != null) {
\r
58 message.append("\n DataTypes" + serviceTemplate.getDataTypes());
\r
59 serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
\r
60 stDataTypes.put(dataTypeKey, dataType);
\r
61 message.append("\n Data Type (" + dataTypeKey + ") loaded successfully.");
\r
67 * This is a validateDataTypes
\r
70 * @throws ConfigModelException
\r
72 @SuppressWarnings("squid:S00112")
\r
73 public boolean validateDataTypes() {
\r
74 if (serviceTemplate != null && serviceTemplate.getDataTypes() != null) {
\r
76 serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
\r
77 if (dataType != null) {
\r
79 String derivedFrom = dataType.getDerivedFrom();
\r
80 checkValidDerivedType(dataTypeKey, derivedFrom);
\r
81 checkValidProperties(dataTypeKey, dataType.getProperties());
\r
82 } catch (ConfigModelException e) {
\r
83 throw new RuntimeException(e);
\r
93 private boolean checkValidDerivedType(String dataTypeName, String derivedFrom) throws ConfigModelException {
\r
95 if (StringUtils.isBlank(derivedFrom) || !ValidTypes.getValidDataTypeDerivedFrom().contains(derivedFrom)) {
\r
96 throw new ConfigModelException(derivedFrom + " is not a valid derived type for Data type " + dataTypeName);
\r
101 private boolean checkValidProperties(String dataTypeName, Map<String, PropertyDefinition> properties) {
\r
102 if (properties != null) {
\r
103 message.append("\n validation Data Type (" + dataTypeName + ") Property.");
\r
104 propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
\r