5561a97ef046cca1441dad9fff477d47bdf716e9
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / TypedOperator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.policy.controlloop.actorserviceprovider.impl;
22
23 import java.util.Map;
24 import lombok.Getter;
25 import org.onap.policy.controlloop.actorserviceprovider.Operation;
26 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
27 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
28
29 /**
30  * Operator with typed parameter information.
31  *
32  * @param <C> type of configuration data
33  * @param <T> type of operation that the operator creates
34  */
35 public abstract class TypedOperator<C, T extends Operation> extends OperatorPartial {
36
37     /**
38      * Function to make an operation.
39      */
40     private final OperationMaker<C, T> operationMaker;
41
42     /**
43      * Current configuration. This is set by {@link #doConfigure(Map)}.
44      */
45     @Getter
46     private C currentConfig;
47
48
49     /**
50      * Constructs the object.
51      *
52      * @param actorName name of the actor with which this operator is associated
53      * @param name operation name
54      */
55     protected TypedOperator(String actorName, String name) {
56         this(actorName, name, null);
57     }
58
59     /**
60      * Constructs the object.
61      *
62      * @param actorName name of the actor with which this operator is associated
63      * @param name operation name
64      * @param operationMaker function to make an operation
65      */
66     public TypedOperator(String actorName, String name, OperationMaker<C, T> operationMaker) {
67         super(actorName, name);
68         this.operationMaker = operationMaker;
69     }
70
71     /**
72      * Translates the parameters, saving the relevant configuration data.
73      */
74     @Override
75     protected void doConfigure(Map<String, Object> parameters) {
76         currentConfig = makeConfiguration(parameters);
77     }
78
79     /**
80      * Makes a new configuration using the specified parameters.
81      *
82      * @param parameters operator parameters
83      * @return a new configuration
84      */
85     protected abstract C makeConfiguration(Map<String, Object> parameters);
86
87     @Override
88     public T buildOperation(ControlLoopOperationParams params) {
89         if (operationMaker == null) {
90             throw new UnsupportedOperationException("cannot make operation for " + getFullName());
91         }
92
93         verifyRunning();
94
95         return operationMaker.apply(params, currentConfig);
96     }
97 }