test coverage
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-sdk / src / main / java / org / openecomp / sdc / validation / base / ResourceBaseValidator.java
1 /*
2  * Copyright © 2018 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.base;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.utilities.CommonMethods;
21 import org.openecomp.core.validation.ErrorMessageCode;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
28 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
29 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
30 import org.openecomp.sdc.heat.datatypes.model.Resource;
31 import org.openecomp.sdc.heat.services.HeatStructureUtil;
32 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
33 import org.openecomp.sdc.logging.api.Logger;
34 import org.openecomp.sdc.logging.api.LoggerFactory;
35 import org.openecomp.sdc.validation.ResourceValidator;
36 import org.openecomp.sdc.validation.ValidationContext;
37 import org.openecomp.sdc.validation.Validator;
38 import org.openecomp.sdc.validation.type.ConfigConstants;
39 import org.openecomp.sdc.validation.util.ValidationUtil;
40
41 import java.util.HashMap;
42 import java.util.Map;
43 import java.util.Objects;
44
45 /**
46  * Created by TALIO on 2/16/2017.
47  */
48 public class ResourceBaseValidator implements Validator {
49
50   protected final Map<String, ImplementationConfiguration> resourceTypeToImpl = new HashMap<>();
51   private static final Logger LOGGER = LoggerFactory.getLogger(ResourceBaseValidator.class);
52   private static final ErrorMessageCode ERROR_CODE_RBV_1 = new ErrorMessageCode("RBV1");
53   private static final ErrorMessageCode ERROR_CODE_RBV_2 = new ErrorMessageCode("RBV2");
54
55   Map<String, ImplementationConfiguration> getResourceTypeToImpl() {
56     return MapUtils.unmodifiableMap(this.resourceTypeToImpl);
57   }
58
59 @Override
60   public void init(Map<String, Object> properties) {
61     if (MapUtils.isEmpty(properties)) {
62       return;
63     }
64
65     properties.entrySet().stream()
66         .filter(entry -> getImplementationConfigurationFromProperties(entry.getValue()) != null)
67         .forEach(entry -> resourceTypeToImpl
68             .put(entry.getKey(), getImplementationConfigurationFromProperties(entry.getValue())));
69   }
70
71   @Override
72   public void validate(GlobalValidationContext globalContext) {
73     ManifestContent manifestContent;
74     try {
75       manifestContent = ValidationUtil.validateManifest(globalContext);
76     } catch (Exception exception) {
77       LOGGER.error("Failed to validate manifest file", exception);
78       return;
79     }
80
81     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
82     Map<String, FileData> fileEnvMap = ManifestUtil.getFileAndItsEnv(manifestContent);
83     globalContext.getFiles().stream()
84         .filter(fileName -> FileData
85             .isHeatFile(fileTypeMap.get(fileName)))
86         .forEach(fileName -> validate(fileName,
87             fileEnvMap.get(fileName) != null ? fileEnvMap.get(fileName).getFile() : null,
88             globalContext));
89   }
90
91   private void validate(String fileName, String envFileName,
92                         GlobalValidationContext globalContext) {
93     globalContext.setMessageCode(ERROR_CODE_RBV_2);
94     HeatOrchestrationTemplate heatOrchestrationTemplate =
95         ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext);
96     if (heatOrchestrationTemplate == null) {
97       return;
98     }
99
100     ValidationContext validationContext =
101         createValidationContext(fileName, envFileName, heatOrchestrationTemplate, globalContext);
102
103     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
104     if (MapUtils.isEmpty(resourcesMap)) {
105       return;
106     }
107
108     for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
109       String resourceType = resourceEntry.getValue().getType();
110
111       if (Objects.isNull(resourceType)) {
112         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
113                 .getErrorWithParameters(ERROR_CODE_RBV_1,
114                         Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),"null",
115                     resourceEntry.getKey()));
116       } else {
117         ResourceValidator
118             resourceValidatorImpl = getResourceValidatorInstance(resourceType, resourceTypeToImpl);
119         if (Objects.nonNull(resourceValidatorImpl)) {
120           resourceValidatorImpl.validate(fileName, resourceEntry, globalContext,
121               validationContext);
122         }
123       }
124     }
125   }
126
127   public ValidationContext createValidationContext(String fileName,
128                                                       String envFileName,
129                                                       HeatOrchestrationTemplate heatOrchestrationTemplate,
130                                                       GlobalValidationContext globalContext) {
131     return null;
132   }
133
134   private static boolean isSupportedResourceType(String resourceType,
135                                                  Map<String, ImplementationConfiguration> resourceTypeToImpl) {
136     return resourceTypeToImpl.containsKey(resourceType) && resourceTypeToImpl.get(resourceType)
137         .isEnable();
138
139   }
140
141   private static ResourceValidator getResourceValidatorInstance(String resourceType,
142                                                                 Map<String, ImplementationConfiguration> resourceTypeToImpl) {
143     ResourceValidator resourceBaseValidator = null;
144     if (isSupportedResourceType(resourceType, resourceTypeToImpl)) {
145       return getValidatorImpl(resourceType, resourceTypeToImpl);
146     }
147     if (HeatStructureUtil.isNestedResource(resourceType)) {
148       return getValidatorImpl("nestedResource", resourceTypeToImpl);
149     }
150     return resourceBaseValidator;
151   }
152
153   private static ResourceValidator getValidatorImpl(String resourceType,
154                                                     Map<String, ImplementationConfiguration> resourceTypeToImpl) {
155     String implementationClass = resourceTypeToImpl.get(resourceType) != null ?
156         resourceTypeToImpl.get(resourceType).getImplementationClass() : null;
157     return implementationClass == null ? null : CommonMethods
158         .newInstance(implementationClass, ResourceValidator.class);
159   }
160
161   private ImplementationConfiguration getImplementationConfigurationFromProperties(Object value) {
162     ImplementationConfiguration implementationConfiguration = new ImplementationConfiguration();
163
164     if (!(value instanceof Map)) {
165       return null;
166     }
167
168     Map<String, Object> valueAsMap = (Map<String, Object>) value;
169     if (!(valueAsMap.containsKey(ConfigConstants.Impl_Class))) {
170       return null;
171     }
172
173     implementationConfiguration.setImplementationClass(
174         valueAsMap.get(ConfigConstants.Impl_Class).toString());
175     if (valueAsMap.containsKey(ConfigConstants.Enable)) {
176       implementationConfiguration.setEnable(Boolean.
177           valueOf(valueAsMap.get(ConfigConstants.Enable).toString()));
178     }
179
180     return implementationConfiguration;
181   }
182 }