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.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.validation.ResourceValidator;
31 import org.openecomp.sdc.validation.ValidationContext;
32 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
33 import org.openecomp.sdc.validation.type.ValidatorConstants;
35 import java.util.List;
37 import java.util.Objects;
39 public class NovaServerGroupResourceValidator implements ResourceValidator {
40 private static final ErrorMessageCode ERROR_CODE_HNG1 = new ErrorMessageCode("HNG1");
41 private static final ErrorMessageCode ERROR_CODE_HNG2 = new ErrorMessageCode("HNG2");
42 private static final ErrorMessageCode ERROR_CODE_HNG3 = new ErrorMessageCode("HNG3");
45 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46 GlobalValidationContext globalContext, ValidationContext validationContext) {
47 validateNovaServerGroupPolicy(fileName, resourceEntry, globalContext);
48 validateServerGroupIsUsed(fileName, resourceEntry, globalContext,
49 (HeatResourceValidationContext) validationContext);
52 @SuppressWarnings("unchecked")
53 private static void validateNovaServerGroupPolicy(String fileName,
54 Map.Entry<String, Resource> resourceEntry,
55 GlobalValidationContext globalContext) {
56 Resource resource = resourceEntry.getValue();
58 resource.getProperties() == null ? null : resource.getProperties().get("policies");
60 if (Objects.nonNull(policies) && policies instanceof List) {
61 List<Object> policiesList = (List<Object>) policies;
62 if (policiesList.size() == 1) {
63 Object policy = policiesList.get(0);
64 if (!isGivenPolicyValid(policy)) {
65 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
66 .getErrorWithParameters(
67 ERROR_CODE_HNG1, Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
68 resourceEntry.getKey()),
69 LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
70 LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
73 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
74 .getErrorWithParameters(ERROR_CODE_HNG1,
75 Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
76 resourceEntry.getKey()),
77 LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
78 LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
83 private static boolean isGivenPolicyValid(Object policy) {
84 if (policy instanceof Map) {
87 if (policy instanceof String) {
88 return PolicyTypes.isGivenPolicyValid((String) policy);
93 public void validateServerGroupIsUsed(String fileName,
94 Map.Entry<String, Resource> resourceEntry,
95 GlobalValidationContext globalContext,
96 HeatResourceValidationContext validationContext) {
98 Map<String, Map<String, List<String>>> pointedServerGroups =
99 validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
100 .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
102 if (MapUtils.isEmpty(pointedServerGroups)) {
107 ErrorMessagesFormatBuilder
108 .getErrorWithParameters(
109 ERROR_CODE_HNG2, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
110 ValidatorConstants.Server_Group, resourceEntry.getKey()),
111 LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
112 LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
116 handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
119 private void handleServerGroupReferences(String fileName, Map.Entry<String, Resource>
120 resourceEntry, Map<String, Map<String, List<String>>> pointedServerGroups,
121 GlobalValidationContext globalContext) {
122 Map<String, List<String>> resourcesPointingToCurrServerGroup =
123 pointedServerGroups.get(resourceEntry.getKey());
125 if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
130 ErrorMessagesFormatBuilder
131 .getErrorWithParameters(
132 ERROR_CODE_HNG3, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
133 ValidatorConstants.Server_Group, resourceEntry.getKey()),
134 LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
135 LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);