re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / heatresource / NeutronPortResourceValidator.java
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.Collection;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Set;
25
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;
40
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");
45
46   @Override
47   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
48                        GlobalValidationContext globalContext, ValidationContext validationContext) {
49
50     validateNovaServerPortBinding(fileName,
51             resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
52   }
53
54
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());
63
64     String portResourceId = resourceEntry.getKey();
65     if (MapUtils.isEmpty(portIdToPointingResources)) {
66       globalContext
67               .addMessage(fileName, ErrorLevel.WARNING,
68                       ErrorMessagesFormatBuilder
69                               .getErrorWithParameters(
70                                       ERROR_HPRODE_HPR1, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
71                                       portResourceId));
72
73       return;
74     }
75
76     Map<String, List<String>> pointingResourcesToCurrPort =
77             portIdToPointingResources.get(portResourceId);
78     checkPortBindingFromMap(
79             fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
80   }
81
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());
89
90     handleErrorEventsForPortBinding(
91             fileName, portResourceId, globalContext, pointingNovaServers);
92
93
94   }
95
96   private static void handleErrorEventsForPortBinding(String fileName,
97                                                       String portResourceId,
98                                                       GlobalValidationContext globalContext,
99                                                       List<String> pointingNovaServers) {
100     if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
101       globalContext
102               .addMessage(fileName, ErrorLevel.ERROR,
103                       ErrorMessagesFormatBuilder
104                               .getErrorWithParameters(
105                                       ERROR_HPRODE_HPR2,
106                                       Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
107                                       portResourceId));
108     }
109
110     if (isNoNovaPointingToPort(pointingNovaServers)) {
111       globalContext
112               .addMessage(fileName, ErrorLevel.WARNING,
113                       ErrorMessagesFormatBuilder
114                               .getErrorWithParameters(
115                                       ERROR_HPRODE_HPR3, Messages.PORT_NO_BIND_TO_ANY_NOVA_SERVER.getErrorMessage(),
116                                       portResourceId));
117     }
118   }
119
120   private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
121     return CollectionUtils.isEmpty(pointingNovaServers);
122   }
123
124   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
125     return CollectionUtils.isNotEmpty(pointingNovaServers)
126             && pointingNovaServers.size() > 1;
127   }
128
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();
135
136     if (MapUtils.isEmpty(propertiesMap)) {
137       return;
138     }
139
140     Object securityGroupsValue = propertiesMap.get("security_groups");
141
142     if (Objects.isNull(securityGroupsValue)) {
143       return;
144     }
145
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);
153       }
154     }
155   }
156
157   private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
158                                                                       String functionName,
159                                                                       Object securityGroup,
160                                                                       Collection<String>
161                                                                               securityGroupResourceNameList,
162                                                                       GlobalValidationContext globalContext) {
163     Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
164             .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
165     securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);
166   }
167 }