932b9a09f725a60bcde38b70df1485314af21d75
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
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  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.interfaces
19
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
22 import java.util.function.Function
23
24
25 interface BlueprintFunctionNode<T, R> : Function<T, R> {
26
27     fun getName(): String
28
29     @Throws(BluePrintProcessorException::class)
30     fun prepareRequest(executionRequest: T): T = runBlocking {
31         prepareRequestNB(executionRequest)
32     }
33
34     @Throws(BluePrintProcessorException::class)
35     fun process(executionRequest: T) = runBlocking {
36         processNB(executionRequest)
37     }
38
39     @Throws(BluePrintProcessorException::class)
40     fun recover(runtimeException: RuntimeException, executionRequest: T) = runBlocking {
41         recoverNB(runtimeException, executionRequest)
42     }
43
44     @Throws(BluePrintProcessorException::class)
45     fun prepareResponse(): R = runBlocking {
46         prepareResponseNB()
47     }
48
49     override fun apply(executionServiceInput: T): R {
50         try {
51             prepareRequest(executionServiceInput)
52             process(executionServiceInput)
53         } catch (runtimeException: RuntimeException) {
54             recover(runtimeException, executionServiceInput)
55         }
56         return prepareResponse()
57     }
58
59     @Throws(BluePrintProcessorException::class)
60     suspend fun prepareRequestNB(executionRequest: T): T
61
62     @Throws(BluePrintProcessorException::class)
63     suspend fun processNB(executionRequest: T)
64
65     @Throws(BluePrintProcessorException::class)
66     suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: T)
67
68     @Throws(BluePrintProcessorException::class)
69     suspend fun prepareResponseNB(): R
70
71     @Throws(BluePrintProcessorException::class)
72     suspend fun applyNB(t: T): R
73 }