b6bbeb5172a5cd6da9ee0a78a6a61896d150ea39
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
18
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;
35
36 import java.util.Map;
37
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");
41
42   @Override
43   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
44                        GlobalValidationContext globalContext, ValidationContext validationContext) {
45
46     HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
47             validationContext;
48     validateNovaServerResourceType (fileName,
49             resourceEntry, heatResourceValidationContext, globalContext );
50   }
51
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 );
59   }
60
61   private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
62                                                                     Map.Entry<String, Resource>
63                                                                             resourceEntry,
64                                                                     GlobalValidationContext
65                                                                             globalContext) {
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);
75     }
76   }
77
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());
88
89     if (MapUtils.isEmpty(schedulerHintsMap)) {
90       return;
91     }
92
93     validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
94   }
95
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)) {
102           continue;
103         }
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);
110
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);
118         }
119       }
120     }
121   }
122
123 }