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