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 | |
| 39 | private final String username; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 40 | private final UiWebSocket webSocket; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 41 | private final UiSharedTopologyModel sharedModel; |
| 42 | |
| 43 | private boolean registered = false; |
| 44 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 45 | private UiTopoLayoutService service; |
| 46 | private UiTopoLayout layout; |
| 47 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Creates a new topology layout. |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame^] | 50 | * @param username user name |
| 51 | * @param webSocket web socket |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 52 | */ |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 53 | public UiTopoSession(String username, UiWebSocket webSocket) { |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 54 | this.username = username; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 55 | this.webSocket = webSocket; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 56 | this.sharedModel = UiSharedTopologyModel.instance(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Initializes the layout; registering with the shared model. |
| 61 | */ |
| 62 | public void init() { |
| 63 | if (!registered) { |
| 64 | sharedModel.register(this); |
| 65 | registered = true; |
| 66 | } else { |
| 67 | log.warn("already registered"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Destroys the layout; unregistering from the shared model. |
| 73 | */ |
| 74 | public void destroy() { |
| 75 | if (!registered) { |
| 76 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 77 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 78 | } else { |
| 79 | log.warn("already unregistered"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 85 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 86 | } |
| 87 | } |