More actor clean-up
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / BidirectionalTopicOperator.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.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import lombok.Getter;
27 import org.onap.policy.common.parameters.ValidationResult;
28 import org.onap.policy.controlloop.actorserviceprovider.Operation;
29 import org.onap.policy.controlloop.actorserviceprovider.Util;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
35 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
36 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
37 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
38
39 /**
40  * Operator that uses a bidirectional topic. Topic operators may share a
41  * {@link BidirectionalTopicHandler}.
42  */
43 public class BidirectionalTopicOperator extends OperatorPartial {
44
45     /**
46      * Function to make an operation.
47      */
48     @SuppressWarnings("rawtypes")
49     private final OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> operationMaker;
50
51     /**
52      * Manager from which to get the topic handlers.
53      */
54     private final BidirectionalTopicManager topicManager;
55
56     /**
57      * Keys used to extract the fields used to select responses for this operator.
58      */
59     private final List<SelectorKey> selectorKeys;
60
61     /**
62      * Current configuration. This is set by {@link #doConfigure(Map)}.
63      */
64     @Getter
65     private BidirectionalTopicConfig currentConfig;
66
67
68     /**
69      * Constructs the object.
70      *
71      * @param actorName name of the actor with which this operator is associated
72      * @param name operation name
73      * @param topicManager manager from which to get the topic handler
74      * @param selectorKeys keys used to extract the fields used to select responses for
75      *        this operator
76      */
77     protected BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
78                     List<SelectorKey> selectorKeys) {
79         this(actorName, name, topicManager, selectorKeys, null);
80     }
81
82     /**
83      * Constructs the object.
84      *
85      * @param actorName name of the actor with which this operator is associated
86      * @param name operation name
87      * @param topicManager manager from which to get the topic handler
88      * @param selectorKeys keys used to extract the fields used to select responses for
89      *        this operator
90      */
91     // @formatter:off
92     public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
93                     List<SelectorKey> selectorKeys,
94                     @SuppressWarnings("rawtypes") OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation>
95                         operationMaker) {
96         // @formatter:on
97
98         super(actorName, name);
99         this.topicManager = topicManager;
100         this.selectorKeys = selectorKeys;
101         this.operationMaker = operationMaker;
102     }
103
104     /**
105      * Constructs the object.
106      *
107      * @param actorName name of the actor with which this operator is associated
108      * @param name operation name
109      * @param topicManager manager from which to get the topic handler
110      * @param selectorKeys keys used to extract the fields used to select responses for
111      *        this operator
112      */
113     // @formatter:off
114     public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
115                     @SuppressWarnings("rawtypes") OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation>
116                         operationMaker,
117                     SelectorKey... selectorKeys) {
118         // @formatter:on
119         this(actorName, name, topicManager, Arrays.asList(selectorKeys), operationMaker);
120     }
121
122     /**
123      * Translates the parameters to an {@link HttpParams} and then extracts the relevant
124      * values.
125      */
126     @Override
127     protected void doConfigure(Map<String, Object> parameters) {
128         currentConfig = makeConfiguration(parameters);
129     }
130
131     /**
132      * Makes a new configuration using the specified parameters.
133      *
134      * @param parameters operator parameters
135      * @return a new configuration
136      */
137     protected BidirectionalTopicConfig makeConfiguration(Map<String, Object> parameters) {
138         BidirectionalTopicParams params = Util.translate(getFullName(), parameters, BidirectionalTopicParams.class);
139         ValidationResult result = params.validate(getFullName());
140         if (!result.isValid()) {
141             throw new ParameterValidationRuntimeException("invalid parameters", result);
142         }
143
144         return new BidirectionalTopicConfig(getBlockingExecutor(), params, topicManager, selectorKeys);
145     }
146
147     @Override
148     public Operation buildOperation(ControlLoopOperationParams params) {
149         if (operationMaker == null) {
150             throw new UnsupportedOperationException("cannot make operation for " + getFullName());
151         }
152
153         verifyRunning();
154
155         return operationMaker.apply(params, currentConfig);
156     }
157 }