c3e1e5c4d4a81467db1cfa299358aab733dae6dd
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / TopicPairActor.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.Map;
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;
34
35 /**
36  * Actor that uses a topic pair. The actor's parameters must be a
37  * {@link TopicPairActorParams}.
38  */
39 public class TopicPairActor extends ActorImpl implements TopicPairManager {
40
41     /**
42      * Maps a topic source and target name to its topic pair.
43      */
44     private final Map<Pair<String, String>, TopicPair> params2topic = new ConcurrentHashMap<>();
45
46
47     /**
48      * Constructs the object.
49      *
50      * @param name actor's name
51      */
52     public TopicPairActor(String name) {
53         super(name);
54     }
55
56     @Override
57     protected void doStart() {
58         params2topic.values().forEach(TopicPair::start);
59         super.doStart();
60     }
61
62     @Override
63     protected void doStop() {
64         params2topic.values().forEach(TopicPair::stop);
65         super.doStop();
66     }
67
68     @Override
69     protected void doShutdown() {
70         params2topic.values().forEach(TopicPair::shutdown);
71         params2topic.clear();
72         super.doShutdown();
73     }
74
75     @Override
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));
79     }
80
81     /**
82      * Translates the parameters to a {@link TopicPairActorParams} and then creates a
83      * function that will extract operator-specific parameters.
84      */
85     @Override
86     protected Function<String, Map<String, Object>> makeOperatorParameters(Map<String, Object> actorParameters) {
87         String actorName = getName();
88
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);
93         }
94
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();
98
99         return operationName -> {
100             Map<String, Object> specificParams = operations.get(operationName);
101             if (specificParams == null) {
102                 return null;
103             }
104
105             // start with a copy of defaults and overlay with specific
106             Map<String, Object> subparams = new TreeMap<>(defaultParams);
107             subparams.putAll(specificParams);
108
109             return Util.translateToMap(getName() + "." + operationName, subparams);
110         };
111     }
112 }