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