a9cbbe565e7cf6a1b987b4feb4f8f7eda165908f
[sdc.git] /
1 package org.openecomp.sdc.validation.impl.validators.heatresource;
2
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;
19
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23
24 /**
25  * Created by TALIO on 2/22/2017.
26  */
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");
32
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);
38   }
39
40   @SuppressWarnings("unchecked")
41   private static void validateNovaServerGroupPolicy(String fileName,
42                                                     Map.Entry<String, Resource> resourceEntry,
43                                                     GlobalValidationContext globalContext) {
44
45     mdcDataDebugMessage.debugEntryMessage("file", fileName);
46
47     Resource resource = resourceEntry.getValue();
48     Object policies =
49             resource.getProperties() == null ? null : resource.getProperties().get("policies");
50
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);
62         }
63       } else {
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);
70       }
71     }
72
73     mdcDataDebugMessage.debugExitMessage("file", fileName);
74   }
75
76   private static boolean isGivenPolicyValid(Object policy) {
77     if (policy instanceof Map) {
78       return true;
79     }
80     if (policy instanceof String) {
81       return PolicyTypes.isGivenPolicyValid((String) policy);
82     }
83     return false;
84   }
85
86   public void validateServerGroupIsUsed(String fileName,
87                                         Map.Entry<String, Resource> resourceEntry,
88                                         GlobalValidationContext globalContext,
89                                         HeatResourceValidationContext validationContext) {
90
91     Map<String, Map<String, List<String>>> pointedServerGroups =
92             validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
93                     .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
94
95     if (MapUtils.isEmpty(pointedServerGroups)) {
96       globalContext
97               .addMessage(
98                       fileName,
99                       ErrorLevel.WARNING,
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);
106       return;
107     }
108
109     handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
110   }
111
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());
117
118     if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
119       globalContext
120               .addMessage(
121                       fileName,
122                       ErrorLevel.WARNING,
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);
129     }
130
131   }
132 }