summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/namingconvention/ContrailServiceTemplateNamingConventionValidator.java
blob: 7895c12ae2576d8b92d88c25d7c2ddc0dc2525b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright © 2016-2017 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openecomp.sdc.validation.impl.validators.namingconvention;

import static java.util.Objects.nonNull;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.openecomp.core.validation.ErrorMessageCode;
import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
import org.openecomp.core.validation.types.GlobalValidationContext;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.heat.datatypes.model.Resource;
import org.openecomp.sdc.validation.ResourceValidator;
import org.openecomp.sdc.validation.ValidationContext;
import org.openecomp.sdc.validation.util.ValidationUtil;

public class ContrailServiceTemplateNamingConventionValidator implements ResourceValidator {

    private static final ErrorMessageCode ERROR_CODE_NST1 = new ErrorMessageCode("NST1");
    private static final ErrorMessageCode ERROR_CODE_NST2 = new ErrorMessageCode("NST2");
    private static final ErrorMessageCode ERROR_CODE_NST3 = new ErrorMessageCode("NST3");

    @Override
    public void validate(String fileName, Map.Entry<String, Resource> resourceEntry, GlobalValidationContext globalContext,
                         ValidationContext validationContext) {
        validateServiceTemplateImageAndFlavor(fileName, resourceEntry, globalContext);
    }

    private void validateServiceTemplateImageAndFlavor(String fileName, Map.Entry<String, Resource> entry, GlobalValidationContext globalContext) {
        if (MapUtils.isEmpty(entry.getValue().getProperties())) {
            return;
        }
        Pair<String, String> imagePair = new ImmutablePair<>("image_name", ".*_image_name");
        Pair<String, String> flavorPair = new ImmutablePair<>("flavor", ".*_flavor_name");
        List<Pair<String, String>> imageFlavorPairs = Arrays.asList(imagePair, flavorPair);
        Map<String, Object> propertiesMap = entry.getValue().getProperties();
        boolean errorExistValidatingImageOrFlavor = false;
        for (Pair<String, String> imageOrFlavor : imageFlavorPairs) {
            boolean errorExistWhenValidatingImageOrFlavorNames = isErrorExistWhenValidatingImageOrFlavorNames(fileName, imageOrFlavor, entry,
                propertiesMap, globalContext);
            errorExistValidatingImageOrFlavor = errorExistValidatingImageOrFlavor || errorExistWhenValidatingImageOrFlavorNames;
        }
        if (!errorExistValidatingImageOrFlavor) {
            validateServiceTemplatePropertiesValuesVmtypesAreIdentical(fileName, entry, globalContext, propertiesMap);
        }
    }

    private void validateServiceTemplatePropertiesValuesVmtypesAreIdentical(String fileName, Map.Entry<String, Resource> entry,
                                                                            GlobalValidationContext globalContext,
                                                                            Map<String, Object> propertiesMap) {
        Pair<String, String> vmTypeImagePair = new ImmutablePair<>("image_name", "\\_image\\_name");
        Pair<String, String> vmTypeFlavorPair = new ImmutablePair<>("flavor", "\\_flavor\\_name");
        validatePropertiesValuesVmtypesAreIdentical(Arrays.asList(vmTypeImagePair, vmTypeFlavorPair), fileName, entry, propertiesMap, globalContext);
    }

    private void validatePropertiesValuesVmtypesAreIdentical(List<Pair> propertiesToMatch, String fileName, Map.Entry<String, Resource> resourceEntry,
                                                             Map<String, Object> propertiesMap, GlobalValidationContext globalContext) {
        if (CollectionUtils.isEmpty(propertiesToMatch)) {
            return;
        }
        String previousPropertyValueValue = null;
        for (Pair propertyToMatch : propertiesToMatch) {
            Optional<String> propertyVmType = extractVmTypeFromProperty(fileName, resourceEntry, propertiesMap, globalContext, propertyToMatch);
            if (propertyVmType.isPresent()) {
                String currentPropVmType = propertyVmType.get();
                previousPropertyValueValue = handleFirstIteration(previousPropertyValueValue, currentPropVmType);
                if (addWarningIfCurrentVmTypeIsDifferentFromPrevious(fileName, resourceEntry, globalContext, previousPropertyValueValue,
                    currentPropVmType)) {
                    return;
                }
            }
        }
    }

    private boolean addWarningIfCurrentVmTypeIsDifferentFromPrevious(String fileName, Map.Entry<String, Resource> resourceEntry,
                                                                     GlobalValidationContext globalContext, String previousPropertyValueValue,
                                                                     String currentPropVmType) {
        if (!Objects.equals(previousPropertyValueValue, currentPropVmType)) {
            globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
                .getErrorWithParameters(ERROR_CODE_NST1, Messages.CONTRAIL_VM_TYPE_NAME_NOT_ALIGNED_WITH_NAMING_CONVENSION.getErrorMessage(),
                    resourceEntry.getKey()));
            return true;
        }
        return false;
    }

    private boolean isErrorExistWhenValidatingImageOrFlavorNames(String fileName, Pair<String, String> propertyNameAndRegex,
                                                                 Map.Entry<String, Resource> resourceEntry, Map<String, Object> propertiesMap,
                                                                 GlobalValidationContext globalContext) {
        String propertyName = propertyNameAndRegex.getKey();
        Object nameValue = propertiesMap.get(propertyName);
        String[] regexList = new String[]{propertyNameAndRegex.getValue()};
        if (nonNull(nameValue)) {
            if (nameValue instanceof Map) {
                globalContext.setMessageCode(ERROR_CODE_NST3);
                if (ValidationUtil.validateMapPropertyValue(fileName, resourceEntry, globalContext, propertyName, nameValue, regexList)) {
                    return true;
                }
            } else {
                globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
                    .getErrorWithParameters(ERROR_CODE_NST2, Messages.MISSING_GET_PARAM.getErrorMessage(), propertyName, resourceEntry.getKey()));
                return true;
            }
            return false;
        }
        return false;
    }

    private Optional<String> extractVmTypeFromProperty(String fileName, Map.Entry<String, Resource> resourceEntry, Map<String, Object> propertiesMap,
                                                       GlobalValidationContext globalContext, Pair propertyKeyRegex) {
        String propertyName = (String) propertyKeyRegex.getKey();
        Object propertyVal = propertiesMap.get(propertyName);
        if (nonNull(propertyVal)) {
            if (propertyVal instanceof Map) {
                String propertyValFromGetParam = ValidationUtil.getWantedNameFromPropertyValueGetParam(propertyVal);
                if (nonNull(propertyValFromGetParam)) {
                    Pattern pattern = Pattern.compile("" + propertyKeyRegex.getValue());
                    return Optional.ofNullable(pattern.split(propertyValFromGetParam)[0]);
                }
            } else {
                globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
                    .getErrorWithParameters(ERROR_CODE_NST2, Messages.MISSING_GET_PARAM.getErrorMessage(), propertyName, resourceEntry.getKey()));
                return Optional.empty();
            }
        }
        return Optional.empty();
    }

    private String handleFirstIteration(String previousPropertyValueValue, String currentPropVmType) {
        String previousPropertyValue;
        if (Objects.isNull(previousPropertyValueValue)) {
            previousPropertyValue = currentPropVmType;
            return previousPropertyValue;
        }
        return previousPropertyValueValue;
    }
}