Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | f389723 | 2018-04-25 12:50:32 -0400 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 43455b8 | 2018-04-23 10:52:48 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Thomas Vachuska | d0d7ea1 | 2018-10-10 10:27:04 -0700 | [diff] [blame^] | 21 | import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest; |
| 22 | import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse; |
| 23 | import org.eclipse.jetty.websocket.servlet.WebSocketCreator; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 24 | import org.eclipse.jetty.websocket.servlet.WebSocketServlet; |
| 25 | import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 26 | import org.onlab.osgi.DefaultServiceDirectory; |
| 27 | import org.onlab.osgi.ServiceDirectory; |
| 28 | |
| 29 | import javax.servlet.ServletException; |
Simon Hunt | ea32708 | 2016-04-11 16:49:07 -0700 | [diff] [blame] | 30 | import java.security.Principal; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | import java.util.Timer; |
| 33 | import java.util.TimerTask; |
| 34 | |
| 35 | /** |
| 36 | * Web socket servlet capable of creating web sockets for the user interface. |
| 37 | */ |
| 38 | public class UiWebSocketServlet extends WebSocketServlet { |
| 39 | |
| 40 | private static final long PING_DELAY_MS = 5000; |
Thomas Vachuska | d0d7ea1 | 2018-10-10 10:27:04 -0700 | [diff] [blame^] | 41 | private static final long IDLE_TIMEOUT_MS = 10000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 42 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 43 | private static UiWebSocketServlet instance; |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 44 | private static final Object INSTANCE_LOCK = new Object(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 45 | |
Ray Milkey | 2014cdb | 2018-05-16 09:12:24 -0700 | [diff] [blame] | 46 | private static ServiceDirectory directory = new DefaultServiceDirectory(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 47 | |
Charles Chan | 43455b8 | 2018-04-23 10:52:48 -0700 | [diff] [blame] | 48 | private final Set<UiWebSocket> sockets = Sets.newConcurrentHashSet(); |
Thomas Vachuska | d0d7ea1 | 2018-10-10 10:27:04 -0700 | [diff] [blame^] | 49 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 50 | private final Timer timer = new Timer(); |
| 51 | private final TimerTask pruner = new Pruner(); |
Ray Milkey | 2014cdb | 2018-05-16 09:12:24 -0700 | [diff] [blame] | 52 | private static boolean isStopped = false; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 53 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | @Override |
| 55 | public void configure(WebSocketServletFactory webSocketServletFactory) { |
Thomas Vachuska | d0d7ea1 | 2018-10-10 10:27:04 -0700 | [diff] [blame^] | 56 | webSocketServletFactory.getPolicy().setIdleTimeout(IDLE_TIMEOUT_MS); |
| 57 | webSocketServletFactory.setCreator(new UiWebSocketCreator()); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Closes all currently open UI web-sockets. |
| 62 | */ |
| 63 | public static void closeAll() { |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 64 | synchronized (INSTANCE_LOCK) { |
| 65 | if (instance != null) { |
Ray Milkey | 2014cdb | 2018-05-16 09:12:24 -0700 | [diff] [blame] | 66 | isStopped = true; |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 67 | instance.sockets.forEach(UiWebSocket::close); |
| 68 | instance.sockets.clear(); |
| 69 | instance.pruner.cancel(); |
| 70 | instance.timer.cancel(); |
| 71 | } |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 75 | @Override |
| 76 | public void init() throws ServletException { |
| 77 | super.init(); |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 78 | synchronized (INSTANCE_LOCK) { |
| 79 | instance = this; |
| 80 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 81 | timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS); |
| 82 | } |
| 83 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Sends the specified message to all the GUI clients. |
| 86 | * |
| 87 | * @param type message type |
| 88 | * @param payload message payload |
| 89 | */ |
| 90 | static void sendToAll(String type, ObjectNode payload) { |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 91 | synchronized (INSTANCE_LOCK) { |
| 92 | if (instance != null) { |
| 93 | instance.sockets.forEach(ws -> ws.sendMessage(type, payload)); |
| 94 | } |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Sends the specified message to all the GUI clients of the specified user. |
| 100 | * |
| 101 | * @param userName user name |
| 102 | * @param type message type |
| 103 | * @param payload message payload |
| 104 | */ |
| 105 | static void sendToUser(String userName, String type, ObjectNode payload) { |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 106 | synchronized (INSTANCE_LOCK) { |
| 107 | if (instance != null) { |
| 108 | instance.sockets.stream().filter(ws -> userName.equals(ws.userName())) |
| 109 | .forEach(ws -> ws.sendMessage(type, payload)); |
| 110 | } |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 114 | // Task for pruning web-sockets that are idle. |
| 115 | private class Pruner extends TimerTask { |
| 116 | @Override |
| 117 | public void run() { |
Thomas Vachuska | f389723 | 2018-04-25 12:50:32 -0400 | [diff] [blame] | 118 | ImmutableSet<UiWebSocket> set = ImmutableSet.copyOf(sockets); |
| 119 | set.stream().filter(UiWebSocket::isIdle).forEach(s -> { |
| 120 | sockets.remove(s); |
| 121 | s.close(); |
| 122 | }); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
Thomas Vachuska | d0d7ea1 | 2018-10-10 10:27:04 -0700 | [diff] [blame^] | 125 | |
| 126 | // FIXME: This should not be necessary |
| 127 | private static final String FAKE_USERNAME = "UI-user"; |
| 128 | |
| 129 | public class UiWebSocketCreator implements WebSocketCreator { |
| 130 | @Override |
| 131 | public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { |
| 132 | if (!isStopped) { |
| 133 | // FIXME: Replace this with globally shared opaque token to allow secure failover |
| 134 | Principal p = request.getUserPrincipal(); |
| 135 | String userName = p != null ? p.getName() : FAKE_USERNAME; |
| 136 | |
| 137 | UiWebSocket socket = new UiWebSocket(directory, userName); |
| 138 | sockets.add(socket); |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 143 | } |