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.List;
22 import org.apache.commons.collections4.CollectionUtils;
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.validation.ErrorMessageCode;
25 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
26 import org.openecomp.core.validation.types.GlobalValidationContext;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
30 import org.openecomp.sdc.heat.datatypes.model.Resource;
31 import org.openecomp.sdc.validation.ResourceValidator;
32 import org.openecomp.sdc.validation.ValidationContext;
33 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
35 public class NeutronPortResourceValidator implements ResourceValidator {
36 private static final ErrorMessageCode ERROR_HPRODE_HPR1 = new ErrorMessageCode("HPR1");
37 private static final ErrorMessageCode ERROR_HPRODE_HPR2 = new ErrorMessageCode("HPR2");
38 private static final ErrorMessageCode ERROR_HPRODE_HPR3 = new ErrorMessageCode("HPR3");
41 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
42 GlobalValidationContext globalContext, ValidationContext validationContext) {
44 validateNovaServerPortBinding(fileName,
45 resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
49 @SuppressWarnings("unchecked")
50 private static void validateNovaServerPortBinding(String fileName,
51 Map.Entry<String, Resource> resourceEntry,
52 HeatResourceValidationContext heatResourceValidationContext,
53 GlobalValidationContext globalContext) {
54 Map<String, Map<String, List<String>>> portIdToPointingResources =
55 heatResourceValidationContext.getFileLevelResourceDependencies()
56 .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
58 String portResourceId = resourceEntry.getKey();
59 if (MapUtils.isEmpty(portIdToPointingResources)) {
61 .addMessage(fileName, ErrorLevel.WARNING,
62 ErrorMessagesFormatBuilder
63 .getErrorWithParameters(
64 ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
70 Map<String, List<String>> pointingResourcesToCurrPort =
71 portIdToPointingResources.get(portResourceId);
72 checkPortBindingFromMap(
73 fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
76 private static void checkPortBindingFromMap(String fileName,
77 String portResourceId,
78 Map<String, List<String>> resourcesPointingToCurrPort,
79 GlobalValidationContext globalContext) {
80 List<String> pointingNovaServers =
81 MapUtils.isEmpty(resourcesPointingToCurrPort) ? new ArrayList<>()
82 : resourcesPointingToCurrPort.get(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource());
84 handleErrorEventsForPortBinding(
85 fileName, portResourceId, globalContext, pointingNovaServers);
90 private static void handleErrorEventsForPortBinding(String fileName,
91 String portResourceId,
92 GlobalValidationContext globalContext,
93 List<String> pointingNovaServers) {
94 if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
96 .addMessage(fileName, ErrorLevel.ERROR,
97 ErrorMessagesFormatBuilder
98 .getErrorWithParameters(
100 Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
104 if (isNoNovaPointingToPort(pointingNovaServers)) {
106 .addMessage(fileName, ErrorLevel.WARNING,
107 ErrorMessagesFormatBuilder
108 .getErrorWithParameters(
109 ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
114 private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
115 return CollectionUtils.isEmpty(pointingNovaServers);
118 private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
119 return CollectionUtils.isNotEmpty(pointingNovaServers)
120 && pointingNovaServers.size() > 1;