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