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.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.validation.ResourceValidator;
31 import org.openecomp.sdc.validation.ValidationContext;
32 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
38 import java.util.Objects;
39 import java.util.Optional;
43 * Created by TALIO on 2/22/2017.
45 public class ResourceGroupResourceValidator implements ResourceValidator {
46 private static final ErrorMessageCode ERROR_CODE_HRR1 = new ErrorMessageCode("HRR1");
47 private static final ErrorMessageCode ERROR_CODE_HRR2 = new ErrorMessageCode("HRR2");
48 private static final ErrorMessageCode ERROR_CODE_HRR3 = new ErrorMessageCode("HRR3");
49 private static final ErrorMessageCode ERROR_CODE_HRR4 = new ErrorMessageCode("HRR4");
50 private static final ErrorMessageCode ERROR_CODE_HRR5 = new ErrorMessageCode("HRR5");
51 private static final ErrorMessageCode ERROR_CODE_HRR6 = new ErrorMessageCode("HRR6");
52 private static final ErrorMessageCode ERROR_CODE_HRR7 = new ErrorMessageCode("HRR7");
53 private static final ErrorMessageCode ERROR_CODE_HRR8 = new ErrorMessageCode("HRR8");
56 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
57 GlobalValidationContext globalContext, ValidationContext validationContext) {
58 validateResourceGroupType(fileName, resourceEntry, globalContext);
61 private static void validateResourceGroupType(String fileName,
62 Map.Entry<String, Resource> resourceEntry,
63 GlobalValidationContext globalContext) {
64 globalContext.setMessageCode(ERROR_CODE_HRR6);
66 .checkResourceTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
68 globalContext.setMessageCode(ERROR_CODE_HRR7);
70 .checkResourceGroupTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
72 globalContext.setMessageCode(ERROR_CODE_HRR8);
73 HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(fileName, resourceEntry.getKey(),
74 resourceEntry.getValue(), globalContext);
75 Resource resourceDef = HeatTreeManagerUtil
76 .getResourceDef( resourceEntry.getValue());
78 if (resourceDef != null && Objects.nonNull(resourceDef.getType())
79 && HeatValidationService.isNestedResource(resourceDef.getType())) {
80 Optional<String> indexVarValue =
81 getResourceGroupIndexVarValue(resourceEntry, fileName, globalContext);
82 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceDef, indexVarValue,
88 private static Optional<String> getResourceGroupIndexVarValue(
89 Map.Entry<String, Resource> resourceEntry, String fileName,
90 GlobalValidationContext globalContext) {
92 resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
93 if (indexVar == null) {
94 return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
97 if (indexVar instanceof String) {
98 return Optional.of((String) indexVar);
100 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
101 .getErrorWithParameters(
102 ERROR_CODE_HRR1, Messages.RESOURCE_GROUP_INVALID_INDEX_VAR.getErrorMessage(),
103 resourceEntry.getKey()),
104 LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
105 LoggerErrorDescription.INVALID_INDEX_VAR);
106 return Optional.empty();
110 private static void handleNestedResourceType(String fileName, String resourceName,
111 Resource resource, Optional<String> indexVarValue,
112 GlobalValidationContext globalContext) {
113 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
115 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
118 private static void validateAllPropertiesMatchNestedParameters(String fileName,
121 Optional<String> indexVarValue,
122 GlobalValidationContext
124 String resourceType = resource.getType();
125 if (globalContext.getFileContextMap().containsKey(resourceType)) {
126 Set<String> propertiesNames =
127 resource.getProperties() == null ? null : resource.getProperties().keySet();
128 if (CollectionUtils.isNotEmpty(propertiesNames)) {
129 globalContext.setMessageCode(ERROR_CODE_HRR4);
130 HeatValidationService
131 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
134 globalContext.setMessageCode(ERROR_CODE_HRR5);
135 HeatValidationService
136 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
137 indexVarValue, globalContext);
140 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
141 .getErrorWithParameters(
142 ERROR_CODE_HRR2, Messages.MISSING_NESTED_FILE.getErrorMessage(),
144 LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
145 LoggerErrorDescription.MISSING_FILE);
149 private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
150 GlobalValidationContext globalContext) {
151 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
152 if (HeatValidationService
153 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
154 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
155 .getErrorWithParameters(
156 ERROR_CODE_HRR3, Messages.NESTED_LOOP.getErrorMessage(),
157 HeatValidationService.drawFilesLoop(filesInLoop)),
158 LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);