1 package org.openecomp.sdc.validation.impl.validators.heatresource;
3 import org.apache.commons.collections4.CollectionUtils;
4 import org.apache.commons.collections4.MapUtils;
5 import org.openecomp.core.validation.ErrorMessageCode;
6 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
7 import org.openecomp.core.validation.types.GlobalValidationContext;
8 import org.openecomp.sdc.common.errors.Messages;
9 import org.openecomp.sdc.datatypes.error.ErrorLevel;
10 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
11 import org.openecomp.sdc.heat.datatypes.model.Resource;
12 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
13 import org.openecomp.sdc.heat.services.HeatStructureUtil;
14 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
15 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
16 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
17 import org.openecomp.sdc.validation.ResourceValidator;
18 import org.openecomp.sdc.validation.ValidationContext;
19 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
25 import java.util.Objects;
29 * Created by TALIO on 2/22/2017.
31 public class NeutronPortResourceValidator implements ResourceValidator {
32 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
33 private static ErrorMessageCode ERROR_HPRODE_HPR1 = new ErrorMessageCode("HPR1");
34 private static ErrorMessageCode ERROR_HPRODE_HPR2 = new ErrorMessageCode("HPR2");
35 private static ErrorMessageCode ERROR_HPRODE_HPR3 = new ErrorMessageCode("HPR3");
38 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
39 GlobalValidationContext globalContext, ValidationContext validationContext) {
41 validateNovaServerPortBinding
42 (fileName, resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
46 @SuppressWarnings("unchecked")
47 private static void validateNovaServerPortBinding(String fileName,
48 Map.Entry<String, Resource> resourceEntry,
49 HeatResourceValidationContext heatResourceValidationContext,
50 GlobalValidationContext globalContext) {
52 mdcDataDebugMessage.debugEntryMessage("file", fileName);
54 Map<String, Map<String, List<String>>> portIdToPointingResources =
55 heatResourceValidationContext.getFileLevelResourceDependencies()
56 .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
58 String portResourceId = resourceEntry.getKey();
59 if (MapUtils.isEmpty(portIdToPointingResources)) {
61 .addMessage(fileName, ErrorLevel.WARNING,
62 ErrorMessagesFormatBuilder
63 .getErrorWithParameters(
64 ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
65 portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
66 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
71 Map<String, List<String>> pointingResourcesToCurrPort =
72 portIdToPointingResources.get(portResourceId);
73 checkPortBindingFromMap(
74 fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
76 mdcDataDebugMessage.debugExitMessage("file", fileName);
79 private static void checkPortBindingFromMap(String fileName,
80 String portResourceId,
81 Map<String, List<String>> resourcesPointingToCurrPort,
82 GlobalValidationContext globalContext) {
83 List<String> pointingNovaServers =
84 MapUtils.isEmpty(resourcesPointingToCurrPort) ? new ArrayList<>()
85 : resourcesPointingToCurrPort.get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
87 handleErrorEventsForPortBinding(
88 fileName, portResourceId, globalContext, pointingNovaServers);
93 private static void handleErrorEventsForPortBinding(String fileName,
94 String portResourceId,
95 GlobalValidationContext globalContext,
96 List<String> pointingNovaServers) {
97 if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
99 .addMessage(fileName, ErrorLevel.ERROR,
100 ErrorMessagesFormatBuilder
101 .getErrorWithParameters(
102 ERROR_HPRODE_HPR2, Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
104 LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
105 LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
108 if (isNoNovaPointingToPort(pointingNovaServers)) {
110 .addMessage(fileName, ErrorLevel.WARNING,
111 ErrorMessagesFormatBuilder
112 .getErrorWithParameters(
113 ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
114 portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
115 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
119 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
120 return CollectionUtils.isEmpty(pointingNovaServers);
123 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
124 return CollectionUtils.isNotEmpty(pointingNovaServers)
125 && pointingNovaServers.size() > 1;
128 @SuppressWarnings("unchecked")
129 private static void validateAllSecurityGroupsAreUsed(String filename,
130 Map.Entry<String, Resource> resourceEntry,
131 List<String> securityGroupResourceNameList,
132 GlobalValidationContext globalContext) {
134 mdcDataDebugMessage.debugEntryMessage("file", filename);
136 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
138 if (MapUtils.isEmpty(propertiesMap)) {
142 Object securityGroupsValue = propertiesMap.get("security_groups");
144 if (Objects.isNull(securityGroupsValue)) {
148 if (securityGroupsValue instanceof List) {
149 List<Object> securityGroupsListFromCurrResource =
150 (List<Object>) propertiesMap.get("security_groups");
151 for (Object securityGroup : securityGroupsListFromCurrResource) {
152 removeSecurityGroupNamesFromListByGivenFunction(filename,
153 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), securityGroup,
154 securityGroupResourceNameList, globalContext);
158 mdcDataDebugMessage.debugExitMessage("file", filename);
161 private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
163 Object securityGroup,
165 securityGroupResourceNameList,
166 GlobalValidationContext globalContext) {
167 Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
168 .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
169 securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);