2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.startstop;
24 import java.util.Arrays;
25 import java.util.Properties;
26 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
27 import org.onap.policy.common.endpoints.event.comm.TopicSource;
28 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
29 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
30 import org.onap.policy.common.parameters.ParameterService;
31 import org.onap.policy.common.utils.services.Registry;
32 import org.onap.policy.common.utils.services.ServiceManagerContainer;
33 import org.onap.policy.models.pdp.concepts.PdpStatus;
34 import org.onap.policy.models.pdp.enums.PdpMessageType;
35 import org.onap.policy.pap.main.PapConstants;
36 import org.onap.policy.pap.main.PolicyPapRuntimeException;
37 import org.onap.policy.pap.main.parameters.PapParameterGroup;
38 import org.onap.policy.pap.main.rest.PapRestServer;
39 import org.onap.policy.pap.main.rest.PapStatisticsManager;
42 * This class wraps a distributor so that it can be activated as a complete service
43 * together with all its pap and forwarding handlers.
45 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47 public class PapActivator extends ServiceManagerContainer {
48 private static final String[] MSG_TYPE_NAMES = {"messageName"};
49 private static final String[] REQ_ID_NAMES = {"response", "responseTo"};
51 private final PapParameterGroup papParameterGroup;
54 * The PAP REST API server.
56 private PapRestServer restServer;
59 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message,
60 * and then dispatches them to {@link #reqIdDispatcher}.
62 private final MessageTypeDispatcher msgDispatcher;
65 * Listens for {@link PdpStatus} messages and then routes them to the listener
66 * associated with the ID of the originating request.
68 private final RequestIdDispatcher<PdpStatus> reqIdDispatcher;
71 * Instantiate the activator for policy pap as a complete service.
73 * @param papParameterGroup the parameters for the pap service
74 * @param topicProperties properties used to configure the topics
76 public PapActivator(final PapParameterGroup papParameterGroup, Properties topicProperties) {
79 TopicEndpoint.manager.addTopicSinks(topicProperties);
80 TopicEndpoint.manager.addTopicSources(topicProperties);
83 this.papParameterGroup = papParameterGroup;
84 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
86 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
87 this.reqIdDispatcher = new RequestIdDispatcher<>(PdpStatus.class, REQ_ID_NAMES);
89 } catch (RuntimeException e) {
90 throw new PolicyPapRuntimeException(e);
93 this.msgDispatcher.register(PdpMessageType.PDP_STATUS.name(), this.reqIdDispatcher);
95 final Object pdpUpdateLock = new Object();
98 addAction("PAP parameters",
99 () -> ParameterService.register(papParameterGroup),
100 () -> ParameterService.deregister(papParameterGroup.getName()));
102 addAction("dispatcher",
103 () -> registerDispatcher(),
104 () -> unregisterDispatcher());
107 () -> TopicEndpoint.manager.start(),
108 () -> TopicEndpoint.manager.shutdown());
110 addAction("PAP statistics",
111 () -> Registry.register(PapConstants.REG_STATISTICS_MANAGER, new PapStatisticsManager()),
112 () -> Registry.unregister(PapConstants.REG_STATISTICS_MANAGER));
114 addAction("PDP modification lock",
115 () -> Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, pdpUpdateLock),
116 () -> Registry.unregister(PapConstants.REG_PDP_MODIFY_LOCK));
118 addAction("REST server",
119 () -> restServer = new PapRestServer(papParameterGroup.getRestServerParameters()),
122 addAction("REST server thread",
123 () -> restServer.start(),
124 () -> restServer.stop());
129 * Get the parameters used by the activator.
131 * @return the parameters of the activator
133 public PapParameterGroup getParameterGroup() {
134 return papParameterGroup;
138 * Registers the dispatcher with the topic source(s).
140 private void registerDispatcher() {
141 for (TopicSource source : TopicEndpoint.manager
142 .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) {
143 source.register(msgDispatcher);
148 * Unregisters the dispatcher from the topic source(s).
150 private void unregisterDispatcher() {
151 for (TopicSource source : TopicEndpoint.manager
152 .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) {
153 source.unregister(msgDispatcher);