2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
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;
33 import java.util.List;
35 import java.util.Objects;
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");
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);
50 @SuppressWarnings("unchecked")
51 private static void validateNovaServerGroupPolicy(String fileName,
52 Map.Entry<String, Resource> resourceEntry,
53 GlobalValidationContext globalContext) {
54 Resource resource = resourceEntry.getValue();
56 resource.getProperties() == null ? null : resource.getProperties().get("policies");
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()));
69 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
70 .getErrorWithParameters(ERROR_CODE_HNG1,
71 Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
72 resourceEntry.getKey()));
77 private static boolean isGivenPolicyValid(Object policy) {
78 if (policy instanceof Map) {
81 if (policy instanceof String) {
82 return PolicyTypes.isGivenPolicyValid((String) policy);
87 public void validateServerGroupIsUsed(String fileName,
88 Map.Entry<String, Resource> resourceEntry,
89 GlobalValidationContext globalContext,
90 HeatResourceValidationContext validationContext) {
92 Map<String, Map<String, List<String>>> pointedServerGroups =
93 validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
94 .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
96 if (MapUtils.isEmpty(pointedServerGroups)) {
101 ErrorMessagesFormatBuilder
102 .getErrorWithParameters(
103 ERROR_CODE_HNG2, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
104 ValidatorConstants.Server_Group, resourceEntry.getKey()));
108 handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
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());
117 if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
122 ErrorMessagesFormatBuilder
123 .getErrorWithParameters(
124 ERROR_CODE_HNG3, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
125 ValidatorConstants.Server_Group, resourceEntry.getKey()));