72226d96f4263c86fc77015987f590839fd1bd32
[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.HeatResourcesTypes;
26 import org.openecomp.sdc.heat.datatypes.model.PolicyTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.validation.ResourceValidator;
29 import org.openecomp.sdc.validation.ValidationContext;
30 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
31 import org.openecomp.sdc.validation.type.ValidatorConstants;
32
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Objects;
36
37 public class NovaServerGroupResourceValidator implements ResourceValidator {
38   private static final ErrorMessageCode ERROR_CODE_HNG1 = new ErrorMessageCode("HNG1");
39   private static final ErrorMessageCode ERROR_CODE_HNG2 = new ErrorMessageCode("HNG2");
40   private static final ErrorMessageCode ERROR_CODE_HNG3 = new ErrorMessageCode("HNG3");
41
42   @Override
43   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
44                        GlobalValidationContext globalContext, ValidationContext validationContext) {
45     validateNovaServerGroupPolicy(fileName, resourceEntry, globalContext);
46     validateServerGroupIsUsed(fileName, resourceEntry, globalContext,
47             (HeatResourceValidationContext) validationContext);
48   }
49
50   @SuppressWarnings("unchecked")
51   private static void validateNovaServerGroupPolicy(String fileName,
52                                                     Map.Entry<String, Resource> resourceEntry,
53                                                     GlobalValidationContext globalContext) {
54     Resource resource = resourceEntry.getValue();
55     Object policies =
56             resource.getProperties() == null ? null : resource.getProperties().get("policies");
57
58     if (Objects.nonNull(policies) && policies instanceof List) {
59       List<Object> policiesList = (List<Object>) policies;
60       if (policiesList.size() == 1) {
61         Object policy = policiesList.get(0);
62         if (!isGivenPolicyValid(policy)) {
63           globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
64                           .getErrorWithParameters(
65                                   ERROR_CODE_HNG1, Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
66                                   resourceEntry.getKey()));
67         }
68       } else {
69         globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
70                         .getErrorWithParameters(ERROR_CODE_HNG1,
71                                 Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
72                                 resourceEntry.getKey()));
73       }
74     }
75   }
76
77   private static boolean isGivenPolicyValid(Object policy) {
78     if (policy instanceof Map) {
79       return true;
80     }
81     if (policy instanceof String) {
82       return PolicyTypes.isGivenPolicyValid((String) policy);
83     }
84     return false;
85   }
86
87   public void validateServerGroupIsUsed(String fileName,
88                                         Map.Entry<String, Resource> resourceEntry,
89                                         GlobalValidationContext globalContext,
90                                         HeatResourceValidationContext validationContext) {
91
92     Map<String, Map<String, List<String>>> pointedServerGroups =
93             validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
94                     .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
95
96     if (MapUtils.isEmpty(pointedServerGroups)) {
97       globalContext
98               .addMessage(
99                       fileName,
100                       ErrorLevel.WARNING,
101                       ErrorMessagesFormatBuilder
102                               .getErrorWithParameters(
103                                       ERROR_CODE_HNG2, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
104                                       ValidatorConstants.Server_Group, resourceEntry.getKey()));
105       return;
106     }
107
108     handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
109   }
110
111   private void handleServerGroupReferences(String fileName, Map.Entry<String, Resource>
112           resourceEntry, Map<String, Map<String, List<String>>> pointedServerGroups,
113                                            GlobalValidationContext globalContext) {
114     Map<String, List<String>> resourcesPointingToCurrServerGroup =
115             pointedServerGroups.get(resourceEntry.getKey());
116
117     if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
118       globalContext
119               .addMessage(
120                       fileName,
121                       ErrorLevel.WARNING,
122                       ErrorMessagesFormatBuilder
123                               .getErrorWithParameters(
124                                       ERROR_CODE_HNG3, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
125                                       ValidatorConstants.Server_Group, resourceEntry.getKey()));
126     }
127
128   }
129 }