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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.analytics.tca.core.util.function.calculation;
22 import java.util.function.Function;
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;
30 * Functional interface which all TCA calculation functions should implement
32 * @author Rajiv Singla
35 public interface TcaCalculationFunction extends Function<TcaExecutionContext, TcaExecutionContext> {
37 Logger logger = LoggerFactory.getLogger(TcaCalculationFunction.class);
39 TcaExecutionContext calculate(TcaExecutionContext tcaExecutionContext);
42 default TcaExecutionContext preCalculation(TcaExecutionContext tcaExecutionContext) {
44 return tcaExecutionContext;
47 default TcaExecutionContext postCalculation(TcaExecutionContext tcaExecutionContext) {
49 return tcaExecutionContext;
53 default TcaExecutionContext apply(TcaExecutionContext tcaExecutionContext) {
54 // triggers pre calculate, calculate nad post calculate
55 return postCalculation(calculate(preCalculation(tcaExecutionContext)));
59 * Updates TCA Processing context with early terminating message or error message
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
65 * @return updated tca execution context with early termination or error message and continue processing flag as
68 default TcaExecutionContext setTerminatingMessage(final String terminatingMessage,
69 final TcaExecutionContext tcaExecutionContext,
70 final boolean isErrorMessage) {
71 final TcaProcessingContext tcaProcessingContext = tcaExecutionContext.getTcaProcessingContext();
73 logger.error("Request Id: {}. Error Message: {}", tcaExecutionContext.getRequestId(), terminatingMessage);
74 tcaProcessingContext.setErrorMessage(terminatingMessage);
76 logger.debug("Request Id: {}. Early Termination Message: {}", tcaExecutionContext.getRequestId(),
78 tcaProcessingContext.setEarlyTerminationMessage(terminatingMessage);
80 tcaProcessingContext.setContinueProcessing(false);
81 return tcaExecutionContext;