[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / heatresource / NeutronPortResourceValidator.java
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.Collection;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Set;
25
26 /**
27  * Created by TALIO on 2/22/2017.
28  */
29 public class NeutronPortResourceValidator implements ResourceValidator {
30   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
31
32   @Override
33   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
34                        GlobalValidationContext globalContext, ValidationContext validationContext) {
35
36     validateNovaServerPortBinding
37         (fileName, resourceEntry, (HeatResourceValidationContext)validationContext, globalContext);
38   }
39
40
41   @SuppressWarnings("unchecked")
42   private static void validateNovaServerPortBinding(String fileName,
43                                                     Map.Entry<String, Resource> resourceEntry,
44                                                     HeatResourceValidationContext heatResourceValidationContext,
45                                                     GlobalValidationContext globalContext) {
46
47     mdcDataDebugMessage.debugEntryMessage("file", fileName);
48
49     Map<String, Map<String, List<String>>> portIdToPointingResources =
50         heatResourceValidationContext.getFileLevelResourceDependencies()
51             .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
52
53     if(MapUtils.isEmpty(portIdToPointingResources)){
54       globalContext
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);
61
62       return;
63     }
64
65     for (Map.Entry<String, Map<String, List<String>>> portEntry :
66         portIdToPointingResources.entrySet()) {
67       checkPortBindingFromMap(fileName, portEntry, globalContext);
68     }
69
70     mdcDataDebugMessage.debugExitMessage("file", fileName);
71   }
72
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());
79
80     if (CollectionUtils.isEmpty(pointingNovaServers)) {
81       return;
82     }
83
84     handleErrorEventsForPortBinding(fileName, portEntry, globalContext, pointingNovaServers);
85
86
87   }
88
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)) {
94       globalContext
95           .addMessage(fileName, ErrorLevel.ERROR,
96               ErrorMessagesFormatBuilder
97                   .getErrorWithParameters(
98                       Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
99                       portEntry.getKey()),
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                       portEntry.getKey()), 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 pointingNovaServers.size() == 0;
117   }
118
119   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
120     return pointingNovaServers.size() > 1;
121   }
122
123   @SuppressWarnings("unchecked")
124   private static void validateAllSecurityGroupsAreUsed(String filename,
125                                                        Map.Entry<String, Resource> resourceEntry,
126                                                        List<String> securityGroupResourceNameList,
127                                                        GlobalValidationContext globalContext) {
128
129     mdcDataDebugMessage.debugEntryMessage("file", filename);
130
131     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
132
133     if (MapUtils.isEmpty(propertiesMap)) {
134       return;
135     }
136
137     Object securityGroupsValue = propertiesMap.get("security_groups");
138
139     if (Objects.isNull(securityGroupsValue)) {
140       return;
141     }
142
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);
150       }
151     }
152
153     mdcDataDebugMessage.debugExitMessage("file", filename);
154   }
155
156   private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
157                                                                       String functionName,
158                                                                       Object securityGroup,
159                                                                       Collection<String>
160                                                                           securityGroupResourceNameList,
161                                                                       GlobalValidationContext globalContext) {
162     Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
163         .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
164     securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);
165   }
166 }