blob: fbc7fa42e4e447e30788af0aeba4744e4f18d751 [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;
21import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
22import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunted804d52016-03-30 09:51:40 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26/**
Simon Huntf679c4e2016-04-01 17:02:24 -070027 * Coordinates with the {@link UiTopoLayoutService} to access
28 * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which
29 * maintains a local model of the network entities,
30 * tailored specifically for displaying on the UI.
Simon Hunted804d52016-03-30 09:51:40 -070031 * <p>
32 * Note that an instance of this class will be created for each
Simon Huntf679c4e2016-04-01 17:02:24 -070033 * {@link UiWebSocket} connection, and will contain
Simon Hunted804d52016-03-30 09:51:40 -070034 * the state of how the topology is laid out for the logged-in user.
35 */
Simon Huntf679c4e2016-04-01 17:02:24 -070036public class UiTopoSession {
Simon Hunted804d52016-03-30 09:51:40 -070037 private final Logger log = LoggerFactory.getLogger(getClass());
38
Simon Huntf679c4e2016-04-01 17:02:24 -070039 private final UiWebSocket webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070040 private final String username;
41
42 final UiSharedTopologyModel sharedModel;
Simon Hunted804d52016-03-30 09:51:40 -070043
44 private boolean registered = false;
45
Simon Huntf679c4e2016-04-01 17:02:24 -070046 private UiTopoLayoutService service;
Simon Hunt7092cc42016-04-06 18:40:17 -070047 private UiTopoLayout currentLayout;
Simon Huntf679c4e2016-04-01 17:02:24 -070048
Simon Hunted804d52016-03-30 09:51:40 -070049 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070050 * Creates a new topology session for the specified web socket connection.
51 *
Ray Milkeyd4334db2016-04-05 17:39:44 -070052 * @param webSocket web socket
Simon Hunted804d52016-03-30 09:51:40 -070053 */
Simon Hunt7092cc42016-04-06 18:40:17 -070054 public UiTopoSession(UiWebSocket webSocket) {
Simon Huntf679c4e2016-04-01 17:02:24 -070055 this.webSocket = webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070056 this.username = webSocket.userName();
Simon Hunted804d52016-03-30 09:51:40 -070057 this.sharedModel = UiSharedTopologyModel.instance();
58 }
59
60 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070061 * Initializes the session; registering with the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070062 */
63 public void init() {
64 if (!registered) {
Simon Hunt7092cc42016-04-06 18:40:17 -070065 log.debug("{} : Registering with shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070066 sharedModel.register(this);
67 registered = true;
68 } else {
69 log.warn("already registered");
70 }
71 }
72
73 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070074 * Destroys the session; unregistering from the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070075 */
76 public void destroy() {
Simon Hunt7092cc42016-04-06 18:40:17 -070077 if (registered) {
78 log.debug("{} : Unregistering from shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070079 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -070080 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -070081 } else {
82 log.warn("already unregistered");
83 }
84 }
85
86 @Override
87 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -070088 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -070089 }
90}