2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
23 import java.util.Arrays;
24 import java.util.List;
26 import java.util.function.BiFunction;
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;
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}.
43 public abstract class TopicPairOperator extends OperatorPartial {
46 * Manager from which to get the topic pair.
48 private final TopicPairManager pairManager;
51 * Keys used to extract the fields used to select responses for this operator.
53 private final List<SelectorKey> selectorKeys;
56 * The remaining fields are initialized when configure() is invoked, thus they may
61 * Current parameters. While {@link params} may change, the values contained within it
62 * will not, thus operations may copy it.
65 private TopicPairParams params;
68 * Topic pair associated with the parameters.
71 private TopicPair topicPair;
74 * Forwarder associated with the parameters.
77 private Forwarder forwarder;
81 * Constructs the object.
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
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;
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);
104 topicPair = pairManager.getTopicPair(params.getSource(), params.getTarget());
105 forwarder = topicPair.addForwarder(selectorKeys);
109 * Makes an operator that will construct operations.
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
119 * @return a new operator
122 public static <Q,S> TopicPairOperator makeOperator(String actorName, String operation, TopicPairManager pairManager,
123 BiFunction<ControlLoopOperationParams, TopicPairOperator, TopicPairOperation<Q,S>> operationMaker,
124 SelectorKey... keys) {
127 return makeOperator(actorName, operation, pairManager, Arrays.asList(keys), operationMaker);
131 * Makes an operator that will construct operations.
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
140 * @param operationMaker function to make an operation
141 * @return a new operator
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) {
149 return new TopicPairOperator(actorName, operation, pairManager, keys) {
151 public synchronized Operation buildOperation(ControlLoopOperationParams params) {
152 return operationMaker.apply(params, this);