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.validation.ResourceValidator;
31 import org.openecomp.sdc.validation.ValidationContext;
32 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
36 public class NovaServerResourceValidator implements ResourceValidator {
37 private static final ErrorMessageCode ERROR_CODE_HNS1 = new ErrorMessageCode("HNS1");
38 private static final ErrorMessageCode ERROR_CODE_HNS2 = new ErrorMessageCode("HNS2");
41 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
42 GlobalValidationContext globalContext, ValidationContext validationContext) {
44 HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
46 validateNovaServerResourceType (fileName,
47 resourceEntry, heatResourceValidationContext, globalContext );
50 private static void validateNovaServerResourceType(String fileName,
51 Map.Entry<String, Resource> resourceEntry,
52 HeatResourceValidationContext heatResourceValidationContext,
53 GlobalValidationContext globalContext) {
54 validateAssignedValueForImageOrFlavorFromNova(fileName, resourceEntry, globalContext);
55 validateAllServerGroupsPointedByServerExistAndDefined (fileName,
56 resourceEntry, heatResourceValidationContext.getHeatOrchestrationTemplate(), globalContext );
59 private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
60 Map.Entry<String, Resource>
62 GlobalValidationContext
64 Resource resource = resourceEntry.getValue();
65 Map<String, Object> propertiesMap = resource.getProperties();
66 if (propertiesMap.get(PropertiesMapKeyTypes.IMAGE.getKeyMap()) == null
67 && propertiesMap.get(PropertiesMapKeyTypes.FLAVOR.getKeyMap()) == null) {
68 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
69 .getErrorWithParameters(ERROR_CODE_HNS1, Messages.MISSING_IMAGE_AND_FLAVOR.getErrorMessage(),
70 resourceEntry.getKey()));
74 @SuppressWarnings("unchecked")
75 private static void validateAllServerGroupsPointedByServerExistAndDefined(String fileName,
76 Map.Entry<String, Resource> resourceEntry,
77 HeatOrchestrationTemplate heatOrchestrationTemplate,
78 GlobalValidationContext globalContext) {
79 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
80 Map<String, Object> resourceProperties = resourceEntry.getValue().getProperties();
81 Map<String, Object> schedulerHintsMap =
82 resourceProperties == null ? null : (Map<String, Object>) resourceProperties.get(
83 ResourceReferenceFunctions.SCHEDULER_HINTS.getFunction());
85 if (MapUtils.isEmpty(schedulerHintsMap)) {
89 validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
92 private static void validateServerGroupValue(String fileName, Map.Entry<String,
93 Resource> resourceEntry, GlobalValidationContext globalContext,
94 Map<String, Resource> resourcesMap, Map<String, Object> schedulerHintsMap) {
95 if (schedulerHintsMap != null) {
96 for (Object serverGroupValue : schedulerHintsMap.values()) {
97 if (!(serverGroupValue instanceof Map)) {
100 Map<String, Object> currentServerMap = (Map<String, Object>) serverGroupValue;
101 String serverResourceName = (String) currentServerMap
102 .get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
103 Resource serverResource =
104 serverResourceName == null || resourcesMap == null ? null
105 : resourcesMap.get(serverResourceName);
107 if (serverResource != null && !serverResource.getType()
108 .equals(HeatResourcesTypes.NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource())) {
109 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
110 .getErrorWithParameters(ERROR_CODE_HNS2,
111 Messages.SERVER_NOT_DEFINED_FROM_NOVA.getErrorMessage(),
112 serverResourceName, resourceEntry.getKey()));