blob: cfd2025482f58db2859277959040eeca44ff7d5b [file] [log] [blame]
Thomas Vachuska3553b302015-03-07 14:49:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska3553b302015-03-07 14:49:43 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui.impl;
17
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska3553b302015-03-07 14:49:43 -080019import org.eclipse.jetty.websocket.WebSocket;
20import org.eclipse.jetty.websocket.WebSocketServlet;
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
23
24import javax.servlet.ServletException;
25import javax.servlet.http.HttpServletRequest;
Simon Huntea327082016-04-11 16:49:07 -070026import java.security.Principal;
Thomas Vachuska3553b302015-03-07 14:49:43 -080027import java.util.HashSet;
28import java.util.Iterator;
29import java.util.Set;
30import java.util.Timer;
31import java.util.TimerTask;
32
33/**
34 * Web socket servlet capable of creating web sockets for the user interface.
35 */
36public class UiWebSocketServlet extends WebSocketServlet {
37
38 private static final long PING_DELAY_MS = 5000;
39
Thomas Vachuska51f540f2015-05-27 17:26:57 -070040 private static UiWebSocketServlet instance;
Ray Milkey06297ed2018-01-22 17:13:41 -080041 private static final Object INSTANCE_LOCK = new Object();
Thomas Vachuska51f540f2015-05-27 17:26:57 -070042
Thomas Vachuska3553b302015-03-07 14:49:43 -080043 private ServiceDirectory directory = new DefaultServiceDirectory();
44
45 private final Set<UiWebSocket> sockets = new HashSet<>();
46 private final Timer timer = new Timer();
47 private final TimerTask pruner = new Pruner();
Thomas Vachuska4e0e0932015-07-24 10:14:34 -070048 private boolean isStopped = false;
Thomas Vachuska3553b302015-03-07 14:49:43 -080049
Thomas Vachuska51f540f2015-05-27 17:26:57 -070050 /**
51 * Closes all currently open UI web-sockets.
52 */
53 public static void closeAll() {
Ray Milkey06297ed2018-01-22 17:13:41 -080054 synchronized (INSTANCE_LOCK) {
55 if (instance != null) {
56 instance.isStopped = true;
57 instance.sockets.forEach(UiWebSocket::close);
58 instance.sockets.clear();
59 instance.pruner.cancel();
60 instance.timer.cancel();
61 }
Thomas Vachuska51f540f2015-05-27 17:26:57 -070062 }
63 }
64
Thomas Vachuska3553b302015-03-07 14:49:43 -080065 @Override
66 public void init() throws ServletException {
67 super.init();
Ray Milkey06297ed2018-01-22 17:13:41 -080068 synchronized (INSTANCE_LOCK) {
69 instance = this;
70 }
Thomas Vachuska3553b302015-03-07 14:49:43 -080071 timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS);
72 }
73
74 @Override
75 public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
Thomas Vachuska4e0e0932015-07-24 10:14:34 -070076 if (isStopped) {
77 return null;
78 }
Thomas Vachuska0af26912016-03-21 21:37:30 -070079
80 // FIXME: Replace this with globally shared opaque token to allow secure failover
Simon Huntea327082016-04-11 16:49:07 -070081 Principal p = request.getUserPrincipal();
82 String userName = p != null ? p.getName() : FAKE_USERNAME;
83
Thomas Vachuska0af26912016-03-21 21:37:30 -070084 UiWebSocket socket = new UiWebSocket(directory, userName);
Thomas Vachuska3553b302015-03-07 14:49:43 -080085 synchronized (sockets) {
86 sockets.add(socket);
87 }
88 return socket;
89 }
90
Simon Huntea327082016-04-11 16:49:07 -070091 // FIXME: This should not be necessary
92 private static final String FAKE_USERNAME = "UI-user";
93
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070094 /**
95 * Sends the specified message to all the GUI clients.
96 *
97 * @param type message type
98 * @param payload message payload
99 */
100 static void sendToAll(String type, ObjectNode payload) {
Ray Milkey06297ed2018-01-22 17:13:41 -0800101 synchronized (INSTANCE_LOCK) {
102 if (instance != null) {
103 instance.sockets.forEach(ws -> ws.sendMessage(type, payload));
104 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700105 }
106 }
107
Thomas Vachuska0af26912016-03-21 21:37:30 -0700108 /**
109 * Sends the specified message to all the GUI clients of the specified user.
110 *
111 * @param userName user name
112 * @param type message type
113 * @param payload message payload
114 */
115 static void sendToUser(String userName, String type, ObjectNode payload) {
Ray Milkey06297ed2018-01-22 17:13:41 -0800116 synchronized (INSTANCE_LOCK) {
117 if (instance != null) {
118 instance.sockets.stream().filter(ws -> userName.equals(ws.userName()))
119 .forEach(ws -> ws.sendMessage(type, payload));
120 }
Thomas Vachuska0af26912016-03-21 21:37:30 -0700121 }
122 }
123
Thomas Vachuska3553b302015-03-07 14:49:43 -0800124 // Task for pruning web-sockets that are idle.
125 private class Pruner extends TimerTask {
126 @Override
127 public void run() {
128 synchronized (sockets) {
129 Iterator<UiWebSocket> it = sockets.iterator();
130 while (it.hasNext()) {
131 UiWebSocket socket = it.next();
132 if (socket.isIdle()) {
133 it.remove();
134 socket.close();
135 }
136 }
137 }
138 }
139 }
140}