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; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 24 | import org.onosproject.ui.model.topo.UiClusterMember; |
| 25 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 30 | import java.util.List; |
| 31 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 32 | /** |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 33 | * Coordinates with the {@link UiTopoLayoutService} to access |
| 34 | * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 35 | * maintains a local model of the network entities, tailored specifically |
| 36 | * for displaying on the UI. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 37 | * <p> |
| 38 | * Note that an instance of this class will be created for each |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 39 | * {@link UiWebSocket} connection, and will contain |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 40 | * the state of how the topology is laid out for the logged-in user. |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 41 | * <p> |
| 42 | * The expected pattern is for the {@link Topo2ViewMessageHandler} to obtain |
| 43 | * a reference to the session instance (via the {@link UiWebSocket}), and |
| 44 | * interact with it when topo-related events come in from the client. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 45 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 46 | public class UiTopoSession implements UiModelListener { |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 47 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 48 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 49 | private final UiWebSocket webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 50 | private final String username; |
| 51 | |
| 52 | final UiSharedTopologyModel sharedModel; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 53 | |
| 54 | private boolean registered = false; |
| 55 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 56 | private UiTopoLayoutService layoutService; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 57 | private UiTopoLayout currentLayout; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 58 | private boolean messagesEnabled; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 59 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 60 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 61 | * Creates a new topology session for the specified web socket connection. |
| 62 | * |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 63 | * @param webSocket web socket |
| 64 | * @param model share topology model |
| 65 | * @param layoutService topology layout service |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 66 | */ |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 67 | public UiTopoSession(UiWebSocket webSocket, |
| 68 | UiSharedTopologyModel model, |
| 69 | UiTopoLayoutService layoutService) { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 70 | this.webSocket = webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 71 | this.username = webSocket.userName(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 72 | this.sharedModel = model; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 73 | this.layoutService = layoutService; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 77 | * Initializes the session; registering with the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 78 | */ |
| 79 | public void init() { |
| 80 | if (!registered) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 81 | log.debug("{} : Registering with shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 82 | sharedModel.register(this); |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 83 | currentLayout = layoutService.getRootLayout(); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 84 | registered = true; |
| 85 | } else { |
| 86 | log.warn("already registered"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 91 | * Destroys the session; unregistering from the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 92 | */ |
| 93 | public void destroy() { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 94 | if (registered) { |
| 95 | log.debug("{} : Unregistering from shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 96 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 97 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 98 | } else { |
| 99 | log.warn("already unregistered"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 105 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 106 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public void event(UiModelEvent event) { |
Yuta HIGUCHI | 28ad09a | 2016-06-28 17:08:29 -0700 | [diff] [blame] | 110 | log.debug("Event received: {}", event); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 111 | // TODO: handle model events from the cache... |
| 112 | } |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Returns the current layout context. |
| 116 | * |
| 117 | * @return current topology layout |
| 118 | */ |
| 119 | public UiTopoLayout currentLayout() { |
| 120 | return currentLayout; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Changes the current layout context to the specified layout. |
| 125 | * |
| 126 | * @param topoLayout new topology layout context |
| 127 | */ |
| 128 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 129 | currentLayout = topoLayout; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Enables or disables the transmission of topology event update messages. |
| 134 | * |
| 135 | * @param enabled true if messages should be sent |
| 136 | */ |
| 137 | public void enableEvent(boolean enabled) { |
| 138 | messagesEnabled = enabled; |
| 139 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 140 | |
| 141 | /** |
| 142 | * Returns the list of ONOS instances (cluster members). |
| 143 | * |
| 144 | * @return the list of ONOS instances |
| 145 | */ |
| 146 | public List<UiClusterMember> getAllInstances() { |
| 147 | return sharedModel.getClusterMembers(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the region for the specified layout. |
| 152 | * |
| 153 | * @param layout layout filter |
| 154 | * @return region that the layout is based upon |
| 155 | */ |
| 156 | public UiRegion getRegion(UiTopoLayout layout) { |
| 157 | return sharedModel.getRegion(layout); |
| 158 | } |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 159 | } |