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.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.validation.ResourceValidator;
34 import org.openecomp.sdc.validation.ValidationContext;
35 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
41 import java.util.Objects;
44 public class NeutronPortResourceValidator implements ResourceValidator {
45 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
46 private static final ErrorMessageCode ERROR_HPRODE_HPR1 = new ErrorMessageCode("HPR1");
47 private static final ErrorMessageCode ERROR_HPRODE_HPR2 = new ErrorMessageCode("HPR2");
48 private static final ErrorMessageCode ERROR_HPRODE_HPR3 = new ErrorMessageCode("HPR3");
51 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
52 GlobalValidationContext globalContext, ValidationContext validationContext) {
54 validateNovaServerPortBinding(fileName,
55 resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
59 @SuppressWarnings("unchecked")
60 private static void validateNovaServerPortBinding(String fileName,
61 Map.Entry<String, Resource> resourceEntry,
62 HeatResourceValidationContext heatResourceValidationContext,
63 GlobalValidationContext globalContext) {
65 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
67 Map<String, Map<String, List<String>>> portIdToPointingResources =
68 heatResourceValidationContext.getFileLevelResourceDependencies()
69 .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
71 String portResourceId = resourceEntry.getKey();
72 if (MapUtils.isEmpty(portIdToPointingResources)) {
74 .addMessage(fileName, ErrorLevel.WARNING,
75 ErrorMessagesFormatBuilder
76 .getErrorWithParameters(
77 ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
78 portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
79 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
84 Map<String, List<String>> pointingResourcesToCurrPort =
85 portIdToPointingResources.get(portResourceId);
86 checkPortBindingFromMap(
87 fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
89 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
92 private static void checkPortBindingFromMap(String fileName,
93 String portResourceId,
94 Map<String, List<String>> resourcesPointingToCurrPort,
95 GlobalValidationContext globalContext) {
96 List<String> pointingNovaServers =
97 MapUtils.isEmpty(resourcesPointingToCurrPort) ? new ArrayList<>()
98 : resourcesPointingToCurrPort.get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
100 handleErrorEventsForPortBinding(
101 fileName, portResourceId, globalContext, pointingNovaServers);
106 private static void handleErrorEventsForPortBinding(String fileName,
107 String portResourceId,
108 GlobalValidationContext globalContext,
109 List<String> pointingNovaServers) {
110 if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
112 .addMessage(fileName, ErrorLevel.ERROR,
113 ErrorMessagesFormatBuilder
114 .getErrorWithParameters(
115 ERROR_HPRODE_HPR2, Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
117 LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
118 LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
121 if (isNoNovaPointingToPort(pointingNovaServers)) {
123 .addMessage(fileName, ErrorLevel.WARNING,
124 ErrorMessagesFormatBuilder
125 .getErrorWithParameters(
126 ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
127 portResourceId), LoggerTragetServiceName.CHECK_FOR_ORPHAN_PORTS,
128 LoggerErrorDescription.NO_BIND_FROM_PORT_TO_NOVA);
132 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
133 return CollectionUtils.isEmpty(pointingNovaServers);
136 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
137 return CollectionUtils.isNotEmpty(pointingNovaServers)
138 && pointingNovaServers.size() > 1;
141 @SuppressWarnings("unchecked")
142 private static void validateAllSecurityGroupsAreUsed(String filename,
143 Map.Entry<String, Resource> resourceEntry,
144 List<String> securityGroupResourceNameList,
145 GlobalValidationContext globalContext) {
147 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", filename);
149 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
151 if (MapUtils.isEmpty(propertiesMap)) {
155 Object securityGroupsValue = propertiesMap.get("security_groups");
157 if (Objects.isNull(securityGroupsValue)) {
161 if (securityGroupsValue instanceof List) {
162 List<Object> securityGroupsListFromCurrResource =
163 (List<Object>) propertiesMap.get("security_groups");
164 for (Object securityGroup : securityGroupsListFromCurrResource) {
165 removeSecurityGroupNamesFromListByGivenFunction(filename,
166 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), securityGroup,
167 securityGroupResourceNameList, globalContext);
171 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", filename);
174 private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
176 Object securityGroup,
178 securityGroupResourceNameList,
179 GlobalValidationContext globalContext) {
180 Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
181 .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
182 securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);