1 package org.openecomp.sdc.validation.impl.validators.heatresource;
3 import org.apache.commons.collections4.MapUtils;
4 import org.openecomp.core.validation.ErrorMessageCode;
5 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
6 import org.openecomp.core.validation.types.GlobalValidationContext;
7 import org.openecomp.sdc.common.errors.Messages;
8 import org.openecomp.sdc.datatypes.error.ErrorLevel;
9 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
10 import org.openecomp.sdc.heat.datatypes.model.PolicyTypes;
11 import org.openecomp.sdc.heat.datatypes.model.Resource;
12 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
13 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
14 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
15 import org.openecomp.sdc.validation.ResourceValidator;
16 import org.openecomp.sdc.validation.ValidationContext;
17 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
18 import org.openecomp.sdc.validation.type.ValidatorConstants;
20 import java.util.List;
22 import java.util.Objects;
25 * Created by TALIO on 2/22/2017.
27 public class NovaServerGroupResourceValidator implements ResourceValidator {
28 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
29 private static final ErrorMessageCode ERROR_CODE_HNG1 = new ErrorMessageCode("HNG1");
30 private static final ErrorMessageCode ERROR_CODE_HNG2 = new ErrorMessageCode("HNG2");
31 private static final ErrorMessageCode ERROR_CODE_HNG3 = new ErrorMessageCode("HNG3");
33 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
34 GlobalValidationContext globalContext, ValidationContext validationContext) {
35 validateNovaServerGroupPolicy(fileName, resourceEntry, globalContext);
36 validateServerGroupIsUsed(fileName, resourceEntry, globalContext,
37 (HeatResourceValidationContext) validationContext);
40 @SuppressWarnings("unchecked")
41 private static void validateNovaServerGroupPolicy(String fileName,
42 Map.Entry<String, Resource> resourceEntry,
43 GlobalValidationContext globalContext) {
45 mdcDataDebugMessage.debugEntryMessage("file", fileName);
47 Resource resource = resourceEntry.getValue();
49 resource.getProperties() == null ? null : resource.getProperties().get("policies");
51 if (Objects.nonNull(policies) && policies instanceof List) {
52 List<Object> policiesList = (List<Object>) policies;
53 if (policiesList.size() == 1) {
54 Object policy = policiesList.get(0);
55 if (!isGivenPolicyValid(policy)) {
56 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
57 .getErrorWithParameters(
58 ERROR_CODE_HNG1, Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
59 resourceEntry.getKey()),
60 LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
61 LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
64 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
65 .getErrorWithParameters(ERROR_CODE_HNG1,
66 Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
67 resourceEntry.getKey()),
68 LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
69 LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
73 mdcDataDebugMessage.debugExitMessage("file", fileName);
76 private static boolean isGivenPolicyValid(Object policy) {
77 if (policy instanceof Map) {
80 if (policy instanceof String) {
81 return PolicyTypes.isGivenPolicyValid((String) policy);
86 public void validateServerGroupIsUsed(String fileName,
87 Map.Entry<String, Resource> resourceEntry,
88 GlobalValidationContext globalContext,
89 HeatResourceValidationContext validationContext) {
91 Map<String, Map<String, List<String>>> pointedServerGroups =
92 validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
93 .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
95 if (MapUtils.isEmpty(pointedServerGroups)) {
100 ErrorMessagesFormatBuilder
101 .getErrorWithParameters(
102 ERROR_CODE_HNG2, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
103 ValidatorConstants.Server_Group, resourceEntry.getKey()),
104 LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
105 LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
109 handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
112 private void handleServerGroupReferences(String fileName, Map.Entry<String, Resource>
113 resourceEntry, Map<String, Map<String, List<String>>> pointedServerGroups,
114 GlobalValidationContext globalContext) {
115 Map<String, List<String>> resourcesPointingToCurrServerGroup =
116 pointedServerGroups.get(resourceEntry.getKey());
118 if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
123 ErrorMessagesFormatBuilder
124 .getErrorWithParameters(
125 ERROR_CODE_HNG3, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
126 ValidatorConstants.Server_Group, resourceEntry.getKey()),
127 LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
128 LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);