re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / heatresource / NovaServerResourceValidator.java
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.*;
26 import org.openecomp.sdc.validation.ResourceValidator;
27 import org.openecomp.sdc.validation.ValidationContext;
28 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
29
30 import java.util.Map;
31
32 public class NovaServerResourceValidator implements ResourceValidator {
33   private static final ErrorMessageCode ERROR_CODE_HNS1 = new ErrorMessageCode("HNS1");
34   private static final ErrorMessageCode ERROR_CODE_HNS2 = new ErrorMessageCode("HNS2");
35
36   @Override
37   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
38                        GlobalValidationContext globalContext, ValidationContext validationContext) {
39
40     HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext)
41             validationContext;
42     validateNovaServerResourceType (fileName,
43             resourceEntry, heatResourceValidationContext, globalContext );
44   }
45
46   private static void validateNovaServerResourceType(String fileName,
47                                                      Map.Entry<String, Resource> resourceEntry,
48                                                      HeatResourceValidationContext heatResourceValidationContext,
49                                                      GlobalValidationContext globalContext) {
50     validateAssignedValueForImageOrFlavorFromNova(fileName, resourceEntry, globalContext);
51     validateAllServerGroupsPointedByServerExistAndDefined (fileName,
52             resourceEntry, heatResourceValidationContext.getHeatOrchestrationTemplate(), globalContext );
53   }
54
55   private static void validateAssignedValueForImageOrFlavorFromNova(String fileName,
56                                                                     Map.Entry<String, Resource>
57                                                                             resourceEntry,
58                                                                     GlobalValidationContext
59                                                                             globalContext) {
60     Resource resource = resourceEntry.getValue();
61     Map<String, Object> propertiesMap = resource.getProperties();
62     if (propertiesMap.get(PropertiesMapKeyTypes.IMAGE.getKeyMap()) == null
63             && propertiesMap.get(PropertiesMapKeyTypes.FLAVOR.getKeyMap()) == null) {
64       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
65                       .getErrorWithParameters(ERROR_CODE_HNS1, Messages.MISSING_IMAGE_AND_FLAVOR.getErrorMessage(),
66                               resourceEntry.getKey()));
67     }
68   }
69
70   @SuppressWarnings("unchecked")
71   private static void validateAllServerGroupsPointedByServerExistAndDefined(String fileName,
72                                                                             Map.Entry<String, Resource> resourceEntry,
73                                                                             HeatOrchestrationTemplate heatOrchestrationTemplate,
74                                                                             GlobalValidationContext globalContext) {
75     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
76     Map<String, Object> resourceProperties = resourceEntry.getValue().getProperties();
77     Map<String, Object> schedulerHintsMap =
78             resourceProperties == null ? null : (Map<String, Object>) resourceProperties.get(
79                     ResourceReferenceFunctions.SCHEDULER_HINTS.getFunction());
80
81     if (MapUtils.isEmpty(schedulerHintsMap)) {
82       return;
83     }
84
85     validateServerGroupValue(fileName, resourceEntry, globalContext, resourcesMap, schedulerHintsMap);
86   }
87
88   private static void validateServerGroupValue(String fileName, Map.Entry<String,
89                   Resource> resourceEntry, GlobalValidationContext globalContext,
90                   Map<String, Resource> resourcesMap, Map<String, Object> schedulerHintsMap) {
91     if (schedulerHintsMap != null) {
92       for (Object serverGroupValue : schedulerHintsMap.values()) {
93         if (!(serverGroupValue instanceof Map)) {
94           continue;
95         }
96         Map<String, Object> currentServerMap = (Map<String, Object>) serverGroupValue;
97         String serverResourceName = (String) currentServerMap
98                 .get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
99         Resource serverResource =
100                 serverResourceName == null || resourcesMap == null ? null
101                         : resourcesMap.get(serverResourceName);
102
103         if (serverResource != null && !serverResource.getType()
104                 .equals(HeatResourcesTypes.NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource())) {
105           globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
106                           .getErrorWithParameters(ERROR_CODE_HNS2,
107                                   Messages.SERVER_NOT_DEFINED_FROM_NOVA.getErrorMessage(),
108                                   serverResourceName, resourceEntry.getKey()));
109         }
110       }
111     }
112   }
113
114 }