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.context.impl.MdcDataDebugMessage;
27 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
28 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
29 import org.openecomp.sdc.validation.ResourceValidator;
30 import org.openecomp.sdc.validation.ValidationContext;
31 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
37 import java.util.Optional;
41 * Created by TALIO on 2/22/2017.
43 public class NestedResourceValidator implements ResourceValidator {
44 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
45 private static final ErrorMessageCode ERROR_CODE_HNR1 = new ErrorMessageCode("HNR1");
46 private static final ErrorMessageCode ERROR_CODE_HNR2 = new ErrorMessageCode("HNR2");
47 private static final ErrorMessageCode ERROR_CODE_HNR3 = new ErrorMessageCode("HNR3");
48 private static final ErrorMessageCode ERROR_CODE_HNR4 = new ErrorMessageCode("HNR4");
51 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
52 GlobalValidationContext globalContext, ValidationContext validationContext) {
54 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
55 Optional.empty(), globalContext);
58 private static void handleNestedResourceType(String fileName, String resourceName,
59 Resource resource, Optional<String> indexVarValue,
60 GlobalValidationContext globalContext) {
62 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
64 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
66 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
68 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
71 public static void validateAllPropertiesMatchNestedParameters(String fileName,
74 Optional<String> indexVarValue,
75 GlobalValidationContext
78 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
80 String resourceType = resource.getType();
81 if (globalContext.getFileContextMap().containsKey(resourceType)) {
82 Set<String> propertiesNames =
83 resource.getProperties() == null ? null : resource.getProperties().keySet();
84 if (CollectionUtils.isNotEmpty(propertiesNames)) {
85 globalContext.setMessageCode(ERROR_CODE_HNR3);
87 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
90 globalContext.setMessageCode(ERROR_CODE_HNR4);
92 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
93 indexVarValue, globalContext);
96 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
97 .getErrorWithParameters(ERROR_CODE_HNR1,
98 Messages.MISSING_NESTED_FILE.getErrorMessage(),
100 LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
101 LoggerErrorDescription.MISSING_FILE);
104 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
107 public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
108 GlobalValidationContext globalContext) {
110 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
112 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
113 if (HeatValidationService
114 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
115 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
116 .getErrorWithParameters(ERROR_CODE_HNR2, Messages.NESTED_LOOP.getErrorMessage(),
117 HeatValidationService.drawFilesLoop(filesInLoop)),
118 LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);
121 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);