blob: 12e48f4529636a33b9746741cc23b1841b8f2b5e [file] [log] [blame]
Simon Hunted804d52016-03-30 09:51:40 -07001/*
2 * Copyright 2016 Open Networking Laboratory
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 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
39 private final String username;
Simon Huntf679c4e2016-04-01 17:02:24 -070040 private final UiWebSocket webSocket;
Simon Hunted804d52016-03-30 09:51:40 -070041 private final UiSharedTopologyModel sharedModel;
42
43 private boolean registered = false;
44
Simon Huntf679c4e2016-04-01 17:02:24 -070045 private UiTopoLayoutService service;
46 private UiTopoLayout layout;
47
Simon Hunted804d52016-03-30 09:51:40 -070048 /**
49 * Creates a new topology layout.
Ray Milkeyd4334db2016-04-05 17:39:44 -070050 * @param username user name
51 * @param webSocket web socket
Simon Hunted804d52016-03-30 09:51:40 -070052 */
Simon Huntf679c4e2016-04-01 17:02:24 -070053 public UiTopoSession(String username, UiWebSocket webSocket) {
Simon Hunted804d52016-03-30 09:51:40 -070054 this.username = username;
Simon Huntf679c4e2016-04-01 17:02:24 -070055 this.webSocket = webSocket;
Simon Hunted804d52016-03-30 09:51:40 -070056 this.sharedModel = UiSharedTopologyModel.instance();
57 }
58
59 /**
60 * Initializes the layout; registering with the shared model.
61 */
62 public void init() {
63 if (!registered) {
64 sharedModel.register(this);
65 registered = true;
66 } else {
67 log.warn("already registered");
68 }
69 }
70
71 /**
72 * Destroys the layout; unregistering from the shared model.
73 */
74 public void destroy() {
75 if (!registered) {
76 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -070077 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -070078 } else {
79 log.warn("already unregistered");
80 }
81 }
82
83 @Override
84 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -070085 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -070086 }
87}