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 org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.model.Resource;
26 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
27 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
28 import org.openecomp.sdc.validation.ResourceValidator;
29 import org.openecomp.sdc.validation.ValidationContext;
30 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
36 import java.util.Optional;
40 * Created by TALIO on 2/22/2017.
42 public class NestedResourceValidator implements ResourceValidator {
43 private static final ErrorMessageCode ERROR_CODE_HNR1 = new ErrorMessageCode("HNR1");
44 private static final ErrorMessageCode ERROR_CODE_HNR2 = new ErrorMessageCode("HNR2");
45 private static final ErrorMessageCode ERROR_CODE_HNR3 = new ErrorMessageCode("HNR3");
46 private static final ErrorMessageCode ERROR_CODE_HNR4 = new ErrorMessageCode("HNR4");
49 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
50 GlobalValidationContext globalContext, ValidationContext validationContext) {
52 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
53 Optional.empty(), globalContext);
56 private static void handleNestedResourceType(String fileName, String resourceName,
57 Resource resource, Optional<String> indexVarValue,
58 GlobalValidationContext globalContext) {
59 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
61 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
64 public static void validateAllPropertiesMatchNestedParameters(String fileName,
67 Optional<String> indexVarValue,
68 GlobalValidationContext
70 String resourceType = resource.getType();
71 if (globalContext.getFileContextMap().containsKey(resourceType)) {
72 Set<String> propertiesNames =
73 resource.getProperties() == null ? null : resource.getProperties().keySet();
74 if (CollectionUtils.isNotEmpty(propertiesNames)) {
75 globalContext.setMessageCode(ERROR_CODE_HNR3);
77 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
80 globalContext.setMessageCode(ERROR_CODE_HNR4);
82 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
83 indexVarValue, globalContext);
86 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
87 .getErrorWithParameters(ERROR_CODE_HNR1,
88 Messages.MISSING_NESTED_FILE.getErrorMessage(),
90 LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
91 LoggerErrorDescription.MISSING_FILE);
95 public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
96 GlobalValidationContext globalContext) {
97 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
98 if (HeatValidationService
99 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
100 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
101 .getErrorWithParameters(ERROR_CODE_HNR2, Messages.NESTED_LOOP.getErrorMessage(),
102 HeatValidationService.drawFilesLoop(filesInLoop)),
103 LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);