2cc6415a50a8ef68297619da64eb86bd9e1e763e
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / validation / FlowValidatorRunner.java
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.validation;
22
23 import java.lang.annotation.Annotation;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29
30 import javax.annotation.Priority;
31
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.javatuples.Pair;
34 import org.onap.so.bpmn.common.BuildingBlockExecution;
35 import org.onap.so.bpmn.common.DelegateExecutionImpl;
36 import org.onap.so.client.exception.ExceptionBuilder;
37 import org.reflections.Reflections;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.stereotype.Component;
43
44
45 /**
46  * Controls running all pre and post validation for flows.
47  * 
48  * To define a validation you must make it a spring bean and implement either {@link org.onap.so.bpmn.common.validation.PreFlowValidator} or 
49  * {@link org.onap.so.bpmn.common.validation.PostFlowValidator} your validation will automatically be
50  * run by this class.
51  *
52  */
53 @Component
54 public abstract class FlowValidatorRunner<S extends FlowValidator, E extends FlowValidator> {
55
56         private static Logger logger = LoggerFactory.getLogger(FlowValidatorRunner.class);
57         @Autowired
58         protected ApplicationContext context;
59         
60         @Autowired
61         protected ExceptionBuilder exceptionBuilder;
62         
63         protected List<S> preFlowValidators;
64         protected List<E> postFlowValidators;
65
66         
67
68         /**
69          * Changed to object because JUEL does not support overloaded methods
70          * 
71          * @param bbName
72          * @param execution
73          * @return
74          */
75         public boolean preValidate(String bbName, Object execution) {
76                 return validateHelper(bbName, preFlowValidators, execution);
77         }
78         
79         /**
80          * Changed to object because JUEL does not support overloaded methods
81          * 
82          * @param bbName
83          * @param execution
84          * @return
85          */
86         public boolean postValidate(String bbName, Object execution) {
87                 return validateHelper(bbName, postFlowValidators, execution);
88         }
89         
90         protected boolean validateHelper(String bbName, List<? extends FlowValidator> validators, Object obj) {
91                 
92                 if (obj instanceof DelegateExecution) {
93                         return validate(validators, bbName, new DelegateExecutionImpl((DelegateExecution)obj));
94                 } else if (obj instanceof BuildingBlockExecution) {
95                         return validate(validators, bbName, (BuildingBlockExecution)obj);
96                 } else {
97                         return false;
98                 }
99         }
100         
101         protected boolean validate(List<? extends FlowValidator> validators, String bbName, BuildingBlockExecution execution) {
102                 List<Pair<String, Optional<String>>> results = runValidations(validators, bbName, execution);
103                 
104                 if (!results.isEmpty()) {
105                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
106                                         "Failed Validations:\n" + results.stream().map(item -> String.format("%s: %s", item.getValue0(), item.getValue1().get())).collect(Collectors.joining("\n")));
107                 }
108                 
109                 return true;
110                 
111         }
112         protected List<Pair<String, Optional<String>>> runValidations(List<? extends FlowValidator> validators, String bbName, BuildingBlockExecution execution) {
113                 
114                 List<FlowValidator> filtered = filterValidators(validators, bbName);
115                 
116                 List<Pair<String,Optional<String>>> results = new ArrayList<>();
117                 filtered.forEach(item -> results.add(new Pair<>(item.getClass().getName(), item.validate(execution))));
118                 
119                 return results.stream().filter(item -> item.getValue1().isPresent()).collect(Collectors.toList());
120         }
121         
122         protected List<FlowValidator> filterValidators(List<? extends FlowValidator> validators, String bbName) {
123                 return validators.stream()
124                                 .filter(item -> {
125                                         return !item.getClass().isAnnotationPresent(Skip.class) && item.shouldRunFor(bbName);
126                                 })
127                                 .sorted(Comparator.comparing(item -> {
128                                         Priority p = Optional.ofNullable(item.getClass().getAnnotation(Priority.class)).orElse(new Priority() {
129                                                 public int value() {
130                                                         return 1000;
131                                                 }
132
133                                                 @Override
134                                                 public Class<? extends Annotation> annotationType() {
135                                                         return Priority.class;
136                                                 }
137                                         });
138                                         return p.value();
139                                 })).collect(Collectors.toList());
140         }
141         
142         protected <T> List<T> buildalidatorList(Reflections reflections, Class<T> clazz) {
143                 List<T> result = new ArrayList<>();
144                 try {
145                         for (Class<? extends T> klass : reflections.getSubTypesOf(clazz)) {
146                                 result.add(klass.newInstance());
147                         }
148                 } catch (InstantiationException | IllegalAccessException e) {
149                         logger.error("failed to build validator list for {}", clazz.getName(), e);
150                         throw new RuntimeException(e);
151                 }
152                 
153                 return result;
154         }
155         
156         protected abstract List<S> getPreFlowValidators();
157         
158         protected abstract List<E> getPostFlowValidators();
159         
160 }