2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
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.clamp.controlloop.participant.policy.main.handler;
23 import java.io.IOException;
24 import java.util.List;
26 import javax.ws.rs.core.Response;
28 import org.onap.policy.clamp.controlloop.common.handler.ControlLoopHandler;
29 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
30 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters;
31 import org.onap.policy.common.endpoints.event.comm.TopicSink;
32 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
33 import org.onap.policy.common.utils.services.Registry;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.provider.PolicyModelsProvider;
37 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
40 * This class handles policy participant and control loop elements.
42 * <p/>It is effectively a singleton that is started at system start.
44 public class PolicyHandler extends ControlLoopHandler {
46 private final ParticipantIntermediaryParameters participantParameters;
47 private final ParticipantPolicyParameters policyParameters;
50 private PolicyProvider policyProvider;
52 private PolicyModelsProvider databaseProvider;
57 * @param parameters the parameters for access to the database
58 * @throws PfModelException in case of an exception
60 public PolicyHandler(ParticipantPolicyParameters parameters) throws PfModelException {
61 super(parameters.getDatabaseProviderParameters());
62 participantParameters = parameters.getIntermediaryParameters();
63 policyParameters = parameters;
66 public static PolicyHandler getInstance() {
67 return Registry.get(PolicyHandler.class.getName());
71 public Set<Class<?>> getProviderClasses() {
76 public void startProviders() {
78 policyProvider = new PolicyProvider(participantParameters);
79 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(
80 policyParameters.getDatabaseProviderParameters());
81 } catch (PfModelException e) {
82 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Start providers failed ", e);
87 public void stopProviders() {
89 policyProvider.close();
90 } catch (IOException e) {
91 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Stop providers failed ", e);
95 databaseProvider.close();
96 } catch (PfModelException e) {
97 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Stop providers failed ", e);