Changes to PAP infrastructure to support PDP
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / startstop / PapActivator.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.startstop;
23
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;
40
41 /**
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.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
46  */
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"};
50
51     private final PapParameterGroup papParameterGroup;
52
53     /**
54      * The PAP REST API server.
55      */
56     private PapRestServer restServer;
57
58     /**
59      * Listens for messages on the topic, decodes them into a {@link PdpStatus} message,
60      * and then dispatches them to {@link #reqIdDispatcher}.
61      */
62     private final MessageTypeDispatcher msgDispatcher;
63
64     /**
65      * Listens for {@link PdpStatus} messages and then routes them to the listener
66      * associated with the ID of the originating request.
67      */
68     private final RequestIdDispatcher<PdpStatus> reqIdDispatcher;
69
70     /**
71      * Instantiate the activator for policy pap as a complete service.
72      *
73      * @param papParameterGroup the parameters for the pap service
74      * @param topicProperties properties used to configure the topics
75      */
76     public PapActivator(final PapParameterGroup papParameterGroup, Properties topicProperties) {
77         super("Policy PAP");
78
79         TopicEndpoint.manager.addTopicSinks(topicProperties);
80         TopicEndpoint.manager.addTopicSources(topicProperties);
81
82         try {
83             this.papParameterGroup = papParameterGroup;
84             papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
85
86             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
87             this.reqIdDispatcher = new RequestIdDispatcher<>(PdpStatus.class, REQ_ID_NAMES);
88
89         } catch (RuntimeException e) {
90             throw new PolicyPapRuntimeException(e);
91         }
92
93         this.msgDispatcher.register(PdpMessageType.PDP_STATUS.name(), this.reqIdDispatcher);
94
95         final Object pdpUpdateLock = new Object();
96
97         // @formatter:off
98         addAction("PAP parameters",
99             () -> ParameterService.register(papParameterGroup),
100             () -> ParameterService.deregister(papParameterGroup.getName()));
101
102         addAction("dispatcher",
103             () -> registerDispatcher(),
104             () -> unregisterDispatcher());
105
106         addAction("topics",
107             () -> TopicEndpoint.manager.start(),
108             () -> TopicEndpoint.manager.shutdown());
109
110         addAction("PAP statistics",
111             () -> Registry.register(PapConstants.REG_STATISTICS_MANAGER, new PapStatisticsManager()),
112             () -> Registry.unregister(PapConstants.REG_STATISTICS_MANAGER));
113
114         addAction("PDP modification lock",
115             () -> Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, pdpUpdateLock),
116             () -> Registry.unregister(PapConstants.REG_PDP_MODIFY_LOCK));
117
118         addAction("REST server",
119             () -> restServer = new PapRestServer(papParameterGroup.getRestServerParameters()),
120             () -> { });
121
122         addAction("REST server thread",
123             () -> restServer.start(),
124             () -> restServer.stop());
125         // @formatter:on
126     }
127
128     /**
129      * Get the parameters used by the activator.
130      *
131      * @return the parameters of the activator
132      */
133     public PapParameterGroup getParameterGroup() {
134         return papParameterGroup;
135     }
136
137     /**
138      * Registers the dispatcher with the topic source(s).
139      */
140     private void registerDispatcher() {
141         for (TopicSource source : TopicEndpoint.manager
142                         .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) {
143             source.register(msgDispatcher);
144         }
145     }
146
147     /**
148      * Unregisters the dispatcher from the topic source(s).
149      */
150     private void unregisterDispatcher() {
151         for (TopicSource source : TopicEndpoint.manager
152                         .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) {
153             source.unregister(msgDispatcher);
154         }
155     }
156 }