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 java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
23 import java.util.Objects;
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.apache.commons.collections4.MapUtils;
28 import org.openecomp.core.validation.ErrorMessageCode;
29 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
30 import org.openecomp.core.validation.types.GlobalValidationContext;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
34 import org.openecomp.sdc.heat.datatypes.model.Resource;
35 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
36 import org.openecomp.sdc.heat.services.HeatStructureUtil;
37 import org.openecomp.sdc.validation.ResourceValidator;
38 import org.openecomp.sdc.validation.ValidationContext;
39 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
41 public class NeutronPortResourceValidator implements ResourceValidator {
42 private static final ErrorMessageCode ERROR_HPRODE_HPR1 = new ErrorMessageCode("HPR1");
43 private static final ErrorMessageCode ERROR_HPRODE_HPR2 = new ErrorMessageCode("HPR2");
44 private static final ErrorMessageCode ERROR_HPRODE_HPR3 = new ErrorMessageCode("HPR3");
47 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
48 GlobalValidationContext globalContext, ValidationContext validationContext) {
50 validateNovaServerPortBinding(fileName,
51 resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
55 @SuppressWarnings("unchecked")
56 private static void validateNovaServerPortBinding(String fileName,
57 Map.Entry<String, Resource> resourceEntry,
58 HeatResourceValidationContext heatResourceValidationContext,
59 GlobalValidationContext globalContext) {
60 Map<String, Map<String, List<String>>> portIdToPointingResources =
61 heatResourceValidationContext.getFileLevelResourceDependencies()
62 .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
64 String portResourceId = resourceEntry.getKey();
65 if (MapUtils.isEmpty(portIdToPointingResources)) {
67 .addMessage(fileName, ErrorLevel.WARNING,
68 ErrorMessagesFormatBuilder
69 .getErrorWithParameters(
70 ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
76 Map<String, List<String>> pointingResourcesToCurrPort =
77 portIdToPointingResources.get(portResourceId);
78 checkPortBindingFromMap(
79 fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
82 private static void checkPortBindingFromMap(String fileName,
83 String portResourceId,
84 Map<String, List<String>> resourcesPointingToCurrPort,
85 GlobalValidationContext globalContext) {
86 List<String> pointingNovaServers =
87 MapUtils.isEmpty(resourcesPointingToCurrPort) ? new ArrayList<>()
88 : resourcesPointingToCurrPort.get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
90 handleErrorEventsForPortBinding(
91 fileName, portResourceId, globalContext, pointingNovaServers);
96 private static void handleErrorEventsForPortBinding(String fileName,
97 String portResourceId,
98 GlobalValidationContext globalContext,
99 List<String> pointingNovaServers) {
100 if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
102 .addMessage(fileName, ErrorLevel.ERROR,
103 ErrorMessagesFormatBuilder
104 .getErrorWithParameters(
106 Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
110 if (isNoNovaPointingToPort(pointingNovaServers)) {
112 .addMessage(fileName, ErrorLevel.WARNING,
113 ErrorMessagesFormatBuilder
114 .getErrorWithParameters(
115 ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
120 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
121 return CollectionUtils.isEmpty(pointingNovaServers);
124 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
125 return CollectionUtils.isNotEmpty(pointingNovaServers)
126 && pointingNovaServers.size() > 1;
129 @SuppressWarnings("unchecked")
130 private static void validateAllSecurityGroupsAreUsed(String filename,
131 Map.Entry<String, Resource> resourceEntry,
132 List<String> securityGroupResourceNameList,
133 GlobalValidationContext globalContext) {
134 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
136 if (MapUtils.isEmpty(propertiesMap)) {
140 Object securityGroupsValue = propertiesMap.get("security_groups");
142 if (Objects.isNull(securityGroupsValue)) {
146 if (securityGroupsValue instanceof List) {
147 List<Object> securityGroupsListFromCurrResource =
148 (List<Object>) propertiesMap.get("security_groups");
149 for (Object securityGroup : securityGroupsListFromCurrResource) {
150 removeSecurityGroupNamesFromListByGivenFunction(filename,
151 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), securityGroup,
152 securityGroupResourceNameList, globalContext);
157 private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
159 Object securityGroup,
161 securityGroupResourceNameList,
162 GlobalValidationContext globalContext) {
163 Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
164 .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
165 securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);