a21173a6b2748ff0c3829afbf38e38ea7cb682b0
[sdc.git] /
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 java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
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;
34
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");
39
40   @Override
41   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
42                        GlobalValidationContext globalContext, ValidationContext validationContext) {
43
44     validateNovaServerPortBinding(fileName,
45             resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
46   }
47
48
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());
57
58     String portResourceId = resourceEntry.getKey();
59     if (MapUtils.isEmpty(portIdToPointingResources)) {
60       globalContext
61               .addMessage(fileName, ErrorLevel.WARNING,
62                       ErrorMessagesFormatBuilder
63                               .getErrorWithParameters(
64                                       ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
65                                       portResourceId));
66
67       return;
68     }
69
70     Map<String, List<String>> pointingResourcesToCurrPort =
71             portIdToPointingResources.get(portResourceId);
72     checkPortBindingFromMap(
73             fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
74   }
75
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());
83
84     handleErrorEventsForPortBinding(
85             fileName, portResourceId, globalContext, pointingNovaServers);
86
87
88   }
89
90   private static void handleErrorEventsForPortBinding(String fileName,
91                                                       String portResourceId,
92                                                       GlobalValidationContext globalContext,
93                                                       List<String> pointingNovaServers) {
94     if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
95       globalContext
96               .addMessage(fileName, ErrorLevel.ERROR,
97                       ErrorMessagesFormatBuilder
98                               .getErrorWithParameters(
99                                       ERROR_HPRODE_HPR2,
100                                       Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
101                                       portResourceId));
102     }
103
104     if (isNoNovaPointingToPort(pointingNovaServers)) {
105       globalContext
106               .addMessage(fileName, ErrorLevel.WARNING,
107                       ErrorMessagesFormatBuilder
108                               .getErrorWithParameters(
109                                       ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
110                                       portResourceId));
111     }
112   }
113
114   private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
115     return CollectionUtils.isEmpty(pointingNovaServers);
116   }
117
118   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
119     return CollectionUtils.isNotEmpty(pointingNovaServers)
120             && pointingNovaServers.size() > 1;
121   }
122 }