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.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.validation.ResourceValidator;
34 import org.openecomp.sdc.validation.ValidationContext;
35 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
39 public class NovaServerResourceValidator implements ResourceValidator {
40 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
41 private static final ErrorMessageCode ERROR_CODE_HNS1 = new ErrorMessageCode("HNS1");
42 private static final ErrorMessageCode ERROR_CODE_HNS2 = new ErrorMessageCode("HNS2");
45 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46 GlobalValidationContext globalContext, ValidationContext validationContext) {
48 HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
50 validateNovaServerResourceType (fileName,
51 resourceEntry, heatResourceValidationContext, globalContext );
54 private static void validateNovaServerResourceType(String fileName,
55 Map.Entry<String, Resource> resourceEntry,
56 HeatResourceValidationContext heatResourceValidationContext,
57 GlobalValidationContext globalContext) {
59 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
61 validateAssignedValueForImageOrFlavorFromNova(fileName, resourceEntry, globalContext);
62 validateAllServerGroupsPointedByServerExistAndDefined (fileName,
63 resourceEntry, heatResourceValidationContext.getHeatOrchestrationTemplate(), globalContext );
65 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
69 private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
70 Map.Entry<String, Resource>
72 GlobalValidationContext
75 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
77 Resource resource = resourceEntry.getValue();
78 Map<String, Object> propertiesMap = resource.getProperties();
79 if (propertiesMap.get(PropertiesMapKeyTypes.IMAGE.getKeyMap()) == null
80 && propertiesMap.get(PropertiesMapKeyTypes.FLAVOR.getKeyMap()) == null) {
81 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
82 .getErrorWithParameters(ERROR_CODE_HNS1, Messages.MISSING_IMAGE_AND_FLAVOR.getErrorMessage(),
83 resourceEntry.getKey()),
84 LoggerTragetServiceName.VALIDATE_ASSIGNED_VALUES_FOR_NOVA_IMAGE_FLAVOR,
85 LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
88 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
91 @SuppressWarnings("unchecked")
92 private static void validateAllServerGroupsPointedByServerExistAndDefined(String fileName,
93 Map.Entry<String, Resource> resourceEntry,
94 HeatOrchestrationTemplate heatOrchestrationTemplate,
95 GlobalValidationContext globalContext) {
97 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
99 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
100 Map<String, Object> resourceProperties = resourceEntry.getValue().getProperties();
101 Map<String, Object> schedulerHintsMap =
102 resourceProperties == null ? null : (Map<String, Object>) resourceProperties.get(
103 ResourceReferenceFunctions.SCHEDULER_HINTS.getFunction());
105 if (MapUtils.isEmpty(schedulerHintsMap)) {
109 validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
111 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
114 private static void validateServerGroupValue(String fileName, Map.Entry<String,
115 Resource> resourceEntry, GlobalValidationContext globalContext,
116 Map<String, Resource> resourcesMap, Map<String, Object> schedulerHintsMap) {
117 if (schedulerHintsMap != null) {
118 for (Object serverGroupValue : schedulerHintsMap.values()) {
119 if (!(serverGroupValue instanceof Map)) {
122 Map<String, Object> currentServerMap = (Map<String, Object>) serverGroupValue;
123 String serverResourceName = (String) currentServerMap
124 .get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
125 Resource serverResource =
126 serverResourceName == null || resourcesMap == null ? null
127 : resourcesMap.get(serverResourceName);
129 if (serverResource != null && !serverResource.getType()
130 .equals(HeatResourcesTypes.NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource())) {
131 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
132 .getErrorWithParameters(ERROR_CODE_HNS2, Messages.SERVER_NOT_DEFINED_FROM_NOVA.getErrorMessage(),
133 serverResourceName, resourceEntry.getKey()),
134 LoggerTragetServiceName.VALIDATE_SERVER_GROUP_EXISTENCE,
135 LoggerErrorDescription.SERVER_NOT_DEFINED_NOVA);