2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.validation.impl.validators.namingconvention;
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.Resource;
26 import org.openecomp.sdc.validation.ResourceValidator;
27 import org.openecomp.sdc.validation.ValidationContext;
28 import org.openecomp.sdc.validation.util.ValidationUtil;
32 import static java.util.Objects.nonNull;
35 public class ContrailServiceInstanceNamingConventionValidator implements ResourceValidator {
36 private static final String AVAILABILITY_ZONE = "availability_zone";
37 private static final ErrorMessageCode ERROR_CODE_NSI1 = new ErrorMessageCode("NSI1");
38 private static final ErrorMessageCode ERROR_CODE_NSI2 = new ErrorMessageCode("NSI2");
41 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
42 GlobalValidationContext globalContext, ValidationContext validationContext) {
43 validateAvailabilityZoneName(fileName, resourceEntry, globalContext);
46 private void validateAvailabilityZoneName(String fileName,
47 Map.Entry<String, Resource> resourceEntry,
48 GlobalValidationContext globalContext) {
49 String[] regexList = new String[]{"availability_zone_(\\d+)"};
50 if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
54 Object availabilityZoneMap =
55 resourceEntry.getValue().getProperties().containsKey(AVAILABILITY_ZONE) ? resourceEntry
56 .getValue().getProperties().get(AVAILABILITY_ZONE) : null;
58 if (nonNull(availabilityZoneMap)) {
59 if (availabilityZoneMap instanceof Map) {
60 String availabilityZoneName = ValidationUtil
61 .getWantedNameFromPropertyValueGetParam (availabilityZoneMap);
63 if (availabilityZoneName != null && !ValidationUtil
64 .evalPattern(availabilityZoneName, regexList)) {
65 globalContext.addMessage(
67 ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(ERROR_CODE_NSI1,
68 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
69 ValidationUtil.getMessagePartAccordingToResourceType(resourceEntry),
71 availabilityZoneName, resourceEntry.getKey()));
74 globalContext.addMessage(
76 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
77 .getErrorWithParameters(ERROR_CODE_NSI2, Messages.MISSING_GET_PARAM.getErrorMessage(),
78 AVAILABILITY_ZONE, resourceEntry.getKey()));