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