Add SO actor
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / SoOperator.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.actor.so;
22
23 import java.util.Map;
24 import java.util.function.BiFunction;
25 import lombok.Getter;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.controlloop.actorserviceprovider.Operation;
28 import org.onap.policy.controlloop.actorserviceprovider.Util;
29 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
32
33 @Getter
34 public abstract class SoOperator extends HttpOperator {
35
36     /**
37      * Path to use for the "get" request. A trailing "/" is added, if it is missing.
38      */
39     private String pathGet;
40
41     /**
42      * Maximum number of "get" requests permitted, after the initial request, to retrieve
43      * the response.
44      */
45     private int maxGets;
46
47     /**
48      * Time, in seconds, to wait between issuing "get" requests.
49      */
50     private int waitSecGet;
51
52
53     public SoOperator(String actorName, String name) {
54         super(actorName, name);
55     }
56
57     @Override
58     protected void doConfigure(Map<String, Object> parameters) {
59         SoParams params = Util.translate(getFullName(), parameters, SoParams.class);
60         ValidationResult result = params.validate(getFullName());
61         if (!result.isValid()) {
62             throw new ParameterValidationRuntimeException("invalid parameters", result);
63         }
64
65         this.pathGet = params.getPathGet() + (params.getPathGet().endsWith("/") ? "" : "/");
66         this.maxGets = params.getMaxGets();
67         this.waitSecGet = params.getWaitSecGet();
68
69         super.doConfigure(params);
70     }
71
72     /**
73      * Makes an operator that will construct operations.
74      *
75      * @param actorName actor name
76      * @param operation operation name
77      * @param operationMaker function to make an operation
78      * @return a new operator
79      */
80     public static SoOperator makeSoOperator(String actorName, String operation,
81                     BiFunction<ControlLoopOperationParams, SoOperator, SoOperation> operationMaker) {
82
83         return new SoOperator(actorName, operation) {
84             @Override
85             public Operation buildOperation(ControlLoopOperationParams params) {
86                 return operationMaker.apply(params, this);
87             }
88         };
89     }
90 }