78c01cfdfc6abbbf6b02de30ccdd7759ea07d83f
[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 = new String[]{".*_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 =
101             new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
102                     "[^_]+_[^_]+_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           if (nonNull(fixedIpsName) && !ValidationUtil
132                   .evalPattern(fixedIpsName, regexList)) {
133             globalContext.addMessage(
134                     fileName,
135                     ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
136                             ERROR_CODE_NNP1,
137                             Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
138                             "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()));
139           }
140
141
142       } else {
143         globalContext.addMessage(
144                 fileName,
145                 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
146                         .getErrorWithParameters(
147                                 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
148                                 "fixed_ips", resourceEntry.getKey()));
149       }
150     }
151   }
152
153   private void validateParamNamingConvention(String fileName, String resourceId,
154                                              Object propertyValue,
155                                               String[] regexList,
156                                              Messages message,
157                                              GlobalValidationContext globalContext) {
158     Object paramName;
159     if (propertyValue instanceof Map) {
160       paramName = ((Map) propertyValue).get("get_param");
161         if (paramName instanceof String && !ValidationUtil.evalPattern(paramName, regexList)) {
162           globalContext.addMessage(
163                   fileName,
164                   ErrorLevel.WARNING, ErrorMessagesFormatBuilder
165                           .getErrorWithParameters(ERROR_CODE_NNP3, message.getErrorMessage(), "Port",
166                                   "Network", (String) paramName, resourceId));
167         }
168
169     } else {
170       globalContext.addMessage(
171               fileName,
172               ErrorLevel.WARNING,
173               ErrorMessagesFormatBuilder
174                       .getErrorWithParameters(
175                               ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
176                               "network or network_id", resourceId));
177     }
178   }
179 }