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.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.Resource;
11 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
12 import org.openecomp.sdc.heat.services.HeatStructureUtil;
13 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
14 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
15 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
16 import org.openecomp.sdc.validation.ResourceValidator;
17 import org.openecomp.sdc.validation.ValidationContext;
18 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
24 import java.util.Objects;
28 * Created by TALIO on 2/22/2017.
30 public class NeutronPortResourceValidator implements ResourceValidator {
31 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
34 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
35 GlobalValidationContext globalContext, ValidationContext validationContext) {
37 validateNovaServerPortBinding
38 (fileName, resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
42 @SuppressWarnings("unchecked")
43 private static void validateNovaServerPortBinding(String fileName,
44 Map.Entry<String, Resource> resourceEntry,
45 HeatResourceValidationContext heatResourceValidationContext,
46 GlobalValidationContext globalContext) {
48 mdcDataDebugMessage.debugEntryMessage("file", fileName);
50 Map<String, Map<String, List<String>>> portIdToPointingResources =
51 heatResourceValidationContext.getFileLevelResourceDependencies()
52 .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
54 String portResourceId = resourceEntry.getKey();
55 if (MapUtils.isEmpty(portIdToPointingResources)) {
57 .addMessage(fileName, ErrorLevel.WARNING,
58 ErrorMessagesFormatBuilder
59 .getErrorWithParameters(
60 Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
61 portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
62 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
67 Map<String, List<String>> pointingResourcesToCurrPort =
68 portIdToPointingResources.get(portResourceId);
69 checkPortBindingFromMap(
70 fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
72 mdcDataDebugMessage.debugExitMessage("file", fileName);
75 private static void checkPortBindingFromMap(String fileName,
76 String portResourceId,
77 Map<String, List<String>> resourcesPointingToCurrPort,
78 GlobalValidationContext globalContext) {
79 List<String> pointingNovaServers =
80 MapUtils.isEmpty(resourcesPointingToCurrPort) ? new ArrayList<>()
81 : resourcesPointingToCurrPort.get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
83 handleErrorEventsForPortBinding(
84 fileName, portResourceId, globalContext, pointingNovaServers);
89 private static void handleErrorEventsForPortBinding(String fileName,
90 String portResourceId,
91 GlobalValidationContext globalContext,
92 List<String> pointingNovaServers) {
93 if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
95 .addMessage(fileName, ErrorLevel.ERROR,
96 ErrorMessagesFormatBuilder
97 .getErrorWithParameters(
98 Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
100 LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
101 LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
104 if (isNoNovaPointingToPort(pointingNovaServers)) {
106 .addMessage(fileName, ErrorLevel.WARNING,
107 ErrorMessagesFormatBuilder
108 .getErrorWithParameters(
109 Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
110 portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
111 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
115 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
116 return CollectionUtils.isEmpty(pointingNovaServers);
119 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
120 return CollectionUtils.isNotEmpty(pointingNovaServers)
121 && pointingNovaServers.size() > 1;
124 @SuppressWarnings("unchecked")
125 private static void validateAllSecurityGroupsAreUsed(String filename,
126 Map.Entry<String, Resource> resourceEntry,
127 List<String> securityGroupResourceNameList,
128 GlobalValidationContext globalContext) {
130 mdcDataDebugMessage.debugEntryMessage("file", filename);
132 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
134 if (MapUtils.isEmpty(propertiesMap)) {
138 Object securityGroupsValue = propertiesMap.get("security_groups");
140 if (Objects.isNull(securityGroupsValue)) {
144 if (securityGroupsValue instanceof List) {
145 List<Object> securityGroupsListFromCurrResource =
146 (List<Object>) propertiesMap.get("security_groups");
147 for (Object securityGroup : securityGroupsListFromCurrResource) {
148 removeSecurityGroupNamesFromListByGivenFunction(filename,
149 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), securityGroup,
150 securityGroupResourceNameList, globalContext);
154 mdcDataDebugMessage.debugExitMessage("file", filename);
157 private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
159 Object securityGroup,
161 securityGroupResourceNameList,
162 GlobalValidationContext globalContext) {
163 Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
164 .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
165 securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);