a3821f2a218e2a5a4f3f523abd481831f614881b
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / namingconvention / NeutronPortNamingConventionValidator.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.namingconvention;
18
19 import static java.util.Objects.nonNull;
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.HeatOrchestrationTemplate;
27 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.validation.ResourceValidator;
33 import org.openecomp.sdc.validation.ValidationContext;
34 import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
35 import org.openecomp.sdc.validation.util.ValidationUtil;
36
37 import java.util.List;
38 import java.util.Map;
39
40 public class NeutronPortNamingConventionValidator implements ResourceValidator {
41   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
42   private static final ErrorMessageCode ERROR_CODE_NNP1 = new ErrorMessageCode("NNP1");
43   private static final ErrorMessageCode ERROR_CODE_NNP2 = new ErrorMessageCode("NNP2");
44   private static final ErrorMessageCode ERROR_CODE_NNP3 = new ErrorMessageCode("NNP3");
45
46   @Override
47   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
48                        GlobalValidationContext globalContext, ValidationContext validationContext) {
49
50     NamingConventionValidationContext namingConventionValidationContext =
51             (NamingConventionValidationContext)validationContext;
52     validatePortNetworkNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
53     validateFixedIpsNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
54   }
55
56   private void validatePortNetworkNamingConvention(String fileName,
57                                                    HeatOrchestrationTemplate heatOrchestrationTemplate,
58                                                    GlobalValidationContext globalContext) {
59
60     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
61
62     if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
63       MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
64       return;
65     }
66     String[] regexList = new String[]{".*_net_id", ".*_net_name", ".*_net_fqdn"};
67
68     heatOrchestrationTemplate
69             .getResources()
70             .entrySet()
71             .stream()
72             .filter(entry -> entry.getValue().getType()
73                     .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
74             .forEach(entry -> entry.getValue()
75                     .getProperties()
76                     .entrySet()
77                     .stream()
78                     .filter(propertyEntry ->
79                             ("network").equalsIgnoreCase(propertyEntry.getKey())
80                                     || ("network_id").equals(propertyEntry.getKey()))
81                     .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
82                             propertyEntry.getValue(),  regexList,
83                             Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES, globalContext)));
84
85     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
86   }
87
88   private void validateFixedIpsNamingConvention(String fileName,
89                                                 HeatOrchestrationTemplate heatOrchestrationTemplate,
90                                                 GlobalValidationContext globalContext) {
91
92     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
93
94     if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
95       MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
96       return;
97     }
98
99     heatOrchestrationTemplate.getResources()
100             .entrySet()
101             .stream()
102             .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
103             .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
104                     .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
105             .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
106
107     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
108   }
109
110   private void checkNeutronPortFixedIpsName(String fileName,
111                                             Map.Entry<String, Resource> resourceEntry,
112                                             GlobalValidationContext globalContext) {
113     String[] regexList =
114             new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
115                     "[^_]+_[^_]+_v6_ip_(\\d+)"};
116
117     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
118       return;
119     }
120
121     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
122     Object fixedIps = propertiesMap.get("fixed_ips");
123     if (nonNull(fixedIps) && fixedIps instanceof List) {
124       List<Object> fixedIpsList = (List<Object>) fixedIps;
125       for (Object fixedIpsObject : fixedIpsList) {
126         Map.Entry<String, Object> fixedIpsEntry =
127                 ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
128
129         validateFixedIpsName(fileName, resourceEntry, globalContext, regexList, fixedIpsEntry);
130
131
132       }
133     }
134   }
135
136   private void validateFixedIpsName(String fileName, Map.Entry<String, Resource> resourceEntry,
137                                     GlobalValidationContext globalContext,
138                                     String[] regexList, Map.Entry<String, Object> fixedIpsEntry) {
139     if (nonNull(fixedIpsEntry)) {
140       if (fixedIpsEntry.getValue() instanceof Map) {
141
142         String fixedIpsName = ValidationUtil
143                 .getWantedNameFromPropertyValueGetParam(fixedIpsEntry.getValue());
144           if (nonNull(fixedIpsName) && !ValidationUtil
145                   .evalPattern(fixedIpsName, regexList)) {
146             globalContext.addMessage(
147                     fileName,
148                     ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
149                             ERROR_CODE_NNP1,
150                             Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
151                             "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()),
152                     LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
153                     LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
154           }
155
156
157       } else {
158         globalContext.addMessage(
159                 fileName,
160                 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
161                         .getErrorWithParameters(
162                                 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
163                                 "fixed_ips", resourceEntry.getKey()),
164                 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
165                 LoggerErrorDescription.MISSING_GET_PARAM);
166       }
167     }
168   }
169
170   private void validateParamNamingConvention(String fileName, String resourceId,
171                                              Object propertyValue,
172                                               String[] regexList,
173                                              Messages message,
174                                              GlobalValidationContext globalContext) {
175
176     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
177
178     Object paramName;
179     if (propertyValue instanceof Map) {
180       paramName = ((Map) propertyValue).get("get_param");
181         if (paramName instanceof String && !ValidationUtil
182                 .evalPattern((String) paramName, regexList)) {
183           globalContext.addMessage(
184                   fileName,
185                   ErrorLevel.WARNING, ErrorMessagesFormatBuilder
186                           .getErrorWithParameters(ERROR_CODE_NNP3, message.getErrorMessage(), "Port",
187                                   "Network", (String) paramName, resourceId),
188                   LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
189                   LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
190         }
191
192     } else {
193       globalContext.addMessage(
194               fileName,
195               ErrorLevel.WARNING,
196               ErrorMessagesFormatBuilder
197                       .getErrorWithParameters(
198                               ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
199                               "network or network_id", resourceId),
200               LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
201               LoggerErrorDescription.MISSING_GET_PARAM);
202     }
203
204     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
205   }
206 }