blob: 70698ec647e851b50284b140955195b8a9898ae8 [file] [log] [blame]
Simon Hunted804d52016-03-30 09:51:40 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunted804d52016-03-30 09:51:40 -07003 *
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 Huntf679c4e2016-04-01 17:02:24 -070017package org.onosproject.ui.impl.topo;
Simon Hunted804d52016-03-30 09:51:40 -070018
Simon Huntf679c4e2016-04-01 17:02:24 -070019import org.onosproject.ui.UiTopoLayoutService;
20import org.onosproject.ui.impl.UiWebSocket;
Simon Huntcda9c032016-04-11 10:32:54 -070021import org.onosproject.ui.impl.topo.model.UiModelEvent;
22import org.onosproject.ui.impl.topo.model.UiModelListener;
Simon Huntf679c4e2016-04-01 17:02:24 -070023import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
24import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunted804d52016-03-30 09:51:40 -070025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28/**
Simon Huntf679c4e2016-04-01 17:02:24 -070029 * Coordinates with the {@link UiTopoLayoutService} to access
30 * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which
Simon Huntcda9c032016-04-11 10:32:54 -070031 * maintains a local model of the network entities, tailored specifically
32 * for displaying on the UI.
Simon Hunted804d52016-03-30 09:51:40 -070033 * <p>
34 * Note that an instance of this class will be created for each
Simon Huntf679c4e2016-04-01 17:02:24 -070035 * {@link UiWebSocket} connection, and will contain
Simon Hunted804d52016-03-30 09:51:40 -070036 * the state of how the topology is laid out for the logged-in user.
37 */
Simon Huntcda9c032016-04-11 10:32:54 -070038public class UiTopoSession implements UiModelListener {
Simon Hunted804d52016-03-30 09:51:40 -070039 private final Logger log = LoggerFactory.getLogger(getClass());
40
Simon Huntf679c4e2016-04-01 17:02:24 -070041 private final UiWebSocket webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070042 private final String username;
43
44 final UiSharedTopologyModel sharedModel;
Simon Hunted804d52016-03-30 09:51:40 -070045
46 private boolean registered = false;
47
Simon Huntf679c4e2016-04-01 17:02:24 -070048 private UiTopoLayoutService service;
Simon Hunt7092cc42016-04-06 18:40:17 -070049 private UiTopoLayout currentLayout;
Simon Huntf679c4e2016-04-01 17:02:24 -070050
Simon Hunted804d52016-03-30 09:51:40 -070051 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070052 * Creates a new topology session for the specified web socket connection.
53 *
Ray Milkeyd4334db2016-04-05 17:39:44 -070054 * @param webSocket web socket
Simon Huntcda9c032016-04-11 10:32:54 -070055 * @param model share topology model
Simon Hunted804d52016-03-30 09:51:40 -070056 */
Simon Huntcda9c032016-04-11 10:32:54 -070057 public UiTopoSession(UiWebSocket webSocket, UiSharedTopologyModel model) {
Simon Huntf679c4e2016-04-01 17:02:24 -070058 this.webSocket = webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070059 this.username = webSocket.userName();
Simon Huntcda9c032016-04-11 10:32:54 -070060 this.sharedModel = model;
Simon Hunted804d52016-03-30 09:51:40 -070061 }
62
63 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070064 * Initializes the session; registering with the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070065 */
66 public void init() {
67 if (!registered) {
Simon Hunt7092cc42016-04-06 18:40:17 -070068 log.debug("{} : Registering with shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070069 sharedModel.register(this);
70 registered = true;
71 } else {
72 log.warn("already registered");
73 }
74 }
75
76 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070077 * Destroys the session; unregistering from the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070078 */
79 public void destroy() {
Simon Hunt7092cc42016-04-06 18:40:17 -070080 if (registered) {
81 log.debug("{} : Unregistering from shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070082 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -070083 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -070084 } else {
85 log.warn("already unregistered");
86 }
87 }
88
89 @Override
90 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -070091 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -070092 }
Simon Huntcda9c032016-04-11 10:32:54 -070093
94 @Override
95 public void event(UiModelEvent event) {
96 log.info("Event received: {}", event);
97 // TODO: handle model events from the cache...
98 }
Simon Hunted804d52016-03-30 09:51:40 -070099}