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