Remove enter/exit debug #4
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / namingconvention / ContrailServiceInstanceNamingConventionValidator.java
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.Resource;
27 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
28 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
29 import org.openecomp.sdc.validation.ResourceValidator;
30 import org.openecomp.sdc.validation.ValidationContext;
31 import org.openecomp.sdc.validation.util.ValidationUtil;
32
33 import java.util.Map;
34
35
36 public class ContrailServiceInstanceNamingConventionValidator implements ResourceValidator {
37   private static final String AVAILABILITY_ZONE = "availability_zone";
38   private static final ErrorMessageCode ERROR_CODE_NSI1 = new ErrorMessageCode("NSI1");
39   private static final ErrorMessageCode ERROR_CODE_NSI2 = new ErrorMessageCode("NSI2");
40
41   @Override
42   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
43                        GlobalValidationContext globalContext, ValidationContext validationContext) {
44     validateAvailabilityZoneName(fileName, resourceEntry, globalContext);
45   }
46
47   private void validateAvailabilityZoneName(String fileName,
48                                             Map.Entry<String, Resource> resourceEntry,
49                                             GlobalValidationContext globalContext) {
50     String[] regexList = new String[]{"availability_zone_(\\d+)"};
51     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
52       return;
53     }
54
55     Object availabilityZoneMap =
56             resourceEntry.getValue().getProperties().containsKey(AVAILABILITY_ZONE) ? resourceEntry
57                     .getValue().getProperties().get(AVAILABILITY_ZONE) : null;
58
59     if (nonNull(availabilityZoneMap)) {
60       if (availabilityZoneMap instanceof Map) {
61         String availabilityZoneName = ValidationUtil
62                 .getWantedNameFromPropertyValueGetParam (availabilityZoneMap);
63
64           if (availabilityZoneName != null && !ValidationUtil
65                   .evalPattern(availabilityZoneName, regexList)) {
66             globalContext.addMessage(
67                     fileName,
68                     ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(ERROR_CODE_NSI1,
69                             Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
70                             ValidationUtil.getMessagePartAccordingToResourceType(resourceEntry),
71                             "Availability Zone",
72                             availabilityZoneName, resourceEntry.getKey()),
73                     LoggerTragetServiceName.VALIDATE_AVAILABILITY_ZONE_NAME,
74                     LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
75           }
76       } else {
77         globalContext.addMessage(
78                 fileName,
79                 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
80                         .getErrorWithParameters(ERROR_CODE_NSI2, Messages.MISSING_GET_PARAM.getErrorMessage(),
81                                 AVAILABILITY_ZONE, resourceEntry.getKey()),
82                 LoggerTragetServiceName.VALIDATE_AVAILABILITY_ZONE_NAME,
83                 LoggerErrorDescription.MISSING_GET_PARAM);
84       }
85     }
86   }
87
88 }