Fix return building on policy get
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / TopicPairOperator.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 java.util.function.BiFunction;
27 import lombok.Getter;
28 import org.onap.policy.common.parameters.ValidationResult;
29 import org.onap.policy.controlloop.actorserviceprovider.Operation;
30 import org.onap.policy.controlloop.actorserviceprovider.Util;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.TopicPairParams;
34 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
35 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
36 import org.onap.policy.controlloop.actorserviceprovider.topic.TopicPair;
37 import org.onap.policy.controlloop.actorserviceprovider.topic.TopicPairManager;
38
39 /**
40  * Operator that uses a pair of topics, one for publishing the request, and another for
41  * receiving the response. Topic operators may share a {@link TopicPair}.
42  */
43 public abstract class TopicPairOperator extends OperatorPartial {
44
45     /**
46      * Manager from which to get the topic pair.
47      */
48     private final TopicPairManager pairManager;
49
50     /**
51      * Keys used to extract the fields used to select responses for this operator.
52      */
53     private final List<SelectorKey> selectorKeys;
54
55     /*
56      * The remaining fields are initialized when configure() is invoked, thus they may
57      * change.
58      */
59
60     /**
61      * Current parameters. While {@link params} may change, the values contained within it
62      * will not, thus operations may copy it.
63      */
64     @Getter
65     private TopicPairParams params;
66
67     /**
68      * Topic pair associated with the parameters.
69      */
70     @Getter
71     private TopicPair topicPair;
72
73     /**
74      * Forwarder associated with the parameters.
75      */
76     @Getter
77     private Forwarder forwarder;
78
79
80     /**
81      * Constructs the object.
82      *
83      * @param actorName name of the actor with which this operator is associated
84      * @param name operation name
85      * @param pairManager manager from which to get the topic pair
86      * @param selectorKeys keys used to extract the fields used to select responses for
87      *        this operator
88      */
89     public TopicPairOperator(String actorName, String name, TopicPairManager pairManager,
90                     List<SelectorKey> selectorKeys) {
91         super(actorName, name);
92         this.pairManager = pairManager;
93         this.selectorKeys = selectorKeys;
94     }
95
96     @Override
97     protected void doConfigure(Map<String, Object> parameters) {
98         params = Util.translate(getFullName(), parameters, TopicPairParams.class);
99         ValidationResult result = params.validate(getFullName());
100         if (!result.isValid()) {
101             throw new ParameterValidationRuntimeException("invalid parameters", result);
102         }
103
104         topicPair = pairManager.getTopicPair(params.getSource(), params.getTarget());
105         forwarder = topicPair.addForwarder(selectorKeys);
106     }
107
108     /**
109      * Makes an operator that will construct operations.
110      *
111      * @param <Q> request type
112      * @param <S> response type
113      * @param actorName actor name
114      * @param operation operation name
115      * @param pairManager manager from which to get the topic pair
116      * @param operationMaker function to make an operation
117      * @param keys keys used to extract the fields used to select responses for this
118      *        operator
119      * @return a new operator
120      */
121     // @formatter:off
122     public static <Q,S> TopicPairOperator makeOperator(String actorName, String operation, TopicPairManager pairManager,
123                     BiFunction<ControlLoopOperationParams, TopicPairOperator, TopicPairOperation<Q,S>> operationMaker,
124                     SelectorKey... keys) {
125         // @formatter:off
126
127         return makeOperator(actorName, operation, pairManager, Arrays.asList(keys), operationMaker);
128     }
129
130     /**
131      * Makes an operator that will construct operations.
132      *
133      * @param <Q> request type
134      * @param <S> response type
135      * @param actorName actor name
136      * @param operation operation name
137      * @param pairManager manager from which to get the topic pair
138      * @param keys keys used to extract the fields used to select responses for
139      *        this operator
140      * @param operationMaker function to make an operation
141      * @return a new operator
142      */
143     // @formatter:off
144     public static <Q,S> TopicPairOperator makeOperator(String actorName, String operation, TopicPairManager pairManager,
145                     List<SelectorKey> keys,
146                     BiFunction<ControlLoopOperationParams, TopicPairOperator, TopicPairOperation<Q,S>> operationMaker) {
147         // @formatter:on
148
149         return new TopicPairOperator(actorName, operation, pairManager, keys) {
150             @Override
151             public synchronized Operation buildOperation(ControlLoopOperationParams params) {
152                 return operationMaker.apply(params, this);
153             }
154         };
155     }
156 }