2 * Copyright © 2018 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.base;
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;
41 import java.util.HashMap;
43 import java.util.Objects;
46 * Created by TALIO on 2/16/2017.
48 public class ResourceBaseValidator implements Validator {
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");
56 public void init(Map<String, Object> properties) {
57 if (MapUtils.isEmpty(properties)) {
61 properties.entrySet().stream()
62 .filter(entry -> getImplementationConfigurationFromProperties(entry.getValue()) != null)
63 .forEach(entry -> resourceTypeToImpl
64 .put(entry.getKey(), getImplementationConfigurationFromProperties(entry.getValue())));
68 public void validate(GlobalValidationContext globalContext) {
69 ManifestContent manifestContent;
71 manifestContent = ValidationUtil.validateManifest(globalContext);
72 } catch (Exception exception) {
73 LOGGER.error("Failed to validate manifest file", exception);
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,
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) {
96 ValidationContext validationContext =
97 createValidationContext(fileName, envFileName, heatOrchestrationTemplate, globalContext);
99 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
100 if (MapUtils.isEmpty(resourcesMap)) {
104 for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
105 String resourceType = resourceEntry.getValue().getType();
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()));
114 resourceValidatorImpl = getResourceValidatorInstance(resourceType, resourceTypeToImpl);
115 if (Objects.nonNull(resourceValidatorImpl)) {
116 resourceValidatorImpl.validate(fileName, resourceEntry, globalContext,
123 public ValidationContext createValidationContext(String fileName,
125 HeatOrchestrationTemplate heatOrchestrationTemplate,
126 GlobalValidationContext globalContext) {
130 private static boolean isSupportedResourceType(String resourceType,
131 Map<String, ImplementationConfiguration> resourceTypeToImpl) {
132 return resourceTypeToImpl.containsKey(resourceType) && resourceTypeToImpl.get(resourceType)
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);
143 if (HeatStructureUtil.isNestedResource(resourceType)) {
144 return getValidatorImpl("nestedResource", resourceTypeToImpl);
146 return resourceBaseValidator;
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);
157 private ImplementationConfiguration getImplementationConfigurationFromProperties(Object value) {
158 ImplementationConfiguration implementationConfiguration = new ImplementationConfiguration();
160 if (!(value instanceof Map)) {
164 Map<String, Object> valueAsMap = (Map<String, Object>) value;
165 if (!(valueAsMap.containsKey(ConfigConstants.Impl_Class))) {
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()));
176 return implementationConfiguration;