Moving common polling code into HttpOperation
[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 org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.controlloop.actorserviceprovider.Util;
28 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
31 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
32 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
33 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
34
35 /**
36  * Operator that uses a bidirectional topic. Topic operators may share a
37  * {@link BidirectionalTopicHandler}.
38  */
39 public class BidirectionalTopicOperator
40                 extends TypedOperator<BidirectionalTopicConfig, BidirectionalTopicOperation<?, ?>> {
41
42     /**
43      * Manager from which to get the topic handlers.
44      */
45     private final BidirectionalTopicManager topicManager;
46
47     /**
48      * Keys used to extract the fields used to select responses for this operator.
49      */
50     private final List<SelectorKey> selectorKeys;
51
52
53     /**
54      * Constructs the object.
55      *
56      * @param actorName name of the actor with which this operator is associated
57      * @param name operation name
58      * @param topicManager manager from which to get the topic handler
59      * @param selectorKeys keys used to extract the fields used to select responses for
60      *        this operator
61      */
62     protected BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
63                     List<SelectorKey> selectorKeys) {
64         this(actorName, name, topicManager, selectorKeys, null);
65     }
66
67     /**
68      * Constructs the object.
69      *
70      * @param actorName name of the actor with which this operator is associated
71      * @param name operation name
72      * @param topicManager manager from which to get the topic handler
73      * @param selectorKeys keys used to extract the fields used to select responses for
74      *        this operator
75      */
76     public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
77                     List<SelectorKey> selectorKeys,
78                     OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation<?, ?>> operationMaker) {
79
80         super(actorName, name, operationMaker);
81         this.topicManager = topicManager;
82         this.selectorKeys = selectorKeys;
83     }
84
85     /**
86      * Constructs the object.
87      *
88      * @param actorName name of the actor with which this operator is associated
89      * @param name operation name
90      * @param topicManager manager from which to get the topic handler
91      * @param selectorKeys keys used to extract the fields used to select responses for
92      *        this operator
93      */
94     public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
95                     OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation<?, ?>> operationMaker,
96                     SelectorKey... selectorKeys) {
97         this(actorName, name, topicManager, Arrays.asList(selectorKeys), operationMaker);
98     }
99
100     /**
101      * Makes a new configuration using the specified parameters.
102      *
103      * @param parameters operator parameters
104      * @return a new configuration
105      */
106     protected BidirectionalTopicConfig makeConfiguration(Map<String, Object> parameters) {
107         BidirectionalTopicParams params = Util.translate(getFullName(), parameters, BidirectionalTopicParams.class);
108         ValidationResult result = params.validate(getFullName());
109         if (!result.isValid()) {
110             throw new ParameterValidationRuntimeException("invalid parameters", result);
111         }
112
113         return new BidirectionalTopicConfig(getBlockingExecutor(), params, topicManager, selectorKeys);
114     }
115 }