0a74856140fb322aa5f917127f9c38b552a7fbe9
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / websocketmanager / impl / src / main / java / org / opendaylight / mwtn / impl / WebsocketImpl.java
1 /*
2 * Copyright (c) 2016 Wipro Ltd. and others. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 */
8
9 package org.opendaylight.mwtn.impl;
10
11 import java.util.concurrent.Future;
12
13 import org.json.JSONObject;
14 import org.opendaylight.mwtn.impl.websocket.WebSocketServerHandler;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class WebsocketImpl implements WebsocketmanagerService {
25
26         public interface EventInputCallback
27         {
28                 void onMessagePushed(final String message) throws Exception;
29         }
30     private static final Logger LOG = LoggerFactory.getLogger(WebsocketImpl.class);
31
32     private final EventInputCallback cb;
33
34     public WebsocketImpl()
35     {
36         this(null);
37     }
38     public WebsocketImpl(EventInputCallback callback)
39     {
40         this.cb=callback;
41     }
42         @Override
43     public Future<RpcResult<WebsocketEventOutput>> websocketEvent(WebsocketEventInput input) {
44         LOG.debug("Send message '{}'", input);
45         try {
46             WebsocketEventOutputBuilder outputBuilder = new WebsocketEventOutputBuilder();
47             final String s=input.getXmlEvent();
48             WebSocketServerHandler.sendMessage(input.getNodeName(), input.getEventType(),s );
49             outputBuilder.setResponse("OK");
50             if(this.cb!=null)
51             try {
52                 JSONObject o=new JSONObject();
53                 o.put(WebSocketServerHandler.KEY_NODENAME, input.getNodeName());
54                 o.put(WebSocketServerHandler.KEY_EVENTTYPE, input.getEventType());
55                 o.put(WebSocketServerHandler.KEY_XMLEVENT, input.getXmlEvent());
56                 this.cb.onMessagePushed(o.toString());
57             }
58             catch(Exception err)
59             {
60                 LOG.warn("problem pushing messsage to other nodes: "+err.getMessage());
61             }
62             return RpcResultBuilder.success(outputBuilder.build()).buildFuture();
63         } catch (Exception e) {
64             LOG.warn("Socketproblem: {}", e);
65         }
66         return null;
67     }
68
69
70         /*
71         public void websocketSend(String message) {
72                  LOG.debug("Send message '{}'", message);
73                 try {
74                     WebsocketEventOutputBuilder outputBuilder = new WebsocketEventOutputBuilder();
75                     WebSocketServerHandler.sendMessage(message);
76                     outputBuilder.setResponse("OK");
77
78                 } catch (Exception e) {
79                     LOG.warn("Socketproblem: {}", e);
80                 }
81         }
82         */
83 }