blob: f1c2f2669da9fe7fce0ec1472fde9043a8eb703c [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.
50 */
Simon Huntf679c4e2016-04-01 17:02:24 -070051 public UiTopoSession(String username, UiWebSocket webSocket) {
Simon Hunted804d52016-03-30 09:51:40 -070052 this.username = username;
Simon Huntf679c4e2016-04-01 17:02:24 -070053 this.webSocket = webSocket;
Simon Hunted804d52016-03-30 09:51:40 -070054 this.sharedModel = UiSharedTopologyModel.instance();
55 }
56
57 /**
58 * Initializes the layout; registering with the shared model.
59 */
60 public void init() {
61 if (!registered) {
62 sharedModel.register(this);
63 registered = true;
64 } else {
65 log.warn("already registered");
66 }
67 }
68
69 /**
70 * Destroys the layout; unregistering from the shared model.
71 */
72 public void destroy() {
73 if (!registered) {
74 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -070075 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -070076 } else {
77 log.warn("already unregistered");
78 }
79 }
80
81 @Override
82 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -070083 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -070084 }
85}