ad7af94497b5de2335c0f408e46030e3258d3324
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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
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.apex.service.engine.main;
22
23 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
24 import org.onap.policy.apex.service.engine.engdep.EngDepMessagingService;
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 service and the EngDep
32  * service for that engine. It also acts as an event receiver for asynchronous and synchronous
33  * 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     // The interface between the Apex engine and Apex policy deployment for the Apex engine
43     private final EngDepMessagingService engDepService;
44
45     /**
46      * Instantiates a new engine holder with its engine service and EngDep service.
47      *
48      * @param apexEngineService the apex engine service
49      * @param engDepService the EngDep service
50      */
51     ApexEngineServiceHandler(final EngineService apexEngineService, final EngDepMessagingService engDepService) {
52         this.apexEngineService = apexEngineService;
53         this.engDepService = engDepService;
54     }
55
56     /**
57      * This method forwards an event to the Apex service.
58      * 
59      * @param apexEvent The event to forward to Apex
60      */
61     public void forwardEvent(final ApexEvent apexEvent) {
62         try {
63             // Send the event to the engine runtime
64             apexEngineService.getEngineServiceEventInterface().sendEvent(apexEvent);
65         } catch (final Exception e) {
66             final String errorMessage = "error transferring event \"" + apexEvent.getName() + "\" to the Apex engine";
67             LOGGER.debug(errorMessage, e);
68             throw new ApexActivatorRuntimeException(errorMessage, e);
69         }
70     }
71
72     /**
73      * Terminate the Apex engine.
74      *
75      * @throws ApexException on termination errors
76      */
77     public void terminate() throws ApexException {
78         // Shut down engine management
79         if (engDepService != null) {
80             engDepService.stop();
81         }
82
83         // Shut down each engine instance
84         if (apexEngineService != null) {
85             apexEngineService.stop();
86         }
87     }
88 }