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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.main.startstop;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
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;
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.
38 public class DistributionActivator {
39 // The logger for this class
40 private static final Logger LOGGER = FlexLogger.getLogger(DistributionActivator.class);
42 // The parameters of this policy distribution activator
43 private final DistributionParameterGroup distributionParameterGroup;
45 // The map of reception handlers initialized by this distribution activator
47 private static boolean alive = false;
49 private DistributionRestServer restServer;
52 * Instantiate the activator for policy distribution as a complete service.
54 * @param distributionParameterGroup the parameters for the distribution service
56 public DistributionActivator(final DistributionParameterGroup distributionParameterGroup) {
57 this.distributionParameterGroup = distributionParameterGroup;
61 * Initialize distribution as a complete service.
63 * @throws PolicyDistributionException on errors in initializing the service
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");
75 * Starts the distribution rest server using configuration parameters.
77 * @throws PolicyDistributionException if server start fails
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...");
89 * Terminate policy distribution.
91 * @throws PolicyDistributionException on termination errors
93 public void terminate() throws PolicyDistributionException {
95 deregisterToParameterService(distributionParameterGroup);
96 DistributionActivator.setAlive(false);
98 // Stop the distribution rest server
100 } catch (final Exception exp) {
101 LOGGER.error("Policy distribution service termination failed", exp);
102 throw new PolicyDistributionException(exp.getMessage(), exp);
107 * Get the parameters used by the activator.
109 * @return the parameters of the activator
111 public DistributionParameterGroup getParameterGroup() {
112 return distributionParameterGroup;
116 * Method to register the parameters to Common Parameter Service.
118 * @param distributionParameterGroup the distribution parameter group
120 public void registerToParameterService(final DistributionParameterGroup distributionParameterGroup) {
121 ParameterService.register(distributionParameterGroup);
127 * Method to deregister the parameters from Common Parameter Service.
129 * @param distributionParameterGroup the distribution parameter group
131 public void deregisterToParameterService(final DistributionParameterGroup distributionParameterGroup) {
132 ParameterService.deregister(distributionParameterGroup.getName());
137 * Returns the alive status of distribution service.
141 public static boolean isAlive() {
146 * Change the alive status of distribution service.
148 * @param status the status
150 public static void setAlive(final boolean status) {