b972bdc41d68b795c5c9f12399de3c98360c62ce
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / devicemanager / impl / xml / WebSocketServiceClient.java
1 /**
2  *
3  */
4 package org.opendaylight.mwtn.devicemanager.impl.xml;
5
6 import java.util.concurrent.Future;
7
8 import org.opendaylight.mwtn.devicemanager.impl.listener.ODLEventListener;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventInputBuilder;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutput;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService;
12 import org.opendaylight.yangtools.yang.common.RpcResult;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * Wrapper for forwarding websocket notifications to the websocket service, that is running as container.
18  * @author herbert
19  */
20 public class WebSocketServiceClient {
21
22     private static final Logger LOG = LoggerFactory.getLogger(ODLEventListener.class);
23
24     private final WebsocketmanagerService websocketmanagerService;
25     private final XmlMapper xmlMapper;
26
27     /**
28      * @param websocketmanagerService object
29      * @param xmlMapper object
30      */
31     public WebSocketServiceClient(WebsocketmanagerService websocketmanagerService,
32             XmlMapper xmlMapper) {
33         super();
34         this.websocketmanagerService = websocketmanagerService;
35         this.xmlMapper = xmlMapper;
36     }
37
38     public <T extends MwtNotificationBase & GetEventType> void sendViaWebsockets(String nodeName, T notificationXml) {
39         LOG.info("Send websocket event {} for mountpoint {}", notificationXml.getClass().getSimpleName(), nodeName);
40
41         try {
42             WebsocketEventInputBuilder wsBuilder = new WebsocketEventInputBuilder();
43             wsBuilder.setNodeName(nodeName);
44             wsBuilder.setEventType(notificationXml.getEventType());
45             wsBuilder.setXmlEvent(xmlMapper.getXmlString(notificationXml));
46             Future<RpcResult<WebsocketEventOutput>> result = websocketmanagerService.websocketEvent(wsBuilder.build());
47             LOG.info("Send websocket result: {}", result.get().getResult().getResponse());
48         } catch (Exception e) {
49             LOG.warn("Can not send websocket event {} for mountpoint {} {}",
50                     notificationXml.getClass().getSimpleName(), nodeName, e.toString());
51         }
52     }
53 }