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 | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 19 | import org.onosproject.net.region.RegionId; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 20 | import org.onosproject.ui.UiTopoLayoutService; |
| 21 | import org.onosproject.ui.impl.UiWebSocket; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 22 | import org.onosproject.ui.impl.topo.model.UiModelEvent; |
| 23 | import org.onosproject.ui.impl.topo.model.UiModelListener; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.model.topo.UiClusterMember; |
| 26 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 31 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 32 | import java.util.List; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 33 | import java.util.Set; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 34 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 35 | /** |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 36 | * Coordinates with the {@link UiTopoLayoutService} to access |
| 37 | * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 38 | * maintains a local model of the network entities, tailored specifically |
| 39 | * for displaying on the UI. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 40 | * <p> |
| 41 | * Note that an instance of this class will be created for each |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 42 | * {@link UiWebSocket} connection, and will contain |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 43 | * 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] | 44 | * <p> |
| 45 | * The expected pattern is for the {@link Topo2ViewMessageHandler} to obtain |
| 46 | * a reference to the session instance (via the {@link UiWebSocket}), and |
| 47 | * interact with it when topo-related events come in from the client. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 48 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 49 | public class UiTopoSession implements UiModelListener { |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 50 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 51 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 52 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 53 | private final UiWebSocket webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 54 | private final String username; |
| 55 | |
| 56 | final UiSharedTopologyModel sharedModel; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 57 | |
| 58 | private boolean registered = false; |
| 59 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 60 | private UiTopoLayoutService layoutService; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 61 | private UiTopoLayout currentLayout; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 62 | private boolean messagesEnabled; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 63 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 64 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 65 | * Creates a new topology session for the specified web socket connection. |
| 66 | * |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 67 | * @param webSocket web socket |
| 68 | * @param model share topology model |
| 69 | * @param layoutService topology layout service |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 70 | */ |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 71 | public UiTopoSession(UiWebSocket webSocket, |
| 72 | UiSharedTopologyModel model, |
| 73 | UiTopoLayoutService layoutService) { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 74 | this.webSocket = webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 75 | this.username = webSocket.userName(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 76 | this.sharedModel = model; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 77 | this.layoutService = layoutService; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 80 | // constructs a neutered instance, for unit testing |
| 81 | UiTopoSession() { |
| 82 | webSocket = null; |
| 83 | username = null; |
| 84 | sharedModel = null; |
| 85 | } |
| 86 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 87 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 88 | * Initializes the session; registering with the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 89 | */ |
| 90 | public void init() { |
| 91 | if (!registered) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 92 | log.debug("{} : Registering with shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 93 | sharedModel.register(this); |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 94 | currentLayout = layoutService.getRootLayout(); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 95 | registered = true; |
| 96 | } else { |
| 97 | log.warn("already registered"); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 102 | * Destroys the session; unregistering from the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 103 | */ |
| 104 | public void destroy() { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 105 | if (registered) { |
| 106 | log.debug("{} : Unregistering from shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 107 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 108 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 109 | } else { |
| 110 | log.warn("already unregistered"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 116 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 117 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public void event(UiModelEvent event) { |
Yuta HIGUCHI | 28ad09a | 2016-06-28 17:08:29 -0700 | [diff] [blame] | 121 | log.debug("Event received: {}", event); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 122 | // TODO: handle model events from the cache... |
| 123 | } |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Returns the current layout context. |
| 127 | * |
| 128 | * @return current topology layout |
| 129 | */ |
| 130 | public UiTopoLayout currentLayout() { |
| 131 | return currentLayout; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Changes the current layout context to the specified layout. |
| 136 | * |
| 137 | * @param topoLayout new topology layout context |
| 138 | */ |
| 139 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 140 | currentLayout = topoLayout; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Enables or disables the transmission of topology event update messages. |
| 145 | * |
| 146 | * @param enabled true if messages should be sent |
| 147 | */ |
| 148 | public void enableEvent(boolean enabled) { |
| 149 | messagesEnabled = enabled; |
| 150 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Returns the list of ONOS instances (cluster members). |
| 154 | * |
| 155 | * @return the list of ONOS instances |
| 156 | */ |
| 157 | public List<UiClusterMember> getAllInstances() { |
| 158 | return sharedModel.getClusterMembers(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the region for the specified layout. |
| 163 | * |
| 164 | * @param layout layout filter |
| 165 | * @return region that the layout is based upon |
| 166 | */ |
| 167 | public UiRegion getRegion(UiTopoLayout layout) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 168 | RegionId rid = layout.regionId(); |
| 169 | return rid == null ? sharedModel.getNullRegion() : sharedModel.getRegion(rid); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 170 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Returns the regions that are "peers" to this region. That is, based on |
| 174 | * the layout the user is viewing, all the regions that are associated with |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 175 | * layouts that share the same parent layout as this layout. |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 176 | * |
| 177 | * @param layout the layout being viewed |
| 178 | * @return all regions that are "siblings" to this layout's region |
| 179 | */ |
| 180 | public Set<UiRegion> getPeerRegions(UiTopoLayout layout) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 181 | Set<UiTopoLayout> peerLayouts = layoutService.getPeers(layout.id()); |
| 182 | Set<UiRegion> peers = new HashSet<>(); |
| 183 | peerLayouts.forEach(l -> peers.add(sharedModel.getRegion(l.regionId()))); |
| 184 | return peers; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the subregions of the region in the specified layout. |
| 189 | * |
| 190 | * @param layout the layout being viewed |
| 191 | * @return all regions that are "contained within" this layout's region |
| 192 | */ |
| 193 | public Set<UiRegion> getSubRegions(UiTopoLayout layout) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 194 | Set<UiTopoLayout> kidLayouts = layoutService.getChildren(layout.id()); |
| 195 | Set<UiRegion> kids = new HashSet<>(); |
| 196 | kidLayouts.forEach(l -> kids.add(sharedModel.getRegion(l.regionId()))); |
| 197 | return kids; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 201 | * Refreshes the model's internal state. |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 202 | */ |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 203 | public void refreshModel() { |
| 204 | sharedModel.refresh(); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 205 | } |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 206 | } |