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.Collection;
21 import java.util.List;
23 import java.util.Objects;
27 * Created by TALIO on 2/22/2017.
29 public class NeutronPortResourceValidator implements ResourceValidator {
30 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
33 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
34 GlobalValidationContext globalContext, ValidationContext validationContext) {
36 validateNovaServerPortBinding
37 (fileName, resourceEntry, (HeatResourceValidationContext)validationContext, globalContext);
41 @SuppressWarnings("unchecked")
42 private static void validateNovaServerPortBinding(String fileName,
43 Map.Entry<String, Resource> resourceEntry,
44 HeatResourceValidationContext heatResourceValidationContext,
45 GlobalValidationContext globalContext) {
47 mdcDataDebugMessage.debugEntryMessage("file", fileName);
49 Map<String, Map<String, List<String>>> portIdToPointingResources =
50 heatResourceValidationContext.getFileLevelResourceDependencies()
51 .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
53 if(MapUtils.isEmpty(portIdToPointingResources)){
55 .addMessage(fileName, ErrorLevel.WARNING,
56 ErrorMessagesFormatBuilder
57 .getErrorWithParameters(
58 Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
59 resourceEntry.getKey()), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
60 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
65 for (Map.Entry<String, Map<String, List<String>>> portEntry :
66 portIdToPointingResources.entrySet()) {
67 checkPortBindingFromMap(fileName, portEntry, globalContext);
70 mdcDataDebugMessage.debugExitMessage("file", fileName);
73 private static void checkPortBindingFromMap(String fileName,
74 Map.Entry<String, Map<String, List<String>>> portEntry,
75 GlobalValidationContext globalContext) {
76 Map<String, List<String>> pointingResourcesToCurrPort = portEntry.getValue();
77 List<String> pointingNovaServers = pointingResourcesToCurrPort
78 .get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
80 if (CollectionUtils.isEmpty(pointingNovaServers)) {
84 handleErrorEventsForPortBinding(fileName, portEntry, globalContext, pointingNovaServers);
89 private static void handleErrorEventsForPortBinding(String fileName,
90 Map.Entry<String, Map<String, List<String>>> portEntry,
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 portEntry.getKey()), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
111 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
115 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
116 return pointingNovaServers.size() == 0;
119 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
120 return pointingNovaServers.size() > 1;
123 @SuppressWarnings("unchecked")
124 private static void validateAllSecurityGroupsAreUsed(String filename,
125 Map.Entry<String, Resource> resourceEntry,
126 List<String> securityGroupResourceNameList,
127 GlobalValidationContext globalContext) {
129 mdcDataDebugMessage.debugEntryMessage("file", filename);
131 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
133 if (MapUtils.isEmpty(propertiesMap)) {
137 Object securityGroupsValue = propertiesMap.get("security_groups");
139 if (Objects.isNull(securityGroupsValue)) {
143 if (securityGroupsValue instanceof List) {
144 List<Object> securityGroupsListFromCurrResource =
145 (List<Object>) propertiesMap.get("security_groups");
146 for (Object securityGroup : securityGroupsListFromCurrResource) {
147 removeSecurityGroupNamesFromListByGivenFunction(filename,
148 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), securityGroup,
149 securityGroupResourceNameList, globalContext);
153 mdcDataDebugMessage.debugExitMessage("file", filename);
156 private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
158 Object securityGroup,
160 securityGroupResourceNameList,
161 GlobalValidationContext globalContext) {
162 Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
163 .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
164 securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);