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