2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
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;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
40 import java.util.Objects;
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");
49 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
50 GlobalValidationContext globalContext, ValidationContext validationContext) {
52 validateNovaServerPortBinding(fileName,
53 resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
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());
66 String portResourceId = resourceEntry.getKey();
67 if (MapUtils.isEmpty(portIdToPointingResources)) {
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);
79 Map<String, List<String>> pointingResourcesToCurrPort =
80 portIdToPointingResources.get(portResourceId);
81 checkPortBindingFromMap(
82 fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
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());
93 handleErrorEventsForPortBinding(
94 fileName, portResourceId, globalContext, pointingNovaServers);
99 private static void handleErrorEventsForPortBinding(String fileName,
100 String portResourceId,
101 GlobalValidationContext globalContext,
102 List<String> pointingNovaServers) {
103 if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
105 .addMessage(fileName, ErrorLevel.ERROR,
106 ErrorMessagesFormatBuilder
107 .getErrorWithParameters(
108 ERROR_HPRODE_HPR2, Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
110 LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
111 LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
114 if (isNoNovaPointingToPort(pointingNovaServers)) {
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);
125 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
126 return CollectionUtils.isEmpty(pointingNovaServers);
129 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
130 return CollectionUtils.isNotEmpty(pointingNovaServers)
131 && pointingNovaServers.size() > 1;
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();
141 if (MapUtils.isEmpty(propertiesMap)) {
145 Object securityGroupsValue = propertiesMap.get("security_groups");
147 if (Objects.isNull(securityGroupsValue)) {
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);
162 private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
164 Object securityGroup,
166 securityGroupResourceNameList,
167 GlobalValidationContext globalContext) {
168 Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
169 .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
170 securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);