Create basic structure of pap component
[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  * ================================================================================
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.pap.main.startstop;
22
23 import org.onap.policy.common.parameters.ParameterService;
24 import org.onap.policy.pap.main.PolicyPapException;
25 import org.onap.policy.pap.main.parameters.PapParameterGroup;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
28
29 /**
30  * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
31  * forwarding handlers.
32  *
33  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
34  */
35 public class PapActivator {
36     // The logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(PapActivator.class);
38
39     // The parameters of this policy pap activator
40     private final PapParameterGroup papParameterGroup;
41
42     private static boolean alive = false;
43
44     /**
45      * Instantiate the activator for policy pap as a complete service.
46      *
47      * @param papParameterGroup the parameters for the pap service
48      */
49     public PapActivator(final PapParameterGroup papParameterGroup) {
50         this.papParameterGroup = papParameterGroup;
51     }
52
53     /**
54      * Initialize pap as a complete service.
55      *
56      * @throws PolicyPapException on errors in initializing the service
57      */
58     public void initialize() throws PolicyPapException {
59         try {
60             LOGGER.debug("Policy pap starting as a service . . .");
61             registerToParameterService(papParameterGroup);
62             PapActivator.setAlive(true);
63             LOGGER.debug("Policy pap started as a service");
64         } catch (final Exception exp) {
65             LOGGER.error("Policy pap service startup failed", exp);
66             throw new PolicyPapException(exp.getMessage(), exp);
67         }
68     }
69
70     /**
71      * Terminate policy pap.
72      *
73      * @throws PolicyPapException on errors in terminating the service
74      */
75     public void terminate() throws PolicyPapException {
76         try {
77             deregisterToParameterService(papParameterGroup);
78             PapActivator.setAlive(false);
79
80         } catch (final Exception exp) {
81             LOGGER.error("Policy pap service termination failed", exp);
82             throw new PolicyPapException(exp.getMessage(), exp);
83         }
84     }
85
86     /**
87      * Get the parameters used by the activator.
88      *
89      * @return the parameters of the activator
90      */
91     public PapParameterGroup getParameterGroup() {
92         return papParameterGroup;
93     }
94
95     /**
96      * Method to register the parameters to Common Parameter Service.
97      *
98      * @param papParameterGroup the pap parameter group
99      */
100     public void registerToParameterService(final PapParameterGroup papParameterGroup) {
101         ParameterService.register(papParameterGroup);
102     }
103
104     /**
105      * Method to deregister the parameters from Common Parameter Service.
106      *
107      * @param papParameterGroup the pap parameter group
108      */
109     public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
110         ParameterService.deregister(papParameterGroup.getName());
111     }
112
113     /**
114      * Returns the alive status of pap service.
115      *
116      * @return the alive
117      */
118     public static boolean isAlive() {
119         return alive;
120     }
121
122     /**
123      * Change the alive status of pap service.
124      *
125      * @param status the status
126      */
127     public static void setAlive(final boolean status) {
128         alive = status;
129     }
130 }