add rest serve and distribution framework
[multicloud/framework.git] / artifactbroker / main / src / main / java / org / onap / policy / distribution / main / startstop / DistributionActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.main.startstop;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.onap.policy.distribution.main.PolicyDistributionException;
31 import org.onap.policy.distribution.main.parameters.DistributionParameterGroup;
32 import org.onap.policy.distribution.main.rest.DistributionRestServer;
33
34 /**
35  * This class wraps a distributor so that it can be activated as a complete service together with all its distribution
36  * and forwarding handlers.
37  */
38 public class DistributionActivator {
39     // The logger for this class
40     private static final Logger LOGGER = FlexLogger.getLogger(DistributionActivator.class);
41
42     // The parameters of this policy distribution activator
43     private final DistributionParameterGroup distributionParameterGroup;
44
45     // The map of reception handlers initialized by this distribution activator
46
47     private static boolean alive = false;
48
49     private DistributionRestServer restServer;
50
51     /**
52      * Instantiate the activator for policy distribution as a complete service.
53      *
54      * @param distributionParameterGroup the parameters for the distribution service
55      */
56     public DistributionActivator(final DistributionParameterGroup distributionParameterGroup) {
57         this.distributionParameterGroup = distributionParameterGroup;
58     }
59
60     /**
61      * Initialize distribution as a complete service.
62      *
63      * @throws PolicyDistributionException on errors in initializing the service
64      */
65     @SuppressWarnings("unchecked")
66     public void initialize() throws PolicyDistributionException {
67         LOGGER.debug("Policy distribution starting as a service . . .");
68         startDistributionRestServer();
69         registerToParameterService(distributionParameterGroup);
70         DistributionActivator.setAlive(true);
71         LOGGER.debug("Policy distribution started as a service");
72     }
73
74     /**
75      * Starts the distribution rest server using configuration parameters.
76      *
77      * @throws PolicyDistributionException if server start fails
78      */
79     private void startDistributionRestServer() throws PolicyDistributionException {
80         distributionParameterGroup.getRestServerParameters().setName(distributionParameterGroup.getName());
81         restServer = new DistributionRestServer(distributionParameterGroup.getRestServerParameters());
82         if (!restServer.start()) {
83             throw new PolicyDistributionException(
84                     "Failed to start distribution rest server. Check log for more details...");
85         }
86     }
87
88     /**
89      * Terminate policy distribution.
90      *
91      * @throws PolicyDistributionException on termination errors
92      */
93     public void terminate() throws PolicyDistributionException {
94         try {
95             deregisterToParameterService(distributionParameterGroup);
96             DistributionActivator.setAlive(false);
97
98             // Stop the distribution rest server
99             restServer.stop();
100         } catch (final Exception exp) {
101             LOGGER.error("Policy distribution service termination failed", exp);
102             throw new PolicyDistributionException(exp.getMessage(), exp);
103         }
104     }
105
106     /**
107      * Get the parameters used by the activator.
108      *
109      * @return the parameters of the activator
110      */
111     public DistributionParameterGroup getParameterGroup() {
112         return distributionParameterGroup;
113     }
114
115     /**
116      * Method to register the parameters to Common Parameter Service.
117      *
118      * @param distributionParameterGroup the distribution parameter group
119      */
120     public void registerToParameterService(final DistributionParameterGroup distributionParameterGroup) {
121         ParameterService.register(distributionParameterGroup);
122         //@formatter:off
123         //@formatter:on
124     }
125
126     /**
127      * Method to deregister the parameters from Common Parameter Service.
128      *
129      * @param distributionParameterGroup the distribution parameter group
130      */
131     public void deregisterToParameterService(final DistributionParameterGroup distributionParameterGroup) {
132         ParameterService.deregister(distributionParameterGroup.getName());
133         //@formatter:on
134     }
135
136     /**
137      * Returns the alive status of distribution service.
138      *
139      * @return the alive
140      */
141     public static boolean isAlive() {
142         return alive;
143     }
144
145     /**
146      * Change the alive status of distribution service.
147      *
148      * @param status the status
149      */
150     public static void setAlive(final boolean status) {
151         alive = status;
152     }
153 }