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