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 | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 20 | import org.onosproject.net.region.RegionId; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 21 | import org.onosproject.ui.UiTopoLayoutService; |
| 22 | import org.onosproject.ui.impl.UiWebSocket; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 23 | import org.onosproject.ui.impl.topo.model.UiModelEvent; |
| 24 | import org.onosproject.ui.impl.topo.model.UiModelListener; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.model.topo.UiNode; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 35 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 36 | import java.util.List; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 37 | import java.util.Set; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 38 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 39 | /** |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 40 | * Coordinates with the {@link UiTopoLayoutService} to access |
| 41 | * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 42 | * maintains a local model of the network entities, tailored specifically |
| 43 | * for displaying on the UI. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 44 | * <p> |
| 45 | * Note that an instance of this class will be created for each |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 46 | * {@link UiWebSocket} connection, and will contain |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 47 | * 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] | 48 | * <p> |
| 49 | * The expected pattern is for the {@link Topo2ViewMessageHandler} to obtain |
| 50 | * a reference to the session instance (via the {@link UiWebSocket}), and |
| 51 | * interact with it when topo-related events come in from the client. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 52 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 53 | public class UiTopoSession implements UiModelListener { |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 54 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 55 | private static final String TOPO2_UI_MODEL_EVENT = "topo2UiModelEvent"; |
| 56 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 59 | private final UiWebSocket webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 60 | private final String username; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 61 | private final Topo2Jsonifier t2json; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 62 | |
| 63 | final UiSharedTopologyModel sharedModel; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 64 | |
| 65 | private boolean registered = false; |
| 66 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 67 | private UiTopoLayoutService layoutService; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 68 | private UiTopoLayout currentLayout; |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 69 | private boolean messagesEnabled = true; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 70 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 71 | /** |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 72 | * Creates a new topology session for the specified web socket connection, |
| 73 | * and references to JSONifier, shared model, and layout service. |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 74 | * |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 75 | * @param webSocket web socket |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 76 | * @param jsonifier JSONifier instance |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 77 | * @param model share topology model |
| 78 | * @param layoutService topology layout service |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 79 | */ |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 80 | public UiTopoSession(UiWebSocket webSocket, |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 81 | Topo2Jsonifier jsonifier, |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 82 | UiSharedTopologyModel model, |
| 83 | UiTopoLayoutService layoutService) { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 84 | this.webSocket = webSocket; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 85 | this.username = webSocket.userName(); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 86 | this.t2json = jsonifier; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 87 | this.sharedModel = model; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 88 | this.layoutService = layoutService; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 91 | // constructs a neutered instance, for unit testing |
| 92 | UiTopoSession() { |
| 93 | webSocket = null; |
| 94 | username = null; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 95 | t2json = null; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 96 | sharedModel = null; |
| 97 | } |
| 98 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 99 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 100 | * Initializes the session; registering with the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 101 | */ |
| 102 | public void init() { |
| 103 | if (!registered) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 104 | log.debug("{} : Registering with shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 105 | sharedModel.register(this); |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 106 | currentLayout = layoutService.getRootLayout(); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 107 | registered = true; |
| 108 | } else { |
| 109 | log.warn("already registered"); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 114 | * Destroys the session; unregistering from the shared model. |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 115 | */ |
| 116 | public void destroy() { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 117 | if (registered) { |
| 118 | log.debug("{} : Unregistering from shared model", this); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 119 | sharedModel.unregister(this); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 120 | registered = false; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 121 | } else { |
| 122 | log.warn("already unregistered"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String toString() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 128 | return String.format("{UiTopoSession for user <%s>}", username); |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 129 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | public void event(UiModelEvent event) { |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 133 | String msg = messagesEnabled |
| 134 | ? "Event received: {}" |
| 135 | : "Event received: {}, but not transmitted"; |
| 136 | log.debug(msg, event); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 137 | |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 138 | if (messagesEnabled) { |
| 139 | ObjectNode payload = t2json.jsonEvent(event); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 140 | |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 141 | // TODO: add filtering for relevant objects only... |
| 142 | // TO Decide: Since the session holds the state of what is being |
| 143 | // displayed on the client, we should filter out any model events |
| 144 | // that are not relevant, and only send up events for objects that |
| 145 | // are currently being viewed by the user. |
| 146 | |
| 147 | webSocket.sendMessage(TOPO2_UI_MODEL_EVENT, payload); |
| 148 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 149 | } |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Returns the current layout context. |
| 153 | * |
| 154 | * @return current topology layout |
| 155 | */ |
| 156 | public UiTopoLayout currentLayout() { |
| 157 | return currentLayout; |
| 158 | } |
| 159 | |
| 160 | /** |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 161 | * Returns the breadcrumb trail from current layout to root. That is, |
| 162 | * element 0 of the list will be the current layout; the last element |
| 163 | * of the list will be the root layout. This list is guaranteed to have |
| 164 | * size of at least 1. |
| 165 | * |
| 166 | * @return breadcrumb trail |
| 167 | */ |
| 168 | public List<UiTopoLayout> breadCrumbs() { |
| 169 | UiTopoLayout current = currentLayout; |
| 170 | List<UiTopoLayout> crumbs = new ArrayList<>(); |
| 171 | crumbs.add(current); |
| 172 | while (!current.isRoot()) { |
| 173 | current = layoutService.getLayout(current.parent()); |
| 174 | crumbs.add(current); |
| 175 | } |
| 176 | return crumbs; |
| 177 | } |
| 178 | |
| 179 | /** |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 180 | * Changes the current layout context to the specified layout. |
| 181 | * |
| 182 | * @param topoLayout new topology layout context |
| 183 | */ |
| 184 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 185 | currentLayout = topoLayout; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Enables or disables the transmission of topology event update messages. |
| 190 | * |
| 191 | * @param enabled true if messages should be sent |
| 192 | */ |
| 193 | public void enableEvent(boolean enabled) { |
| 194 | messagesEnabled = enabled; |
| 195 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Returns the list of ONOS instances (cluster members). |
| 199 | * |
| 200 | * @return the list of ONOS instances |
| 201 | */ |
| 202 | public List<UiClusterMember> getAllInstances() { |
| 203 | return sharedModel.getClusterMembers(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the region for the specified layout. |
| 208 | * |
| 209 | * @param layout layout filter |
| 210 | * @return region that the layout is based upon |
| 211 | */ |
| 212 | public UiRegion getRegion(UiTopoLayout layout) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 213 | RegionId rid = layout.regionId(); |
| 214 | return rid == null ? sharedModel.getNullRegion() : sharedModel.getRegion(rid); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 215 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 216 | |
| 217 | /** |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 218 | * Returns the regions/devices that are "peers" to this region. That is, |
| 219 | * based on the layout the user is viewing, all the regions/devices that |
| 220 | * are associated with layouts that share the same parent layout as this |
| 221 | * layout, AND that are linked to an element within this region. |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 222 | * |
| 223 | * @param layout the layout being viewed |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 224 | * @return all regions/devices that are "siblings" to this layout's region |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 225 | */ |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 226 | public Set<UiNode> getPeerNodes(UiTopoLayout layout) { |
| 227 | Set<UiNode> peers = new HashSet<>(); |
| 228 | |
| 229 | // first, get the peer regions |
| 230 | Set<UiTopoLayout> peerLayouts = layoutService.getPeerLayouts(layout.id()); |
| 231 | peerLayouts.forEach(l -> { |
| 232 | RegionId peerRegion = l.regionId(); |
| 233 | peers.add(sharedModel.getRegion(peerRegion)); |
| 234 | }); |
| 235 | |
| 236 | // now add the devices that reside in the parent region |
| 237 | if (!layout.isRoot()) { |
| 238 | UiTopoLayout parentLayout = layoutService.getLayout(layout.parent()); |
| 239 | getRegion(parentLayout).devices().forEach(peers::add); |
| 240 | } |
| 241 | |
| 242 | // TODO: Finally, filter out regions / devices that are not connected |
| 243 | // directly to this region by an implicit link |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 244 | return peers; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns the subregions of the region in the specified layout. |
| 249 | * |
| 250 | * @param layout the layout being viewed |
| 251 | * @return all regions that are "contained within" this layout's region |
| 252 | */ |
| 253 | public Set<UiRegion> getSubRegions(UiTopoLayout layout) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 254 | Set<UiTopoLayout> kidLayouts = layoutService.getChildren(layout.id()); |
| 255 | Set<UiRegion> kids = new HashSet<>(); |
| 256 | kidLayouts.forEach(l -> kids.add(sharedModel.getRegion(l.regionId()))); |
| 257 | return kids; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 261 | * Returns the (synthetic) links of the region in the specified layout. |
| 262 | * |
| 263 | * @param layout the layout being viewed |
| 264 | * @return all links that are contained by this layout's region |
| 265 | */ |
| 266 | public List<UiSynthLink> getLinks(UiTopoLayout layout) { |
| 267 | return sharedModel.getSynthLinks(layout.regionId()); |
| 268 | } |
| 269 | |
| 270 | /** |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 271 | * Refreshes the model's internal state. |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 272 | */ |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 273 | public void refreshModel() { |
| 274 | sharedModel.refresh(); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 275 | } |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Navigates to the specified region by setting the associated layout as |
| 279 | * current. |
| 280 | * |
| 281 | * @param regionId region identifier |
| 282 | */ |
| 283 | public void navToRegion(String regionId) { |
| 284 | // 1. find the layout corresponding to the region ID |
| 285 | // 2. set this layout to be "current" |
| 286 | RegionId r = RegionId.regionId(regionId); |
| 287 | UiTopoLayout layout = layoutService.getLayout(r); |
| 288 | setCurrentLayout(layout); |
| 289 | } |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 290 | } |