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.heat.services.HeatConstants;
27 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
28 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.validation.ResourceValidator;
32 import org.openecomp.sdc.validation.ValidationContext;
33 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
39 import java.util.Objects;
40 import java.util.Optional;
44 * Created by TALIO on 2/22/2017.
46 public class ResourceGroupResourceValidator implements ResourceValidator {
47 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
49 private static final ErrorMessageCode ERROR_CODE_HRR1 = new ErrorMessageCode("HRR1");
50 private static final ErrorMessageCode ERROR_CODE_HRR2 = new ErrorMessageCode("HRR2");
51 private static final ErrorMessageCode ERROR_CODE_HRR3 = new ErrorMessageCode("HRR3");
52 private static final ErrorMessageCode ERROR_CODE_HRR4 = new ErrorMessageCode("HRR4");
53 private static final ErrorMessageCode ERROR_CODE_HRR5 = new ErrorMessageCode("HRR5");
54 private static final ErrorMessageCode ERROR_CODE_HRR6 = new ErrorMessageCode("HRR6");
55 private static final ErrorMessageCode ERROR_CODE_HRR7 = new ErrorMessageCode("HRR7");
56 private static final ErrorMessageCode ERROR_CODE_HRR8 = new ErrorMessageCode("HRR8");
59 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
60 GlobalValidationContext globalContext, ValidationContext validationContext) {
61 validateResourceGroupType(fileName, resourceEntry, globalContext);
64 private static void validateResourceGroupType(String fileName,
65 Map.Entry<String, Resource> resourceEntry,
66 GlobalValidationContext globalContext) {
68 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
70 globalContext.setMessageCode(ERROR_CODE_HRR6);
72 .checkResourceTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
74 globalContext.setMessageCode(ERROR_CODE_HRR7);
76 .checkResourceGroupTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
78 globalContext.setMessageCode(ERROR_CODE_HRR8);
79 HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(fileName, resourceEntry.getKey(),
80 resourceEntry.getValue(), globalContext);
81 Resource resourceDef = HeatTreeManagerUtil
82 .getResourceDef( resourceEntry.getValue());
84 if (resourceDef != null && Objects.nonNull(resourceDef.getType())
85 && HeatValidationService.isNestedResource(resourceDef.getType())) {
86 Optional<String> indexVarValue =
87 getResourceGroupIndexVarValue(resourceEntry, fileName, globalContext);
88 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceDef, indexVarValue,
93 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
96 private static Optional<String> getResourceGroupIndexVarValue(
97 Map.Entry<String, Resource> resourceEntry, String fileName,
98 GlobalValidationContext globalContext) {
100 resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
101 if (indexVar == null) {
102 return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
105 if (indexVar instanceof String) {
106 return Optional.of((String) indexVar);
108 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
109 .getErrorWithParameters(
110 ERROR_CODE_HRR1, Messages.RESOURCE_GROUP_INVALID_INDEX_VAR.getErrorMessage(),
111 resourceEntry.getKey()),
112 LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
113 LoggerErrorDescription.INVALID_INDEX_VAR);
114 return Optional.empty();
118 private static void handleNestedResourceType(String fileName, String resourceName,
119 Resource resource, Optional<String> indexVarValue,
120 GlobalValidationContext globalContext) {
122 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
124 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
126 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
128 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
131 private static void validateAllPropertiesMatchNestedParameters(String fileName,
134 Optional<String> indexVarValue,
135 GlobalValidationContext
138 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
140 String resourceType = resource.getType();
141 if (globalContext.getFileContextMap().containsKey(resourceType)) {
142 Set<String> propertiesNames =
143 resource.getProperties() == null ? null : resource.getProperties().keySet();
144 if (CollectionUtils.isNotEmpty(propertiesNames)) {
145 globalContext.setMessageCode(ERROR_CODE_HRR4);
146 HeatValidationService
147 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
150 globalContext.setMessageCode(ERROR_CODE_HRR5);
151 HeatValidationService
152 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
153 indexVarValue, globalContext);
156 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
157 .getErrorWithParameters(
158 ERROR_CODE_HRR2, Messages.MISSING_NESTED_FILE.getErrorMessage(),
160 LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
161 LoggerErrorDescription.MISSING_FILE);
164 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
167 private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
168 GlobalValidationContext globalContext) {
170 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
172 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
173 if (HeatValidationService
174 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
175 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
176 .getErrorWithParameters(
177 ERROR_CODE_HRR3, Messages.NESTED_LOOP.getErrorMessage(),
178 HeatValidationService.drawFilesLoop(filesInLoop)),
179 LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);
182 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);