Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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; |
| 21 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
| 22 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | /** |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 27 | * Coordinates with the {@link UiTopoLayoutService} to access |
| 28 | * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which |
| 29 | * maintains a local model of the network entities, |
| 30 | * tailored specifically for displaying on the UI. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 31 | * <p> |
| 32 | * Note that an instance of this class will be created for each |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 33 | * {@link UiWebSocket} connection, and will contain |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 34 | * the state of how the topology is laid out for the logged-in user. |
| 35 | */ |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 36 | public class UiTopoSession { |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 39 | private final UiWebSocket webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 40 | private final String username; |
| 41 | |
| 42 | final UiSharedTopologyModel sharedModel; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 43 | |
| 44 | private boolean registered = false; |
| 45 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 46 | private UiTopoLayoutService service; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 47 | private UiTopoLayout currentLayout; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 48 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 49 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 50 | * Creates a new topology session for the specified web socket connection. |
| 51 | * |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 52 | * @param webSocket web socket |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 53 | */ |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 54 | public UiTopoSession(UiWebSocket webSocket) { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 55 | this.webSocket = webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 56 | this.username = webSocket.userName(); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 57 | this.sharedModel = UiSharedTopologyModel.instance(); |
| 58 | } |
| 59 | |
| 60 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 61 | * Initializes the session; registering with the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 62 | */ |
| 63 | public void init() { |
| 64 | if (!registered) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 65 | log.debug("{} : Registering with shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 66 | sharedModel.register(this); |
| 67 | registered = true; |
| 68 | } else { |
| 69 | log.warn("already registered"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 74 | * Destroys the session; unregistering from the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 75 | */ |
| 76 | public void destroy() { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 77 | if (registered) { |
| 78 | log.debug("{} : Unregistering from shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 79 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 80 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 81 | } else { |
| 82 | log.warn("already unregistered"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 88 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 89 | } |
| 90 | } |