bb749079f5f01d87cb1d6b1ec7ebf13a78e0e5fb
[sdc.git] /
1 package org.openecomp.sdc.validation.impl.validators.heatresource;
2
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;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Set;
26
27 /**
28  * Created by TALIO on 2/22/2017.
29  */
30 public class NeutronPortResourceValidator implements ResourceValidator {
31   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
32
33   @Override
34   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
35                        GlobalValidationContext globalContext, ValidationContext validationContext) {
36
37     validateNovaServerPortBinding
38         (fileName, resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
39   }
40
41
42   @SuppressWarnings("unchecked")
43   private static void validateNovaServerPortBinding(String fileName,
44                                                     Map.Entry<String, Resource> resourceEntry,
45                                                     HeatResourceValidationContext heatResourceValidationContext,
46                                                     GlobalValidationContext globalContext) {
47
48     mdcDataDebugMessage.debugEntryMessage("file", fileName);
49
50     Map<String, Map<String, List<String>>> portIdToPointingResources =
51         heatResourceValidationContext.getFileLevelResourceDependencies()
52             .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
53
54     String portResourceId = resourceEntry.getKey();
55     if (MapUtils.isEmpty(portIdToPointingResources)) {
56       globalContext
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);
63
64       return;
65     }
66
67     Map<String, List<String>> pointingResourcesToCurrPort =
68         portIdToPointingResources.get(portResourceId);
69     checkPortBindingFromMap(
70         fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
71
72     mdcDataDebugMessage.debugExitMessage("file", fileName);
73   }
74
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());
82
83     handleErrorEventsForPortBinding(
84         fileName, portResourceId, globalContext, pointingNovaServers);
85
86
87   }
88
89   private static void handleErrorEventsForPortBinding(String fileName,
90                                                       String portResourceId,
91                                                       GlobalValidationContext globalContext,
92                                                       List<String> pointingNovaServers) {
93     if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
94       globalContext
95           .addMessage(fileName, ErrorLevel.ERROR,
96               ErrorMessagesFormatBuilder
97                   .getErrorWithParameters(
98                       Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
99                       portResourceId),
100               LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
101               LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
102     }
103
104     if (isNoNovaPointingToPort(pointingNovaServers)) {
105       globalContext
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);
112     }
113   }
114
115   private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
116     return CollectionUtils.isEmpty(pointingNovaServers);
117   }
118
119   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
120     return CollectionUtils.isNotEmpty(pointingNovaServers)
121             && pointingNovaServers.size() > 1;
122   }
123
124   @SuppressWarnings("unchecked")
125   private static void validateAllSecurityGroupsAreUsed(String filename,
126                                                        Map.Entry<String, Resource> resourceEntry,
127                                                        List<String> securityGroupResourceNameList,
128                                                        GlobalValidationContext globalContext) {
129
130     mdcDataDebugMessage.debugEntryMessage("file", filename);
131
132     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
133
134     if (MapUtils.isEmpty(propertiesMap)) {
135       return;
136     }
137
138     Object securityGroupsValue = propertiesMap.get("security_groups");
139
140     if (Objects.isNull(securityGroupsValue)) {
141       return;
142     }
143
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);
151       }
152     }
153
154     mdcDataDebugMessage.debugExitMessage("file", filename);
155   }
156
157   private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
158                                                                       String functionName,
159                                                                       Object securityGroup,
160                                                                       Collection<String>
161                                                                           securityGroupResourceNameList,
162                                                                       GlobalValidationContext globalContext) {
163     Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
164         .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
165     securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);
166   }
167 }