re base code
[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 @Override
56   public void init(Map<String, Object> properties) {
57     if (MapUtils.isEmpty(properties)) {
58       return;
59     }
60
61     properties.entrySet().stream()
62         .filter(entry -> getImplementationConfigurationFromProperties(entry.getValue()) != null)
63         .forEach(entry -> resourceTypeToImpl
64             .put(entry.getKey(), getImplementationConfigurationFromProperties(entry.getValue())));
65   }
66
67   @Override
68   public void validate(GlobalValidationContext globalContext) {
69     ManifestContent manifestContent;
70     try {
71       manifestContent = ValidationUtil.validateManifest(globalContext);
72     } catch (Exception exception) {
73       LOGGER.error("Failed to validate manifest file", exception);
74       return;
75     }
76
77     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
78     Map<String, FileData> fileEnvMap = ManifestUtil.getFileAndItsEnv(manifestContent);
79     globalContext.getFiles().stream()
80         .filter(fileName -> FileData
81             .isHeatFile(fileTypeMap.get(fileName)))
82         .forEach(fileName -> validate(fileName,
83             fileEnvMap.get(fileName) != null ? fileEnvMap.get(fileName).getFile() : null,
84             globalContext));
85   }
86
87   private void validate(String fileName, String envFileName,
88                         GlobalValidationContext globalContext) {
89     globalContext.setMessageCode(ERROR_CODE_RBV_2);
90     HeatOrchestrationTemplate heatOrchestrationTemplate =
91         ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext);
92     if (heatOrchestrationTemplate == null) {
93       return;
94     }
95
96     ValidationContext validationContext =
97         createValidationContext(fileName, envFileName, heatOrchestrationTemplate, globalContext);
98
99     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
100     if (MapUtils.isEmpty(resourcesMap)) {
101       return;
102     }
103
104     for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
105       String resourceType = resourceEntry.getValue().getType();
106
107       if (Objects.isNull(resourceType)) {
108         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
109                 .getErrorWithParameters(ERROR_CODE_RBV_1,
110                         Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),"null",
111                     resourceEntry.getKey()));
112       } else {
113         ResourceValidator
114             resourceValidatorImpl = getResourceValidatorInstance(resourceType, resourceTypeToImpl);
115         if (Objects.nonNull(resourceValidatorImpl)) {
116           resourceValidatorImpl.validate(fileName, resourceEntry, globalContext,
117               validationContext);
118         }
119       }
120     }
121   }
122
123   public ValidationContext createValidationContext(String fileName,
124                                                       String envFileName,
125                                                       HeatOrchestrationTemplate heatOrchestrationTemplate,
126                                                       GlobalValidationContext globalContext) {
127     return null;
128   }
129
130   private static boolean isSupportedResourceType(String resourceType,
131                                                  Map<String, ImplementationConfiguration> resourceTypeToImpl) {
132     return resourceTypeToImpl.containsKey(resourceType) && resourceTypeToImpl.get(resourceType)
133         .isEnable();
134
135   }
136
137   private static ResourceValidator getResourceValidatorInstance(String resourceType,
138                                                                 Map<String, ImplementationConfiguration> resourceTypeToImpl) {
139     ResourceValidator resourceBaseValidator = null;
140     if (isSupportedResourceType(resourceType, resourceTypeToImpl)) {
141       return getValidatorImpl(resourceType, resourceTypeToImpl);
142     }
143     if (HeatStructureUtil.isNestedResource(resourceType)) {
144       return getValidatorImpl("nestedResource", resourceTypeToImpl);
145     }
146     return resourceBaseValidator;
147   }
148
149   private static ResourceValidator getValidatorImpl(String resourceType,
150                                                     Map<String, ImplementationConfiguration> resourceTypeToImpl) {
151     String implementationClass = resourceTypeToImpl.get(resourceType) != null ?
152         resourceTypeToImpl.get(resourceType).getImplementationClass() : null;
153     return implementationClass == null ? null : CommonMethods
154         .newInstance(implementationClass, ResourceValidator.class);
155   }
156
157   private ImplementationConfiguration getImplementationConfigurationFromProperties(Object value) {
158     ImplementationConfiguration implementationConfiguration = new ImplementationConfiguration();
159
160     if (!(value instanceof Map)) {
161       return null;
162     }
163
164     Map<String, Object> valueAsMap = (Map<String, Object>) value;
165     if (!(valueAsMap.containsKey(ConfigConstants.Impl_Class))) {
166       return null;
167     }
168
169     implementationConfiguration.setImplementationClass(
170         valueAsMap.get(ConfigConstants.Impl_Class).toString());
171     if (valueAsMap.containsKey(ConfigConstants.Enable)) {
172       implementationConfiguration.setEnable(Boolean.
173           valueOf(valueAsMap.get(ConfigConstants.Enable).toString()));
174     }
175
176     return implementationConfiguration;
177   }
178 }