06794923cf1e959c89d583104181ae2482bfb9b0
[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.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;
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.Optional;
41 import java.util.Set;
42
43 /**
44  * Created by TALIO on 2/22/2017.
45  */
46 public class ResourceGroupResourceValidator implements ResourceValidator {
47   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
48
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");
57
58   @Override
59   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
60                        GlobalValidationContext globalContext, ValidationContext validationContext) {
61     validateResourceGroupType(fileName, resourceEntry, globalContext);
62   }
63
64   private static void validateResourceGroupType(String fileName,
65                                                 Map.Entry<String, Resource> resourceEntry,
66                                                 GlobalValidationContext globalContext) {
67
68     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
69
70     globalContext.setMessageCode(ERROR_CODE_HRR6);
71     HeatTreeManagerUtil
72             .checkResourceTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
73                     globalContext);
74     globalContext.setMessageCode(ERROR_CODE_HRR7);
75     HeatTreeManagerUtil
76             .checkResourceGroupTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
77                     globalContext);
78     globalContext.setMessageCode(ERROR_CODE_HRR8);
79     HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(fileName, resourceEntry.getKey(),
80             resourceEntry.getValue(), globalContext);
81     Resource resourceDef = HeatTreeManagerUtil
82             .getResourceDef( resourceEntry.getValue());
83
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,
89                 globalContext);
90
91     }
92
93     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
94   }
95
96   private static Optional<String> getResourceGroupIndexVarValue(
97           Map.Entry<String, Resource> resourceEntry, String fileName,
98           GlobalValidationContext globalContext) {
99     Object indexVar =
100             resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
101     if (indexVar == null) {
102       return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
103     }
104
105     if (indexVar instanceof String) {
106       return Optional.of((String) indexVar);
107     } else {
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();
115     }
116   }
117
118   private static void handleNestedResourceType(String fileName, String resourceName,
119                                                Resource resource, Optional<String> indexVarValue,
120                                                GlobalValidationContext globalContext) {
121
122     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
123
124     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
125             globalContext);
126     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
127
128     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
129   }
130
131   private static void validateAllPropertiesMatchNestedParameters(String fileName,
132                                                                  String resourceName,
133                                                                  Resource resource,
134                                                                  Optional<String> indexVarValue,
135                                                                  GlobalValidationContext
136                                                                          globalContext) {
137
138     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
139
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,
148                          propertiesNames,
149                          globalContext);
150         globalContext.setMessageCode(ERROR_CODE_HRR5);
151         HeatValidationService
152                 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
153                         indexVarValue, globalContext);
154       }
155     } else {
156       globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
157                       .getErrorWithParameters(
158                               ERROR_CODE_HRR2, Messages.MISSING_NESTED_FILE.getErrorMessage(),
159                               resourceType),
160               LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
161               LoggerErrorDescription.MISSING_FILE);
162     }
163
164     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
165   }
166
167   private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
168                                                      GlobalValidationContext globalContext) {
169
170     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
171
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);
180     }
181
182     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
183   }
184 }