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 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 48 | private UiTopoLayoutService service; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 49 | private UiTopoLayout currentLayout; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 50 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 51 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 52 | * Creates a new topology session for the specified web socket connection. |
| 53 | * |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 54 | * @param webSocket web socket |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame^] | 55 | * @param model share topology model |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 56 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame^] | 57 | public UiTopoSession(UiWebSocket webSocket, UiSharedTopologyModel model) { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 58 | this.webSocket = webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 59 | this.username = webSocket.userName(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame^] | 60 | this.sharedModel = model; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 64 | * Initializes the session; registering with the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 65 | */ |
| 66 | public void init() { |
| 67 | if (!registered) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 68 | log.debug("{} : Registering with shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 69 | sharedModel.register(this); |
| 70 | registered = true; |
| 71 | } else { |
| 72 | log.warn("already registered"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 77 | * Destroys the session; unregistering from the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 78 | */ |
| 79 | public void destroy() { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 80 | if (registered) { |
| 81 | log.debug("{} : Unregistering from shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 82 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 83 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 84 | } else { |
| 85 | log.warn("already unregistered"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 91 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 92 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame^] | 93 | |
| 94 | @Override |
| 95 | public void event(UiModelEvent event) { |
| 96 | log.info("Event received: {}", event); |
| 97 | // TODO: handle model events from the cache... |
| 98 | } |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 99 | } |