a8e1ea5b40addf8b201f339169a0e2c056007388
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
18
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;
33
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.Set;
41
42 /**
43  * Created by TALIO on 2/22/2017.
44  */
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");
54
55   @Override
56   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
57                        GlobalValidationContext globalContext, ValidationContext validationContext) {
58     validateResourceGroupType(fileName, resourceEntry, globalContext);
59   }
60
61   private static void validateResourceGroupType(String fileName,
62                                                 Map.Entry<String, Resource> resourceEntry,
63                                                 GlobalValidationContext globalContext) {
64     globalContext.setMessageCode(ERROR_CODE_HRR6);
65     HeatTreeManagerUtil
66             .checkResourceTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
67                     globalContext);
68     globalContext.setMessageCode(ERROR_CODE_HRR7);
69     HeatTreeManagerUtil
70             .checkResourceGroupTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
71                     globalContext);
72     globalContext.setMessageCode(ERROR_CODE_HRR8);
73     HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(fileName, resourceEntry.getKey(),
74             resourceEntry.getValue(), globalContext);
75     Resource resourceDef = HeatTreeManagerUtil
76             .getResourceDef( resourceEntry.getValue());
77
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,
83                 globalContext);
84
85     }
86   }
87
88   private static Optional<String> getResourceGroupIndexVarValue(
89           Map.Entry<String, Resource> resourceEntry, String fileName,
90           GlobalValidationContext globalContext) {
91     Object indexVar =
92             resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
93     if (indexVar == null) {
94       return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
95     }
96
97     if (indexVar instanceof String) {
98       return Optional.of((String) indexVar);
99     } else {
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();
107     }
108   }
109
110   private static void handleNestedResourceType(String fileName, String resourceName,
111                                                Resource resource, Optional<String> indexVarValue,
112                                                GlobalValidationContext globalContext) {
113     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
114             globalContext);
115     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
116   }
117
118   private static void validateAllPropertiesMatchNestedParameters(String fileName,
119                                                                  String resourceName,
120                                                                  Resource resource,
121                                                                  Optional<String> indexVarValue,
122                                                                  GlobalValidationContext
123                                                                          globalContext) {
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,
132                          propertiesNames,
133                          globalContext);
134         globalContext.setMessageCode(ERROR_CODE_HRR5);
135         HeatValidationService
136                 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
137                         indexVarValue, globalContext);
138       }
139     } else {
140       globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
141                       .getErrorWithParameters(
142                               ERROR_CODE_HRR2, Messages.MISSING_NESTED_FILE.getErrorMessage(),
143                               resourceType),
144               LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
145               LoggerErrorDescription.MISSING_FILE);
146     }
147   }
148
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);
159     }
160   }
161 }