Add actor for CDS
[policy/models.git] / models-interactions / model-actors / actor.cds / src / main / java / org / onap / policy / controlloop / actor / cds / GrpcOperator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Bell Canada. 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 package org.onap.policy.controlloop.actor.cds;
20
21 import java.util.Map;
22 import lombok.Getter;
23 import org.onap.policy.cds.properties.CdsServerProperties;
24 import org.onap.policy.common.parameters.ValidationResult;
25 import org.onap.policy.controlloop.actorserviceprovider.Operation;
26 import org.onap.policy.controlloop.actorserviceprovider.Util;
27 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
28 import org.onap.policy.controlloop.actorserviceprovider.impl.OperatorPartial;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
31
32 /**
33  * Operator that uses gRPC. The operator's parameters must be a
34  * {@link CdsServerProperties}.
35  */
36 @Getter
37 public class GrpcOperator extends OperatorPartial {
38
39     /**
40      * Function to make an operation.
41      */
42     private final OperationMaker<GrpcConfig, GrpcOperation> operationMaker;
43
44     /**
45      * Current configuration. This is set by {@link #doConfigure(Map)}.
46      */
47     private GrpcConfig currentConfig;
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     public GrpcOperator(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 GrpcOperator(String actorName, String name, OperationMaker<GrpcConfig, GrpcOperation> operationMaker) {
67         super(actorName, name);
68         this.operationMaker = operationMaker;
69     }
70
71     /**
72      * Translates the parameters to an {@link CdsServerProperties} and then extracts the
73      * relevant values.
74      */
75     @Override
76     protected void doConfigure(Map<String, Object> parameters) {
77         currentConfig = makeConfiguration(parameters);
78     }
79
80     /**
81      * Makes a new configuration using the specified parameters.
82      *
83      * @param parameters operator parameters
84      * @return a new configuration
85      */
86     protected GrpcConfig makeConfiguration(Map<String, Object> parameters) {
87         CdsServerProperties params = Util.translate(getFullName(), parameters, CdsServerProperties.class);
88         ValidationResult result = params.validate();
89         if (!result.isValid()) {
90             throw new ParameterValidationRuntimeException("invalid parameters", result);
91         }
92         return new GrpcConfig(getBlockingExecutor(), params);
93     }
94
95     /**
96      * {@inheritDoc}.
97      */
98     @Override
99     public Operation buildOperation(ControlLoopOperationParams params) {
100         if (operationMaker == null) {
101             throw new UnsupportedOperationException("cannot make operation for " + getFullName());
102         }
103         verifyRunning();
104         return operationMaker.apply(params, currentConfig);
105     }
106 }