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.Objects;
24 import java.util.Optional;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.openecomp.core.validation.ErrorMessageCode;
29 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
30 import org.openecomp.core.validation.types.GlobalValidationContext;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.heat.datatypes.model.Resource;
34 import org.openecomp.sdc.heat.services.HeatConstants;
35 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
36 import org.openecomp.sdc.validation.ResourceValidator;
37 import org.openecomp.sdc.validation.ValidationContext;
38 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
41 * Created by TALIO on 2/22/2017.
43 public class ResourceGroupResourceValidator implements ResourceValidator {
44 private static final ErrorMessageCode ERROR_CODE_HRR1 = new ErrorMessageCode("HRR1");
45 private static final ErrorMessageCode ERROR_CODE_HRR2 = new ErrorMessageCode("HRR2");
46 private static final ErrorMessageCode ERROR_CODE_HRR3 = new ErrorMessageCode("HRR3");
47 private static final ErrorMessageCode ERROR_CODE_HRR4 = new ErrorMessageCode("HRR4");
48 private static final ErrorMessageCode ERROR_CODE_HRR5 = new ErrorMessageCode("HRR5");
49 private static final ErrorMessageCode ERROR_CODE_HRR6 = new ErrorMessageCode("HRR6");
50 private static final ErrorMessageCode ERROR_CODE_HRR7 = new ErrorMessageCode("HRR7");
51 private static final ErrorMessageCode ERROR_CODE_HRR8 = new ErrorMessageCode("HRR8");
54 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
55 GlobalValidationContext globalContext, ValidationContext validationContext) {
56 validateResourceGroupType(fileName, resourceEntry, globalContext);
59 private static void validateResourceGroupType(String fileName,
60 Map.Entry<String, Resource> resourceEntry,
61 GlobalValidationContext globalContext) {
62 globalContext.setMessageCode(ERROR_CODE_HRR6);
64 .checkResourceTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
66 globalContext.setMessageCode(ERROR_CODE_HRR7);
68 .checkResourceGroupTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
70 globalContext.setMessageCode(ERROR_CODE_HRR8);
71 HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(fileName, resourceEntry.getKey(),
72 resourceEntry.getValue(), globalContext);
73 Resource resourceDef = HeatTreeManagerUtil
74 .getResourceDef( resourceEntry.getValue());
76 if (resourceDef != null && Objects.nonNull(resourceDef.getType())
77 && HeatValidationService.isNestedResource(resourceDef.getType())) {
78 Optional<String> indexVarValue =
79 getResourceGroupIndexVarValue(resourceEntry, fileName, globalContext);
80 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceDef, indexVarValue,
86 private static Optional<String> getResourceGroupIndexVarValue(
87 Map.Entry<String, Resource> resourceEntry, String fileName,
88 GlobalValidationContext globalContext) {
90 resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
91 if (indexVar == null) {
92 return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
95 if (indexVar instanceof String) {
96 return Optional.of((String) indexVar);
98 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
99 .getErrorWithParameters(
100 ERROR_CODE_HRR1, Messages.RESOURCE_GROUP_INVALID_INDEX_VAR.getErrorMessage(),
101 resourceEntry.getKey()));
102 return Optional.empty();
106 private static void handleNestedResourceType(String fileName, String resourceName,
107 Resource resource, Optional<String> indexVarValue,
108 GlobalValidationContext globalContext) {
109 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
111 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
114 private static void validateAllPropertiesMatchNestedParameters(String fileName,
117 Optional<String> indexVarValue,
118 GlobalValidationContext
120 String resourceType = resource.getType();
121 if (globalContext.getFileContextMap().containsKey(resourceType)) {
122 Set<String> propertiesNames =
123 resource.getProperties() == null ? null : resource.getProperties().keySet();
124 if (CollectionUtils.isNotEmpty(propertiesNames)) {
125 globalContext.setMessageCode(ERROR_CODE_HRR4);
126 HeatValidationService
127 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
130 globalContext.setMessageCode(ERROR_CODE_HRR5);
131 HeatValidationService
132 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
133 indexVarValue, globalContext);
136 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
137 .getErrorWithParameters(
138 ERROR_CODE_HRR2, Messages.MISSING_NESTED_FILE.getErrorMessage(),
143 private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
144 GlobalValidationContext globalContext) {
145 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
146 if (HeatValidationService
147 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
148 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
149 .getErrorWithParameters(
150 ERROR_CODE_HRR3, Messages.NESTED_LOOP.getErrorMessage(),
151 HeatValidationService.drawFilesLoop(filesInLoop)));