ec9f7bd5a93f8e9c6dc437482c549f6f892781a2
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-core / src / main / java / org / onap / dcae / analytics / tca / core / util / function / calculation / TcaCalculationFunction.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.core.util.function.calculation;
21
22 import java.util.function.Function;
23
24 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
25 import org.onap.dcae.analytics.tca.core.service.TcaProcessingContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Functional interface which all TCA calculation functions should implement
31  *
32  * @author Rajiv Singla
33  */
34 @FunctionalInterface
35 public interface TcaCalculationFunction extends Function<TcaExecutionContext, TcaExecutionContext> {
36
37     Logger logger = LoggerFactory.getLogger(TcaCalculationFunction.class);
38
39     TcaExecutionContext calculate(TcaExecutionContext tcaExecutionContext);
40
41
42     default TcaExecutionContext preCalculation(TcaExecutionContext tcaExecutionContext) {
43         // do nothing
44         return tcaExecutionContext;
45     }
46
47     default TcaExecutionContext postCalculation(TcaExecutionContext tcaExecutionContext) {
48         // do nothing
49         return tcaExecutionContext;
50     }
51
52     @Override
53     default TcaExecutionContext apply(TcaExecutionContext tcaExecutionContext) {
54         // triggers pre calculate, calculate nad post calculate
55         return postCalculation(calculate(preCalculation(tcaExecutionContext)));
56     }
57
58     /**
59      * Updates TCA Processing context with early terminating message or error message
60      *
61      * @param terminatingMessage terminating message to be set in processing context
62      * @param tcaExecutionContext current tca execution context
63      * @param isErrorMessage specifies if terminating message is an error message
64      *
65      * @return updated tca execution context with early termination or error message and continue processing flag as
66      * false
67      */
68     default TcaExecutionContext setTerminatingMessage(final String terminatingMessage,
69                                                       final TcaExecutionContext tcaExecutionContext,
70                                                       final boolean isErrorMessage) {
71         final TcaProcessingContext tcaProcessingContext = tcaExecutionContext.getTcaProcessingContext();
72         if (isErrorMessage) {
73             logger.error("Request Id: {}. Error Message: {}", tcaExecutionContext.getRequestId(), terminatingMessage);
74             tcaProcessingContext.setErrorMessage(terminatingMessage);
75         } else {
76             logger.debug("Request Id: {}. Early Termination Message: {}", tcaExecutionContext.getRequestId(),
77                     terminatingMessage);
78             tcaProcessingContext.setEarlyTerminationMessage(terminatingMessage);
79         }
80         tcaProcessingContext.setContinueProcessing(false);
81         return tcaExecutionContext;
82     }
83
84 }