2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.so.bpmn.common.listener.validation;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.javatuples.Pair;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.bpmn.common.DelegateExecutionImpl;
31 import org.onap.so.bpmn.common.listener.ListenerRunner;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Component;
38 * Controls running all pre and post validation for flows.
40 * To define a validation you must make it a spring bean and implement either
41 * {@link org.onap.so.bpmn.common.validation.PreFlowValidator} or
42 * {@link org.onap.so.bpmn.common.validation.PostFlowValidator} your validation will automatically be run by this class.
46 public abstract class FlowValidatorRunner<S extends FlowValidator, E extends FlowValidator> extends ListenerRunner {
48 private static Logger logger = LoggerFactory.getLogger(FlowValidatorRunner.class);
50 protected List<S> preFlowValidators;
51 protected List<E> postFlowValidators;
56 * Changed to object because JUEL does not support overloaded methods
62 public boolean preValidate(String bbName, Object execution) {
63 return validateHelper(bbName, preFlowValidators, execution);
67 * Changed to object because JUEL does not support overloaded methods
73 public boolean postValidate(String bbName, Object execution) {
74 return validateHelper(bbName, postFlowValidators, execution);
77 protected boolean validateHelper(String bbName, List<? extends FlowValidator> validators, Object obj) {
79 if (obj instanceof DelegateExecution) {
80 return validate(validators, bbName, new DelegateExecutionImpl((DelegateExecution) obj));
81 } else if (obj instanceof BuildingBlockExecution) {
82 return validate(validators, bbName, (BuildingBlockExecution) obj);
88 protected boolean validate(List<? extends FlowValidator> validators, String bbName,
89 BuildingBlockExecution execution) {
90 List<Pair<String, Optional<String>>> results = runValidations(validators, bbName, execution);
92 if (!results.isEmpty()) {
93 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
94 "Failed Validations:\n" + results.stream()
95 .map(item -> String.format("%s: %s", item.getValue0(), item.getValue1().get()))
96 .collect(Collectors.joining("\n")));
103 protected List<Pair<String, Optional<String>>> runValidations(List<? extends FlowValidator> validators,
104 String bbName, BuildingBlockExecution execution) {
106 List<? extends FlowValidator> filtered = filterListeners(validators, (item -> item.shouldRunFor(bbName)));
108 List<Pair<String, Optional<String>>> results = new ArrayList<>();
109 filtered.forEach(item -> results.add(new Pair<>(item.getClass().getName(), item.validate(execution))));
111 return results.stream().filter(item -> item.getValue1().isPresent()).collect(Collectors.toList());
114 protected abstract List<S> getPreFlowValidators();
116 protected abstract List<E> getPostFlowValidators();