2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.Optional;
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.openecomp.core.validation.ErrorMessageCode;
28 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
29 import org.openecomp.core.validation.types.GlobalValidationContext;
30 import org.openecomp.sdc.common.errors.Messages;
31 import org.openecomp.sdc.datatypes.error.ErrorLevel;
32 import org.openecomp.sdc.heat.datatypes.model.Resource;
33 import org.openecomp.sdc.validation.ResourceValidator;
34 import org.openecomp.sdc.validation.ValidationContext;
35 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
38 * Created by TALIO on 2/22/2017.
40 public class NestedResourceValidator implements ResourceValidator {
41 private static final ErrorMessageCode ERROR_CODE_HNR1 = new ErrorMessageCode("HNR1");
42 private static final ErrorMessageCode ERROR_CODE_HNR2 = new ErrorMessageCode("HNR2");
43 private static final ErrorMessageCode ERROR_CODE_HNR3 = new ErrorMessageCode("HNR3");
44 private static final ErrorMessageCode ERROR_CODE_HNR4 = new ErrorMessageCode("HNR4");
47 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
48 GlobalValidationContext globalContext, ValidationContext validationContext) {
50 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
51 Optional.empty(), globalContext);
54 private static void handleNestedResourceType(String fileName, String resourceName,
55 Resource resource, Optional<String> indexVarValue,
56 GlobalValidationContext globalContext) {
57 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
59 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
62 public static void validateAllPropertiesMatchNestedParameters(String fileName,
65 Optional<String> indexVarValue,
66 GlobalValidationContext
68 String resourceType = resource.getType();
69 if (globalContext.getFileContextMap().containsKey(resourceType)) {
70 Set<String> propertiesNames =
71 resource.getProperties() == null ? null : resource.getProperties().keySet();
72 if (CollectionUtils.isNotEmpty(propertiesNames)) {
73 globalContext.setMessageCode(ERROR_CODE_HNR3);
75 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
78 globalContext.setMessageCode(ERROR_CODE_HNR4);
80 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
81 indexVarValue, globalContext);
84 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
85 .getErrorWithParameters(ERROR_CODE_HNR1,
86 Messages.MISSING_NESTED_FILE.getErrorMessage(),
91 public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
92 GlobalValidationContext globalContext) {
93 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
94 if (HeatValidationService
95 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
96 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
97 .getErrorWithParameters(ERROR_CODE_HNR2, Messages.NESTED_LOOP.getErrorMessage(),
98 HeatValidationService.drawFilesLoop(filesInLoop)));