7d8ab251b9d8a06320e2c1959736f0b9f1eb6b0d
[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.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;
36
37 import java.util.Map;
38
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");
43
44   @Override
45   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46                        GlobalValidationContext globalContext, ValidationContext validationContext) {
47
48     HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
49             validationContext;
50     validateNovaServerResourceType (fileName,
51             resourceEntry, heatResourceValidationContext, globalContext );
52   }
53
54   private static void validateNovaServerResourceType(String fileName,
55                                                      Map.Entry<String, Resource> resourceEntry,
56                                                      HeatResourceValidationContext heatResourceValidationContext,
57                                                      GlobalValidationContext globalContext) {
58
59     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
60
61     validateAssignedValueForImageOrFlavorFromNova(fileName, resourceEntry, globalContext);
62     validateAllServerGroupsPointedByServerExistAndDefined (fileName,
63             resourceEntry, heatResourceValidationContext.getHeatOrchestrationTemplate(), globalContext );
64
65     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
66
67   }
68
69   private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
70                                                                     Map.Entry<String, Resource>
71                                                                             resourceEntry,
72                                                                     GlobalValidationContext
73                                                                             globalContext) {
74
75     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
76
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);
86     }
87
88     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
89   }
90
91   @SuppressWarnings("unchecked")
92   private static void validateAllServerGroupsPointedByServerExistAndDefined(String fileName,
93                                                                             Map.Entry<String, Resource> resourceEntry,
94                                                                             HeatOrchestrationTemplate heatOrchestrationTemplate,
95                                                                             GlobalValidationContext globalContext) {
96
97     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
98
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());
104
105     if (MapUtils.isEmpty(schedulerHintsMap)) {
106       return;
107     }
108
109     validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
110
111     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
112   }
113
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)) {
120           continue;
121         }
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);
128
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);
136         }
137       }
138     }
139   }
140
141 }