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;
 
  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;
 
  40  * Operator that uses a bidirectional topic. Topic operators may share a
 
  41  * {@link BidirectionalTopicHandler}.
 
  43 public class BidirectionalTopicOperator extends OperatorPartial {
 
  46      * Function to make an operation.
 
  48     @SuppressWarnings("rawtypes")
 
  49     private final OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> operationMaker;
 
  52      * Manager from which to get the topic handlers.
 
  54     private final BidirectionalTopicManager topicManager;
 
  57      * Keys used to extract the fields used to select responses for this operator.
 
  59     private final List<SelectorKey> selectorKeys;
 
  62      * Current configuration. This is set by {@link #doConfigure(Map)}.
 
  65     private BidirectionalTopicConfig currentConfig;
 
  69      * Constructs the object.
 
  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
 
  77     protected BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
 
  78                     List<SelectorKey> selectorKeys) {
 
  79         this(actorName, name, topicManager, selectorKeys, null);
 
  83      * Constructs the object.
 
  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
 
  92     public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
 
  93                     List<SelectorKey> selectorKeys,
 
  94                     @SuppressWarnings("rawtypes") OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation>
 
  98         super(actorName, name);
 
  99         this.topicManager = topicManager;
 
 100         this.selectorKeys = selectorKeys;
 
 101         this.operationMaker = operationMaker;
 
 105      * Constructs the object.
 
 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
 
 114     public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager,
 
 115                     @SuppressWarnings("rawtypes") OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation>
 
 117                     SelectorKey... selectorKeys) {
 
 119         this(actorName, name, topicManager, Arrays.asList(selectorKeys), operationMaker);
 
 123      * Translates the parameters to an {@link HttpParams} and then extracts the relevant
 
 127     protected void doConfigure(Map<String, Object> parameters) {
 
 128         currentConfig = makeConfiguration(parameters);
 
 132      * Makes a new configuration using the specified parameters.
 
 134      * @param parameters operator parameters
 
 135      * @return a new configuration
 
 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);
 
 144         return new BidirectionalTopicConfig(getBlockingExecutor(), params, topicManager, selectorKeys);
 
 148     public Operation buildOperation(ControlLoopOperationParams params) {
 
 149         if (operationMaker == null) {
 
 150             throw new UnsupportedOperationException("cannot make operation for " + getFullName());
 
 155         return operationMaker.apply(params, currentConfig);