2568f404165c563468523ed2dff4d48c490ceab5
[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 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.validation.ErrorMessageCode;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
29 import org.openecomp.sdc.heat.services.HeatStructureUtil;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.validation.ResourceValidator;
33 import org.openecomp.sdc.validation.ValidationContext;
34 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Objects;
41 import java.util.Set;
42
43 public class NeutronPortResourceValidator implements ResourceValidator {
44   private static final ErrorMessageCode ERROR_HPRODE_HPR1 = new ErrorMessageCode("HPR1");
45   private static final ErrorMessageCode ERROR_HPRODE_HPR2 = new ErrorMessageCode("HPR2");
46   private static final ErrorMessageCode ERROR_HPRODE_HPR3 = new ErrorMessageCode("HPR3");
47
48   @Override
49   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
50                        GlobalValidationContext globalContext, ValidationContext validationContext) {
51
52     validateNovaServerPortBinding(fileName,
53             resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
54   }
55
56
57   @SuppressWarnings("unchecked")
58   private static void validateNovaServerPortBinding(String fileName,
59                                                     Map.Entry<String, Resource> resourceEntry,
60                                                     HeatResourceValidationContext heatResourceValidationContext,
61                                                     GlobalValidationContext globalContext) {
62     Map<String, Map<String, List<String>>> portIdToPointingResources =
63             heatResourceValidationContext.getFileLevelResourceDependencies()
64                     .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
65
66     String portResourceId = resourceEntry.getKey();
67     if (MapUtils.isEmpty(portIdToPointingResources)) {
68       globalContext
69               .addMessage(fileName, ErrorLevel.WARNING,
70                       ErrorMessagesFormatBuilder
71                               .getErrorWithParameters(
72                                       ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
73                                       portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
74                       LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
75
76       return;
77     }
78
79     Map<String, List<String>> pointingResourcesToCurrPort =
80             portIdToPointingResources.get(portResourceId);
81     checkPortBindingFromMap(
82             fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
83   }
84
85   private static void checkPortBindingFromMap(String fileName,
86                                               String portResourceId,
87                                               Map<String, List<String>> resourcesPointingToCurrPort,
88                                               GlobalValidationContext globalContext) {
89     List<String> pointingNovaServers =
90             MapUtils.isEmpty(resourcesPointingToCurrPort) ? new ArrayList<>()
91                     : resourcesPointingToCurrPort.get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
92
93     handleErrorEventsForPortBinding(
94             fileName, portResourceId, globalContext, pointingNovaServers);
95
96
97   }
98
99   private static void handleErrorEventsForPortBinding(String fileName,
100                                                       String portResourceId,
101                                                       GlobalValidationContext globalContext,
102                                                       List<String> pointingNovaServers) {
103     if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
104       globalContext
105               .addMessage(fileName, ErrorLevel.ERROR,
106                       ErrorMessagesFormatBuilder
107                               .getErrorWithParameters(
108                                       ERROR_HPRODE_HPR2, Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
109                                       portResourceId),
110                       LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
111                       LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
112     }
113
114     if (isNoNovaPointingToPort(pointingNovaServers)) {
115       globalContext
116               .addMessage(fileName, ErrorLevel.WARNING,
117                       ErrorMessagesFormatBuilder
118                               .getErrorWithParameters(
119                                       ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
120                                       portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
121                       LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
122     }
123   }
124
125   private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
126     return CollectionUtils.isEmpty(pointingNovaServers);
127   }
128
129   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
130     return CollectionUtils.isNotEmpty(pointingNovaServers)
131             && pointingNovaServers.size() > 1;
132   }
133
134   @SuppressWarnings("unchecked")
135   private static void validateAllSecurityGroupsAreUsed(String filename,
136                                                        Map.Entry<String, Resource> resourceEntry,
137                                                        List<String> securityGroupResourceNameList,
138                                                        GlobalValidationContext globalContext) {
139     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
140
141     if (MapUtils.isEmpty(propertiesMap)) {
142       return;
143     }
144
145     Object securityGroupsValue = propertiesMap.get("security_groups");
146
147     if (Objects.isNull(securityGroupsValue)) {
148       return;
149     }
150
151     if (securityGroupsValue instanceof List) {
152       List<Object> securityGroupsListFromCurrResource =
153               (List<Object>) propertiesMap.get("security_groups");
154       for (Object securityGroup : securityGroupsListFromCurrResource) {
155         removeSecurityGroupNamesFromListByGivenFunction(filename,
156                 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), securityGroup,
157                 securityGroupResourceNameList, globalContext);
158       }
159     }
160   }
161
162   private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
163                                                                       String functionName,
164                                                                       Object securityGroup,
165                                                                       Collection<String>
166                                                                               securityGroupResourceNameList,
167                                                                       GlobalValidationContext globalContext) {
168     Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
169             .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
170     securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);
171   }
172 }