befbe3bb977a89c93730c80a5cf7a1f772cc3ef6
[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 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;
36
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.Set;
43
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");
49
50   @Override
51   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
52                        GlobalValidationContext globalContext, ValidationContext validationContext) {
53
54     validateNovaServerPortBinding(fileName,
55             resourceEntry, (HeatResourceValidationContext) validationContext, globalContext);
56   }
57
58
59   @SuppressWarnings("unchecked")
60   private static void validateNovaServerPortBinding(String fileName,
61                                                     Map.Entry<String, Resource> resourceEntry,
62                                                     HeatResourceValidationContext heatResourceValidationContext,
63                                                     GlobalValidationContext globalContext) {
64
65     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
66
67     Map<String, Map<String, List<String>>> portIdToPointingResources =
68             heatResourceValidationContext.getFileLevelResourceDependencies()
69                     .get(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
70
71     String portResourceId = resourceEntry.getKey();
72     if (MapUtils.isEmpty(portIdToPointingResources)) {
73       globalContext
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);
80
81       return;
82     }
83
84     Map<String, List<String>> pointingResourcesToCurrPort =
85             portIdToPointingResources.get(portResourceId);
86     checkPortBindingFromMap(
87             fileName, portResourceId, pointingResourcesToCurrPort, globalContext);
88
89     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
90   }
91
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());
99
100     handleErrorEventsForPortBinding(
101             fileName, portResourceId, globalContext, pointingNovaServers);
102
103
104   }
105
106   private static void handleErrorEventsForPortBinding(String fileName,
107                                                       String portResourceId,
108                                                       GlobalValidationContext globalContext,
109                                                       List<String> pointingNovaServers) {
110     if (isThereMoreThanOneBindFromNovaToPort(pointingNovaServers)) {
111       globalContext
112               .addMessage(fileName, ErrorLevel.ERROR,
113                       ErrorMessagesFormatBuilder
114                               .getErrorWithParameters(
115                                       ERROR_HPRODE_HPR2, Messages.MORE_THAN_ONE_BIND_FROM_NOVA_TO_PORT.getErrorMessage(),
116                                       portResourceId),
117                       LoggerTragetServiceName.VALIDATE_NOVA_SERVER_PORT_BINDING,
118                       LoggerErrorDescription.PORT_BINDS_MORE_THAN_ONE_NOVA);
119     }
120
121     if (isNoNovaPointingToPort(pointingNovaServers)) {
122       globalContext
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);
129     }
130   }
131
132   private static boolean isNoNovaPointingToPort(List<String> pointingNovaServers) {
133     return CollectionUtils.isEmpty(pointingNovaServers);
134   }
135
136   private static boolean isThereMoreThanOneBindFromNovaToPort(List<String> pointingNovaServers) {
137     return CollectionUtils.isNotEmpty(pointingNovaServers)
138             && pointingNovaServers.size() > 1;
139   }
140
141   @SuppressWarnings("unchecked")
142   private static void validateAllSecurityGroupsAreUsed(String filename,
143                                                        Map.Entry<String, Resource> resourceEntry,
144                                                        List<String> securityGroupResourceNameList,
145                                                        GlobalValidationContext globalContext) {
146
147     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", filename);
148
149     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
150
151     if (MapUtils.isEmpty(propertiesMap)) {
152       return;
153     }
154
155     Object securityGroupsValue = propertiesMap.get("security_groups");
156
157     if (Objects.isNull(securityGroupsValue)) {
158       return;
159     }
160
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);
168       }
169     }
170
171     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", filename);
172   }
173
174   private static void removeSecurityGroupNamesFromListByGivenFunction(String filename,
175                                                                       String functionName,
176                                                                       Object securityGroup,
177                                                                       Collection<String>
178                                                                               securityGroupResourceNameList,
179                                                                       GlobalValidationContext globalContext) {
180     Set<String> securityGroupsNamesFromFunction = HeatStructureUtil
181             .getReferencedValuesByFunctionName(filename, functionName, securityGroup, globalContext);
182     securityGroupsNamesFromFunction.forEach(securityGroupResourceNameList::remove);
183   }
184 }