blob: 1c9fc9ddfc2f74fbb8ace89120724d1362ab99e6 [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;
Simon Huntd5b96732016-07-08 13:22:27 -070024import org.onosproject.ui.model.topo.UiClusterMember;
25import org.onosproject.ui.model.topo.UiRegion;
Simon Huntf679c4e2016-04-01 17:02:24 -070026import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunted804d52016-03-30 09:51:40 -070027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Simon Huntd5b96732016-07-08 13:22:27 -070030import java.util.List;
31
Simon Hunted804d52016-03-30 09:51:40 -070032/**
Simon Huntf679c4e2016-04-01 17:02:24 -070033 * Coordinates with the {@link UiTopoLayoutService} to access
34 * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which
Simon Huntcda9c032016-04-11 10:32:54 -070035 * maintains a local model of the network entities, tailored specifically
36 * for displaying on the UI.
Simon Hunted804d52016-03-30 09:51:40 -070037 * <p>
38 * Note that an instance of this class will be created for each
Simon Huntf679c4e2016-04-01 17:02:24 -070039 * {@link UiWebSocket} connection, and will contain
Simon Hunted804d52016-03-30 09:51:40 -070040 * the state of how the topology is laid out for the logged-in user.
Simon Huntd5b96732016-07-08 13:22:27 -070041 * <p>
42 * The expected pattern is for the {@link Topo2ViewMessageHandler} to obtain
43 * a reference to the session instance (via the {@link UiWebSocket}), and
44 * interact with it when topo-related events come in from the client.
Simon Hunted804d52016-03-30 09:51:40 -070045 */
Simon Huntcda9c032016-04-11 10:32:54 -070046public class UiTopoSession implements UiModelListener {
Simon Hunted804d52016-03-30 09:51:40 -070047 private final Logger log = LoggerFactory.getLogger(getClass());
48
Simon Huntf679c4e2016-04-01 17:02:24 -070049 private final UiWebSocket webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070050 private final String username;
51
52 final UiSharedTopologyModel sharedModel;
Simon Hunted804d52016-03-30 09:51:40 -070053
54 private boolean registered = false;
55
Thomas Vachuska92b016b2016-05-20 11:37:57 -070056 private UiTopoLayoutService layoutService;
Simon Hunt7092cc42016-04-06 18:40:17 -070057 private UiTopoLayout currentLayout;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070058 private boolean messagesEnabled;
Simon Huntf679c4e2016-04-01 17:02:24 -070059
Simon Hunted804d52016-03-30 09:51:40 -070060 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070061 * Creates a new topology session for the specified web socket connection.
62 *
Thomas Vachuska92b016b2016-05-20 11:37:57 -070063 * @param webSocket web socket
64 * @param model share topology model
65 * @param layoutService topology layout service
Simon Hunted804d52016-03-30 09:51:40 -070066 */
Thomas Vachuska92b016b2016-05-20 11:37:57 -070067 public UiTopoSession(UiWebSocket webSocket,
68 UiSharedTopologyModel model,
69 UiTopoLayoutService layoutService) {
Simon Huntf679c4e2016-04-01 17:02:24 -070070 this.webSocket = webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070071 this.username = webSocket.userName();
Simon Huntcda9c032016-04-11 10:32:54 -070072 this.sharedModel = model;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070073 this.layoutService = layoutService;
Simon Hunted804d52016-03-30 09:51:40 -070074 }
75
76 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070077 * Initializes the session; registering with the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070078 */
79 public void init() {
80 if (!registered) {
Simon Hunt7092cc42016-04-06 18:40:17 -070081 log.debug("{} : Registering with shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070082 sharedModel.register(this);
Thomas Vachuska92b016b2016-05-20 11:37:57 -070083 currentLayout = layoutService.getRootLayout();
Simon Hunted804d52016-03-30 09:51:40 -070084 registered = true;
85 } else {
86 log.warn("already registered");
87 }
88 }
89
90 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070091 * Destroys the session; unregistering from the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070092 */
93 public void destroy() {
Simon Hunt7092cc42016-04-06 18:40:17 -070094 if (registered) {
95 log.debug("{} : Unregistering from shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070096 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -070097 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -070098 } else {
99 log.warn("already unregistered");
100 }
101 }
102
103 @Override
104 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -0700105 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -0700106 }
Simon Huntcda9c032016-04-11 10:32:54 -0700107
108 @Override
109 public void event(UiModelEvent event) {
Yuta HIGUCHI28ad09a2016-06-28 17:08:29 -0700110 log.debug("Event received: {}", event);
Simon Huntcda9c032016-04-11 10:32:54 -0700111 // TODO: handle model events from the cache...
112 }
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700113
114 /**
115 * Returns the current layout context.
116 *
117 * @return current topology layout
118 */
119 public UiTopoLayout currentLayout() {
120 return currentLayout;
121 }
122
123 /**
124 * Changes the current layout context to the specified layout.
125 *
126 * @param topoLayout new topology layout context
127 */
128 public void setCurrentLayout(UiTopoLayout topoLayout) {
129 currentLayout = topoLayout;
130 }
131
132 /**
133 * Enables or disables the transmission of topology event update messages.
134 *
135 * @param enabled true if messages should be sent
136 */
137 public void enableEvent(boolean enabled) {
138 messagesEnabled = enabled;
139 }
Simon Huntd5b96732016-07-08 13:22:27 -0700140
141 /**
142 * Returns the list of ONOS instances (cluster members).
143 *
144 * @return the list of ONOS instances
145 */
146 public List<UiClusterMember> getAllInstances() {
147 return sharedModel.getClusterMembers();
148 }
149
150 /**
151 * Returns the region for the specified layout.
152 *
153 * @param layout layout filter
154 * @return region that the layout is based upon
155 */
156 public UiRegion getRegion(UiTopoLayout layout) {
157 return sharedModel.getRegion(layout);
158 }
Simon Hunted804d52016-03-30 09:51:40 -0700159}