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
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=========================================================
19 package org.onap.policy.controlloop.actor.cds;
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;
33 * Operator that uses gRPC. The operator's parameters must be a
34 * {@link CdsServerProperties}.
37 public class GrpcOperator extends OperatorPartial {
40 * Function to make an operation.
42 private final OperationMaker<GrpcConfig, GrpcOperation> operationMaker;
45 * Current configuration. This is set by {@link #doConfigure(Map)}.
47 private GrpcConfig currentConfig;
50 * Constructs the object.
52 * @param actorName name of the actor with which this operator is associated
53 * @param name operation name
55 public GrpcOperator(String actorName, String name) {
56 this(actorName, name, null);
60 * Constructs the object.
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
66 public GrpcOperator(String actorName, String name, OperationMaker<GrpcConfig, GrpcOperation> operationMaker) {
67 super(actorName, name);
68 this.operationMaker = operationMaker;
72 * Translates the parameters to an {@link CdsServerProperties} and then extracts the
76 protected void doConfigure(Map<String, Object> parameters) {
77 currentConfig = makeConfiguration(parameters);
81 * Makes a new configuration using the specified parameters.
83 * @param parameters operator parameters
84 * @return a new configuration
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);
92 return new GrpcConfig(getBlockingExecutor(), params);
99 public Operation buildOperation(ControlLoopOperationParams params) {
100 if (operationMaker == null) {
101 throw new UnsupportedOperationException("cannot make operation for " + getFullName());
104 return operationMaker.apply(params, currentConfig);