2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.dcae.rule.editor.validators;
23 import org.onap.sdc.dcae.composition.restmodels.ruleeditor.MappingRules;
24 import org.onap.sdc.dcae.composition.restmodels.ruleeditor.Rule;
25 import org.onap.sdc.dcae.errormng.ActionStatus;
26 import org.onap.sdc.dcae.errormng.ErrConfMgr;
27 import org.onap.sdc.dcae.errormng.ResponseFormat;
28 import org.onap.sdc.dcae.rule.editor.utils.ValidationUtils;
29 import org.onap.sdc.dcae.ves.VesStructureLoader;
31 import java.util.List;
34 import java.util.stream.Collectors;
36 public class MappingRulesValidator implements IRuleElementValidator<MappingRules> {
38 private RuleValidator ruleValidator = RuleValidator.getInstance();
40 private static MappingRulesValidator mappingRulesValidator = new MappingRulesValidator();
42 public static MappingRulesValidator getInstance() {
43 return mappingRulesValidator;
46 private MappingRulesValidator(){}
48 public boolean validate(MappingRules rules, List<ResponseFormat> errors) {
52 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.INVALID_RULE_FORMAT, "", "no rules found"));
54 // TODO consider using 'allMatch' which will stop on the first error
55 return rules.getRules().values().stream().map(r -> ruleValidator.validate(r, errors))
56 .reduce(true, (x,y) -> x && y) && valid;
59 public boolean validateVersionAndType(MappingRules rules) {
60 Map<String, Set<String>> supportedVersions =
61 VesStructureLoader.getAvailableVersionsAndEventTypes();
63 return ValidationUtils.validateNotEmpty(rules.getVersion())
64 && supportedVersions.containsKey(rules.getVersion())
65 && ValidationUtils.validateNotEmpty(rules.getEventType())
66 && supportedVersions.get(rules.getVersion()).contains(rules.getEventType());
70 public boolean validateGroupDefinitions(MappingRules rules) {
71 return rules.getRules().values().stream().allMatch(r -> ValidationUtils.validateNotEmpty(r.getGroupId()) &&
72 ValidationUtils.validateNotEmpty(r.getPhase())) &&
73 rules.getRules().values().stream().collect(
74 Collectors.groupingBy(Rule::getGroupId, Collectors.mapping(Rule::getPhase, Collectors.toSet())))
75 .values().stream().allMatch(p -> 1 == p.size());
79 public boolean validateTranslationPhaseNames(MappingRules rules, List<ResponseFormat> errors) {
82 rules.getRules().values().stream().map(Rule::getPhase).collect(Collectors.toSet());
83 if (phases.contains(rules.getEntryPhase())) {
85 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null,
86 "entry phase name already exists"));
88 if (phases.contains(rules.getPublishPhase())) {
90 errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null,
91 "publish phase name already exists"));