Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [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 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 17 | package org.onosproject.ui.impl.topo; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 18 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 19 | import org.onosproject.ui.UiTopoLayoutService; |
| 20 | import org.onosproject.ui.impl.UiWebSocket; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 21 | import org.onosproject.ui.impl.topo.model.UiModelEvent; |
| 22 | import org.onosproject.ui.impl.topo.model.UiModelListener; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 23 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
| 24 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | /** |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 29 | * Coordinates with the {@link UiTopoLayoutService} to access |
| 30 | * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 31 | * maintains a local model of the network entities, tailored specifically |
| 32 | * for displaying on the UI. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 33 | * <p> |
| 34 | * Note that an instance of this class will be created for each |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 35 | * {@link UiWebSocket} connection, and will contain |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 36 | * the state of how the topology is laid out for the logged-in user. |
| 37 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 38 | public class UiTopoSession implements UiModelListener { |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 41 | private final UiWebSocket webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 42 | private final String username; |
| 43 | |
| 44 | final UiSharedTopologyModel sharedModel; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 45 | |
| 46 | private boolean registered = false; |
| 47 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 48 | private UiTopoLayoutService layoutService; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 49 | private UiTopoLayout currentLayout; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 50 | private boolean messagesEnabled; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 51 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 52 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 53 | * Creates a new topology session for the specified web socket connection. |
| 54 | * |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 55 | * @param webSocket web socket |
| 56 | * @param model share topology model |
| 57 | * @param layoutService topology layout service |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 58 | */ |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 59 | public UiTopoSession(UiWebSocket webSocket, |
| 60 | UiSharedTopologyModel model, |
| 61 | UiTopoLayoutService layoutService) { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 62 | this.webSocket = webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 63 | this.username = webSocket.userName(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 64 | this.sharedModel = model; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 65 | this.layoutService = layoutService; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 69 | * Initializes the session; registering with the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 70 | */ |
| 71 | public void init() { |
| 72 | if (!registered) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 73 | log.debug("{} : Registering with shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 74 | sharedModel.register(this); |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 75 | currentLayout = layoutService.getRootLayout(); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 76 | registered = true; |
| 77 | } else { |
| 78 | log.warn("already registered"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 83 | * Destroys the session; unregistering from the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 84 | */ |
| 85 | public void destroy() { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 86 | if (registered) { |
| 87 | log.debug("{} : Unregistering from shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 88 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 89 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 90 | } else { |
| 91 | log.warn("already unregistered"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 97 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 98 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 99 | |
| 100 | @Override |
| 101 | public void event(UiModelEvent event) { |
Yuta HIGUCHI | 28ad09a | 2016-06-28 17:08:29 -0700 | [diff] [blame] | 102 | log.debug("Event received: {}", event); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 103 | // TODO: handle model events from the cache... |
| 104 | } |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Returns the current layout context. |
| 108 | * |
| 109 | * @return current topology layout |
| 110 | */ |
| 111 | public UiTopoLayout currentLayout() { |
| 112 | return currentLayout; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Changes the current layout context to the specified layout. |
| 117 | * |
| 118 | * @param topoLayout new topology layout context |
| 119 | */ |
| 120 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 121 | currentLayout = topoLayout; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Enables or disables the transmission of topology event update messages. |
| 126 | * |
| 127 | * @param enabled true if messages should be sent |
| 128 | */ |
| 129 | public void enableEvent(boolean enabled) { |
| 130 | messagesEnabled = enabled; |
| 131 | } |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 132 | } |