4f4091f506599bf0611ce579d1309a3ab0c5ba24
[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.validation.ResourceValidator;
31 import org.openecomp.sdc.validation.ValidationContext;
32 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
33
34 import java.util.Map;
35
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");
39
40   @Override
41   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
42                        GlobalValidationContext globalContext, ValidationContext validationContext) {
43
44     HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
45             validationContext;
46     validateNovaServerResourceType (fileName,
47             resourceEntry, heatResourceValidationContext, globalContext );
48   }
49
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 );
57   }
58
59   private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
60                                                                     Map.Entry<String, Resource>
61                                                                             resourceEntry,
62                                                                     GlobalValidationContext
63                                                                             globalContext) {
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()));
71     }
72   }
73
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());
84
85     if (MapUtils.isEmpty(schedulerHintsMap)) {
86       return;
87     }
88
89     validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
90   }
91
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)) {
98           continue;
99         }
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);
106
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()));
113         }
114       }
115     }
116   }
117
118 }