ca76a53a6ffdb80a98460f345f4577f8d814c9a0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 Nordix Foundation.
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.apex.service.engine.main;
23
24 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
25 import org.onap.policy.apex.service.engine.event.ApexEvent;
26 import org.onap.policy.apex.service.engine.runtime.EngineService;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * The Class ApexEngineServiceHandler holds the reference to the Apex engine
32  * service and the EngDep service for that engine. It also acts as an event
33  * receiver for asynchronous and synchronous events.
34  */
35 public class ApexEngineServiceHandler {
36     // The logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEngineServiceHandler.class);
38
39     // The Apex engine service, the Apex engine itself
40     private final EngineService apexEngineService;
41
42     /**
43      * Instantiates a new engine holder with its engine service and EngDep service.
44      *
45      * @param apexEngineService the apex engine service
46      */
47     ApexEngineServiceHandler(final EngineService apexEngineService) {
48         this.apexEngineService = apexEngineService;
49     }
50
51     /**
52      * This method forwards an event to the Apex service.
53      *
54      * @param apexEvent The event to forward to Apex
55      */
56     public void forwardEvent(final ApexEvent apexEvent) {
57         try {
58             // Send the event to the engine runtime
59             apexEngineService.getEngineServiceEventInterface().sendEvent(apexEvent);
60         } catch (final Exception e) {
61             final String errorMessage = "error transferring event \"" + apexEvent.getName() + "\" to the Apex engine";
62             LOGGER.debug(errorMessage, e);
63             throw new ApexActivatorRuntimeException(errorMessage, e);
64         }
65     }
66
67     /**
68      * Terminate the Apex engine.
69      *
70      * @throws ApexException on termination errors
71      */
72     public void terminate() throws ApexException {
73         // Shut down each engine instance
74         if (apexEngineService != null) {
75             apexEngineService.stop();
76             apexEngineService.clear();
77         }
78     }
79 }