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");
55 Map<String, ImplementationConfiguration> getResourceTypeToImpl() {
56 return MapUtils.unmodifiableMap(this.resourceTypeToImpl);
60 public void init(Map<String, Object> properties) {
61 if (MapUtils.isEmpty(properties)) {
65 properties.entrySet().stream()
66 .filter(entry -> getImplementationConfigurationFromProperties(entry.getValue()) != null)
67 .forEach(entry -> resourceTypeToImpl
68 .put(entry.getKey(), getImplementationConfigurationFromProperties(entry.getValue())));
72 public void validate(GlobalValidationContext globalContext) {
73 ManifestContent manifestContent;
75 manifestContent = ValidationUtil.validateManifest(globalContext);
76 } catch (Exception exception) {
77 LOGGER.error("Failed to validate manifest file", exception);
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,
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) {
100 ValidationContext validationContext =
101 createValidationContext(fileName, envFileName, heatOrchestrationTemplate, globalContext);
103 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
104 if (MapUtils.isEmpty(resourcesMap)) {
108 for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
109 String resourceType = resourceEntry.getValue().getType();
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()));
118 resourceValidatorImpl = getResourceValidatorInstance(resourceType, resourceTypeToImpl);
119 if (Objects.nonNull(resourceValidatorImpl)) {
120 resourceValidatorImpl.validate(fileName, resourceEntry, globalContext,
127 public ValidationContext createValidationContext(String fileName,
129 HeatOrchestrationTemplate heatOrchestrationTemplate,
130 GlobalValidationContext globalContext) {
134 private static boolean isSupportedResourceType(String resourceType,
135 Map<String, ImplementationConfiguration> resourceTypeToImpl) {
136 return resourceTypeToImpl.containsKey(resourceType) && resourceTypeToImpl.get(resourceType)
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);
147 if (HeatStructureUtil.isNestedResource(resourceType)) {
148 return getValidatorImpl("nestedResource", resourceTypeToImpl);
150 return resourceBaseValidator;
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);
161 private ImplementationConfiguration getImplementationConfigurationFromProperties(Object value) {
162 ImplementationConfiguration implementationConfiguration = new ImplementationConfiguration();
164 if (!(value instanceof Map)) {
168 Map<String, Object> valueAsMap = (Map<String, Object>) value;
169 if (!(valueAsMap.containsKey(ConfigConstants.Impl_Class))) {
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()));
180 return implementationConfiguration;