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