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