2 * Copyright (c) 2016 Wipro Ltd. and others. All rights reserved.
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
9 package org.opendaylight.mwtn.impl;
11 import java.util.concurrent.Future;
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;
24 public class WebsocketImpl implements WebsocketmanagerService {
26 public interface EventInputCallback
28 void onMessagePushed(final String message) throws Exception;
30 private static final Logger LOG = LoggerFactory.getLogger(WebsocketImpl.class);
32 private final EventInputCallback cb;
34 public WebsocketImpl()
38 public WebsocketImpl(EventInputCallback callback)
43 public Future<RpcResult<WebsocketEventOutput>> websocketEvent(WebsocketEventInput input) {
44 LOG.debug("Send message '{}'", input);
46 WebsocketEventOutputBuilder outputBuilder = new WebsocketEventOutputBuilder();
47 final String s=input.getXmlEvent();
48 WebSocketServerHandler.sendMessage(input.getNodeName(), input.getEventType(),s );
49 outputBuilder.setResponse("OK");
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());
60 LOG.warn("problem pushing messsage to other nodes: "+err.getMessage());
62 return RpcResultBuilder.success(outputBuilder.build()).buildFuture();
63 } catch (Exception e) {
64 LOG.warn("Socketproblem: {}", e);
71 public void websocketSend(String message) {
72 LOG.debug("Send message '{}'", message);
74 WebsocketEventOutputBuilder outputBuilder = new WebsocketEventOutputBuilder();
75 WebSocketServerHandler.sendMessage(message);
76 outputBuilder.setResponse("OK");
78 } catch (Exception e) {
79 LOG.warn("Socketproblem: {}", e);