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;
24 import java.util.TreeMap;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.function.Function;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.onap.policy.common.parameters.ValidationResult;
29 import org.onap.policy.controlloop.actorserviceprovider.Util;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.TopicPairActorParams;
32 import org.onap.policy.controlloop.actorserviceprovider.topic.TopicPair;
33 import org.onap.policy.controlloop.actorserviceprovider.topic.TopicPairManager;
36 * Actor that uses a topic pair. The actor's parameters must be a
37 * {@link TopicPairActorParams}.
39 public class TopicPairActor extends ActorImpl implements TopicPairManager {
42 * Maps a topic source and target name to its topic pair.
44 private final Map<Pair<String, String>, TopicPair> params2topic = new ConcurrentHashMap<>();
48 * Constructs the object.
50 * @param name actor's name
52 public TopicPairActor(String name) {
57 protected void doStart() {
58 params2topic.values().forEach(TopicPair::start);
63 protected void doStop() {
64 params2topic.values().forEach(TopicPair::stop);
69 protected void doShutdown() {
70 params2topic.values().forEach(TopicPair::shutdown);
76 public TopicPair getTopicPair(String source, String target) {
77 Pair<String, String> key = Pair.of(source, target);
78 return params2topic.computeIfAbsent(key, pair -> new TopicPair(source, target));
82 * Translates the parameters to a {@link TopicPairActorParams} and then creates a
83 * function that will extract operator-specific parameters.
86 protected Function<String, Map<String, Object>> makeOperatorParameters(Map<String, Object> actorParameters) {
87 String actorName = getName();
89 TopicPairActorParams params = Util.translate(actorName, actorParameters, TopicPairActorParams.class);
90 ValidationResult result = params.validate(getName());
91 if (!result.isValid()) {
92 throw new ParameterValidationRuntimeException("invalid parameters", result);
95 // create a map of the default parameters
96 Map<String, Object> defaultParams = Util.translateToMap(getName(), params.getDefaults());
97 Map<String, Map<String, Object>> operations = params.getOperation();
99 return operationName -> {
100 Map<String, Object> specificParams = operations.get(operationName);
101 if (specificParams == null) {
105 // start with a copy of defaults and overlay with specific
106 Map<String, Object> subparams = new TreeMap<>(defaultParams);
107 subparams.putAll(specificParams);
109 return Util.translateToMap(getName() + "." + operationName, subparams);