Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-common / src / main / java / org / openecomp / dcae / apod / analytics / common / service / processor / GenericMessageChainProcessor.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 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.openecomp.dcae.apod.analytics.common.service.processor;
22
23 import org.openecomp.dcae.apod.analytics.common.utils.MessageProcessorUtils.MessageProcessorFunction;
24
25 import java.util.List;
26
27 import static org.openecomp.dcae.apod.analytics.common.utils.MessageProcessorUtils.computeMessageProcessorChain;
28
29 /**
30  * <p>
31  *     A Generic Message Processor which passes the {@link ProcessorContext} from first to second
32  *     {@link MessageProcessor}
33  * </p>
34  *
35  * @param <P> Processor Context sub classes
36  *
37  * @author Rajiv Singla . Creation Date: 11/8/2016.
38  */
39 public class GenericMessageChainProcessor<P extends ProcessorContext> {
40
41     private final List<? extends MessageProcessor<P>> messageProcessors;
42     private final P initialProcessorContext;
43
44     public GenericMessageChainProcessor(List<? extends MessageProcessor<P>> messageProcessors,
45                                         P initialProcessorContext) {
46         this.messageProcessors = messageProcessors;
47         this.initialProcessorContext = initialProcessorContext;
48     }
49
50     /**
51      * Process a processor chain
52      *
53      * @return Processor Context after processing the processor chain
54      */
55     public P processChain() {
56
57         final MessageProcessorFunction<P> messageProcessorFunction =
58                 new MessageProcessorFunction<P>() {
59                     @Override
60                     public <M extends MessageProcessor<P>> P apply(P context, M processor) {
61                         return processor.apply(context);
62                     }
63                 };
64
65         return computeMessageProcessorChain(messageProcessors, initialProcessorContext,
66                 messageProcessorFunction);
67
68     }
69
70 }