f0e7ceccd3331fdbf13abb56507de0083c7d2322
[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.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;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27
28 /**
29  * Created by TALIO on 2/22/2017.
30  */
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");
36
37   @Override
38   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
39                        GlobalValidationContext globalContext, ValidationContext validationContext) {
40
41     validateNovaServerPortBinding
42             (fileName, resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
43   }
44
45
46   @SuppressWarnings("unchecked")
47   private static void validateNovaServerPortBinding(String fileName,
48                                                     Map.Entry<String, Resource> resourceEntry,
49                                                     HeatResourceValidationContext heatResourceValidationContext,
50                                                     GlobalValidationContext globalContext) {
51
52     mdcDataDebugMessage.debugEntryMessage("file", fileName);
53
54     Map<String, Map<String, List<String>>> portIdToPointingResources =
55             heatResourceValidationContext.getFileLevelResourceDependencies()
56                     .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
57
58     String portResourceId = resourceEntry.getKey();
59     if (MapUtils.isEmpty(portIdToPointingResources)) {
60       globalContext
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);
67
68       return;
69     }
70
71     Map<String, List<String>> pointingResourcesToCurrPort =
72             portIdToPointingResources.get(portResourceId);
73     checkPortBindingFromMap(
74             fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
75
76     mdcDataDebugMessage.debugExitMessage("file", fileName);
77   }
78
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());
86
87     handleErrorEventsForPortBinding(
88             fileName, portResourceId, globalContext, pointingNovaServers);
89
90
91   }
92
93   private static void handleErrorEventsForPortBinding(String fileName,
94                                                       String portResourceId,
95                                                       GlobalValidationContext globalContext,
96                                                       List<String> pointingNovaServers) {
97     if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
98       globalContext
99               .addMessage(fileName, ErrorLevel.ERROR,
100                       ErrorMessagesFormatBuilder
101                               .getErrorWithParameters(
102                                       ERROR_HPRODE_HPR2, Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
103                                       portResourceId),
104                       LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
105                       LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
106     }
107
108     if (isNoNovaPointingToPort(pointingNovaServers)) {
109       globalContext
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);
116     }
117   }
118
119   private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
120     return CollectionUtils.isEmpty(pointingNovaServers);
121   }
122
123   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
124     return CollectionUtils.isNotEmpty(pointingNovaServers)
125             && pointingNovaServers.size() > 1;
126   }
127
128   @SuppressWarnings("unchecked")
129   private static void validateAllSecurityGroupsAreUsed(String filename,
130                                                        Map.Entry<String, Resource> resourceEntry,
131                                                        List<String> securityGroupResourceNameList,
132                                                        GlobalValidationContext globalContext) {
133
134     mdcDataDebugMessage.debugEntryMessage("file", filename);
135
136     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
137
138     if (MapUtils.isEmpty(propertiesMap)) {
139       return;
140     }
141
142     Object securityGroupsValue = propertiesMap.get("security_groups");
143
144     if (Objects.isNull(securityGroupsValue)) {
145       return;
146     }
147
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);
155       }
156     }
157
158     mdcDataDebugMessage.debugExitMessage("file", filename);
159   }
160
161   private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
162                                                                       String functionName,
163                                                                       Object securityGroup,
164                                                                       Collection<String>
165                                                                               securityGroupResourceNameList,
166                                                                       GlobalValidationContext globalContext) {
167     Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
168             .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
169     securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);
170   }
171 }