7ebcad830bdd38e0dda6bd6cfaf65162f8b0df68
[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.engdep;
22
23 import java.net.InetSocketAddress;
24
25 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
27 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
28 import org.onap.policy.apex.core.protocols.Message;
29 import org.onap.policy.apex.service.engine.runtime.EngineService;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * The Class EngDepMessagingService is used to encapsulate the server side of EngDep communication.
35  * This class allows users to create and start an EngDep server.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class EngDepMessagingService {
40     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngDepMessagingService.class);
41
42     // Messaging service is used to transmit and receive messages over a communication protocol
43     private static MessagingServiceFactory<Message> messageServiceFactory = new MessagingServiceFactory<>();
44     private final MessagingService<Message> messageService;
45
46     // The listener that is listening for messages coming in on the EngDep protocol from clients
47     private final EngDepMessageListener messageListener;
48
49     /**
50      * Instantiates a new EngDep messaging service. It creates the message service instance, a
51      * listener for incoming messages, and starts the message listener thread for handling incoming
52      * messages.
53      *
54      * @param service the Apex engine service that this EngDep service is running for
55      * @param port the port The port to use for EngDep communication
56      */
57     public EngDepMessagingService(final EngineService service, final int port) {
58         LOGGER.entry(service);
59
60         // Create the service and listener and add the listener.
61         messageService = messageServiceFactory.createServer(new InetSocketAddress(MessagingUtils.checkPort(port)));
62         messageListener = new EngDepMessageListener(service);
63         messageService.addMessageListener(messageListener);
64
65         // Start incoming message processing on the listener
66         messageListener.startProcessorThread();
67         LOGGER.exit();
68     }
69
70     /**
71      * Start the server, open the communication mechanism for connections.
72      */
73     public void start() {
74         LOGGER.info("engine<-->deployment messaging starting . . .");
75         messageService.startConnection();
76         LOGGER.info("engine<-->deployment messaging started");
77     }
78
79     /**
80      * Start the server, close the communication mechanism.
81      */
82     public void stop() {
83         LOGGER.info("engine<-->deployment messaging stopping . . .");
84         messageService.stopConnection();
85         messageListener.stopProcessorThreads();
86         LOGGER.info("engine<-->deployment messaging stopped");
87     }
88
89     /**
90      * Is the server started?.
91      *
92      * @return true, if checks if is started
93      */
94     public boolean isStarted() {
95         return messageService.isStarted();
96     }
97
98     /**
99      * Is the server stopped?.
100      *
101      * @return true, if checks if is stopped
102      */
103     public boolean isStopped() {
104         return !messageService.isStarted();
105     }
106 }