f278fd59d1a3b1ee0dd43ed9f5f4cb10878ddad1
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 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      * @param engDepService     the EngDep service
47      */
48     ApexEngineServiceHandler(final EngineService apexEngineService) {
49         this.apexEngineService = apexEngineService;
50     }
51
52     /**
53      * This method forwards an event to the Apex service.
54      *
55      * @param apexEvent The event to forward to Apex
56      */
57     public void forwardEvent(final ApexEvent apexEvent) {
58         try {
59             // Send the event to the engine runtime
60             apexEngineService.getEngineServiceEventInterface().sendEvent(apexEvent);
61         } catch (final Exception e) {
62             final String errorMessage = "error transferring event \"" + apexEvent.getName() + "\" to the Apex engine";
63             LOGGER.debug(errorMessage, e);
64             throw new ApexActivatorRuntimeException(errorMessage, e);
65         }
66     }
67
68     /**
69      * Terminate the Apex engine.
70      *
71      * @throws ApexException on termination errors
72      */
73     public void terminate() throws ApexException {
74         // Shut down each engine instance
75         if (apexEngineService != null) {
76             apexEngineService.stop();
77             apexEngineService.clear();
78         }
79     }
80 }