040522b576cd636fb6560a460a9b637d6b99a55f
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.common.listener.validation;
22
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;
35
36
37 /**
38  * Controls running all pre and post validation for flows.
39  * 
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.
43  *
44  */
45 @Component
46 public abstract class FlowValidatorRunner<S extends FlowValidator, E extends FlowValidator> extends ListenerRunner {
47
48     private static Logger logger = LoggerFactory.getLogger(FlowValidatorRunner.class);
49
50     protected List<S> preFlowValidators;
51     protected List<E> postFlowValidators;
52
53
54
55     /**
56      * Changed to object because JUEL does not support overloaded methods
57      * 
58      * @param bbName
59      * @param execution
60      * @return
61      */
62     public boolean preValidate(String bbName, Object execution) {
63         return validateHelper(bbName, preFlowValidators, execution);
64     }
65
66     /**
67      * Changed to object because JUEL does not support overloaded methods
68      * 
69      * @param bbName
70      * @param execution
71      * @return
72      */
73     public boolean postValidate(String bbName, Object execution) {
74         return validateHelper(bbName, postFlowValidators, execution);
75     }
76
77     protected boolean validateHelper(String bbName, List<? extends FlowValidator> validators, Object obj) {
78
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);
83         } else {
84             return false;
85         }
86     }
87
88     protected boolean validate(List<? extends FlowValidator> validators, String bbName,
89             BuildingBlockExecution execution) {
90         List<Pair<String, Optional<String>>> results = runValidations(validators, bbName, execution);
91
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")));
97         }
98
99         return true;
100
101     }
102
103     protected List<Pair<String, Optional<String>>> runValidations(List<? extends FlowValidator> validators,
104             String bbName, BuildingBlockExecution execution) {
105
106         List<? extends FlowValidator> filtered = filterListeners(validators, (item -> item.shouldRunFor(bbName)));
107
108         List<Pair<String, Optional<String>>> results = new ArrayList<>();
109         filtered.forEach(item -> results.add(new Pair<>(item.getClass().getName(), item.validate(execution))));
110
111         return results.stream().filter(item -> item.getValue1().isPresent()).collect(Collectors.toList());
112     }
113
114     protected abstract List<S> getPreFlowValidators();
115
116     protected abstract List<E> getPostFlowValidators();
117
118 }