2 * Copyright © 2016-2017 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.impl.validators.heatresource;
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.PropertiesMapKeyTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.validation.ResourceValidator;
33 import org.openecomp.sdc.validation.ValidationContext;
34 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
38 public class NovaServerResourceValidator implements ResourceValidator {
39 private static final ErrorMessageCode ERROR_CODE_HNS1 = new ErrorMessageCode("HNS1");
40 private static final ErrorMessageCode ERROR_CODE_HNS2 = new ErrorMessageCode("HNS2");
43 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
44 GlobalValidationContext globalContext, ValidationContext validationContext) {
46 HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
48 validateNovaServerResourceType (fileName,
49 resourceEntry, heatResourceValidationContext, globalContext );
52 private static void validateNovaServerResourceType(String fileName,
53 Map.Entry<String, Resource> resourceEntry,
54 HeatResourceValidationContext heatResourceValidationContext,
55 GlobalValidationContext globalContext) {
56 validateAssignedValueForImageOrFlavorFromNova(fileName, resourceEntry, globalContext);
57 validateAllServerGroupsPointedByServerExistAndDefined (fileName,
58 resourceEntry, heatResourceValidationContext.getHeatOrchestrationTemplate(), globalContext );
61 private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
62 Map.Entry<String, Resource>
64 GlobalValidationContext
66 Resource resource = resourceEntry.getValue();
67 Map<String, Object> propertiesMap = resource.getProperties();
68 if (propertiesMap.get(PropertiesMapKeyTypes.IMAGE.getKeyMap()) == null
69 && propertiesMap.get(PropertiesMapKeyTypes.FLAVOR.getKeyMap()) == null) {
70 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
71 .getErrorWithParameters(ERROR_CODE_HNS1, Messages.MISSING_IMAGE_AND_FLAVOR.getErrorMessage(),
72 resourceEntry.getKey()),
73 LoggerTragetServiceName.VALIDATE_ASSIGNED_VALUES_FOR_NOVA_IMAGE_FLAVOR,
74 LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
78 @SuppressWarnings("unchecked")
79 private static void validateAllServerGroupsPointedByServerExistAndDefined(String fileName,
80 Map.Entry<String, Resource> resourceEntry,
81 HeatOrchestrationTemplate heatOrchestrationTemplate,
82 GlobalValidationContext globalContext) {
83 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
84 Map<String, Object> resourceProperties = resourceEntry.getValue().getProperties();
85 Map<String, Object> schedulerHintsMap =
86 resourceProperties == null ? null : (Map<String, Object>) resourceProperties.get(
87 ResourceReferenceFunctions.SCHEDULER_HINTS.getFunction());
89 if (MapUtils.isEmpty(schedulerHintsMap)) {
93 validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
96 private static void validateServerGroupValue(String fileName, Map.Entry<String,
97 Resource> resourceEntry, GlobalValidationContext globalContext,
98 Map<String, Resource> resourcesMap, Map<String, Object> schedulerHintsMap) {
99 if (schedulerHintsMap != null) {
100 for (Object serverGroupValue : schedulerHintsMap.values()) {
101 if (!(serverGroupValue instanceof Map)) {
104 Map<String, Object> currentServerMap = (Map<String, Object>) serverGroupValue;
105 String serverResourceName = (String) currentServerMap
106 .get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
107 Resource serverResource =
108 serverResourceName == null || resourcesMap == null ? null
109 : resourcesMap.get(serverResourceName);
111 if (serverResource != null && !serverResource.getType()
112 .equals(HeatResourcesTypes.NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource())) {
113 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
114 .getErrorWithParameters(ERROR_CODE_HNS2, Messages.SERVER_NOT_DEFINED_FROM_NOVA.getErrorMessage(),
115 serverResourceName, resourceEntry.getKey()),
116 LoggerTragetServiceName.VALIDATE_SERVER_GROUP_EXISTENCE,
117 LoggerErrorDescription.SERVER_NOT_DEFINED_NOVA);