blob: a693565395da618a744f33c2e30f7fd8da58d169 [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
Thomas Vachuska92b016b2016-05-20 11:37:57 -070048 private UiTopoLayoutService layoutService;
Simon Hunt7092cc42016-04-06 18:40:17 -070049 private UiTopoLayout currentLayout;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070050 private boolean messagesEnabled;
Simon Huntf679c4e2016-04-01 17:02:24 -070051
Simon Hunted804d52016-03-30 09:51:40 -070052 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070053 * Creates a new topology session for the specified web socket connection.
54 *
Thomas Vachuska92b016b2016-05-20 11:37:57 -070055 * @param webSocket web socket
56 * @param model share topology model
57 * @param layoutService topology layout service
Simon Hunted804d52016-03-30 09:51:40 -070058 */
Thomas Vachuska92b016b2016-05-20 11:37:57 -070059 public UiTopoSession(UiWebSocket webSocket,
60 UiSharedTopologyModel model,
61 UiTopoLayoutService layoutService) {
Simon Huntf679c4e2016-04-01 17:02:24 -070062 this.webSocket = webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070063 this.username = webSocket.userName();
Simon Huntcda9c032016-04-11 10:32:54 -070064 this.sharedModel = model;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070065 this.layoutService = layoutService;
Simon Hunted804d52016-03-30 09:51:40 -070066 }
67
68 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070069 * Initializes the session; registering with the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070070 */
71 public void init() {
72 if (!registered) {
Simon Hunt7092cc42016-04-06 18:40:17 -070073 log.debug("{} : Registering with shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070074 sharedModel.register(this);
Thomas Vachuska92b016b2016-05-20 11:37:57 -070075 currentLayout = layoutService.getRootLayout();
Simon Hunted804d52016-03-30 09:51:40 -070076 registered = true;
77 } else {
78 log.warn("already registered");
79 }
80 }
81
82 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070083 * Destroys the session; unregistering from the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070084 */
85 public void destroy() {
Simon Hunt7092cc42016-04-06 18:40:17 -070086 if (registered) {
87 log.debug("{} : Unregistering from shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070088 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -070089 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -070090 } else {
91 log.warn("already unregistered");
92 }
93 }
94
95 @Override
96 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -070097 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -070098 }
Simon Huntcda9c032016-04-11 10:32:54 -070099
100 @Override
101 public void event(UiModelEvent event) {
Yuta HIGUCHI28ad09a2016-06-28 17:08:29 -0700102 log.debug("Event received: {}", event);
Simon Huntcda9c032016-04-11 10:32:54 -0700103 // TODO: handle model events from the cache...
104 }
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700105
106 /**
107 * Returns the current layout context.
108 *
109 * @return current topology layout
110 */
111 public UiTopoLayout currentLayout() {
112 return currentLayout;
113 }
114
115 /**
116 * Changes the current layout context to the specified layout.
117 *
118 * @param topoLayout new topology layout context
119 */
120 public void setCurrentLayout(UiTopoLayout topoLayout) {
121 currentLayout = topoLayout;
122 }
123
124 /**
125 * Enables or disables the transmission of topology event update messages.
126 *
127 * @param enabled true if messages should be sent
128 */
129 public void enableEvent(boolean enabled) {
130 messagesEnabled = enabled;
131 }
Simon Hunted804d52016-03-30 09:51:40 -0700132}