blob: 47907d497c9d7faa9f19742ff3b2d2331e7cb77a [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 Huntb1ce2602016-07-23 14:04:31 -070019import org.onosproject.net.region.RegionId;
Simon Huntf679c4e2016-04-01 17:02:24 -070020import org.onosproject.ui.UiTopoLayoutService;
21import org.onosproject.ui.impl.UiWebSocket;
Simon Huntcda9c032016-04-11 10:32:54 -070022import org.onosproject.ui.impl.topo.model.UiModelEvent;
23import org.onosproject.ui.impl.topo.model.UiModelListener;
Simon Huntf679c4e2016-04-01 17:02:24 -070024import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
Simon Huntd5b96732016-07-08 13:22:27 -070025import org.onosproject.ui.model.topo.UiClusterMember;
26import org.onosproject.ui.model.topo.UiRegion;
Simon Huntf679c4e2016-04-01 17:02:24 -070027import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunted804d52016-03-30 09:51:40 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Simon Huntb1ce2602016-07-23 14:04:31 -070031import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070032import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070033import java.util.Set;
Simon Huntd5b96732016-07-08 13:22:27 -070034
Simon Hunted804d52016-03-30 09:51:40 -070035/**
Simon Huntf679c4e2016-04-01 17:02:24 -070036 * Coordinates with the {@link UiTopoLayoutService} to access
37 * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which
Simon Huntcda9c032016-04-11 10:32:54 -070038 * maintains a local model of the network entities, tailored specifically
39 * for displaying on the UI.
Simon Hunted804d52016-03-30 09:51:40 -070040 * <p>
41 * Note that an instance of this class will be created for each
Simon Huntf679c4e2016-04-01 17:02:24 -070042 * {@link UiWebSocket} connection, and will contain
Simon Hunted804d52016-03-30 09:51:40 -070043 * the state of how the topology is laid out for the logged-in user.
Simon Huntd5b96732016-07-08 13:22:27 -070044 * <p>
45 * The expected pattern is for the {@link Topo2ViewMessageHandler} to obtain
46 * a reference to the session instance (via the {@link UiWebSocket}), and
47 * interact with it when topo-related events come in from the client.
Simon Hunted804d52016-03-30 09:51:40 -070048 */
Simon Huntcda9c032016-04-11 10:32:54 -070049public class UiTopoSession implements UiModelListener {
Simon Hunt977aa052016-07-20 17:08:29 -070050
Simon Hunted804d52016-03-30 09:51:40 -070051 private final Logger log = LoggerFactory.getLogger(getClass());
52
Simon Huntf679c4e2016-04-01 17:02:24 -070053 private final UiWebSocket webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070054 private final String username;
55
56 final UiSharedTopologyModel sharedModel;
Simon Hunted804d52016-03-30 09:51:40 -070057
58 private boolean registered = false;
59
Thomas Vachuska92b016b2016-05-20 11:37:57 -070060 private UiTopoLayoutService layoutService;
Simon Hunt7092cc42016-04-06 18:40:17 -070061 private UiTopoLayout currentLayout;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070062 private boolean messagesEnabled;
Simon Huntf679c4e2016-04-01 17:02:24 -070063
Simon Hunted804d52016-03-30 09:51:40 -070064 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070065 * Creates a new topology session for the specified web socket connection.
66 *
Thomas Vachuska92b016b2016-05-20 11:37:57 -070067 * @param webSocket web socket
68 * @param model share topology model
69 * @param layoutService topology layout service
Simon Hunted804d52016-03-30 09:51:40 -070070 */
Thomas Vachuska92b016b2016-05-20 11:37:57 -070071 public UiTopoSession(UiWebSocket webSocket,
72 UiSharedTopologyModel model,
73 UiTopoLayoutService layoutService) {
Simon Huntf679c4e2016-04-01 17:02:24 -070074 this.webSocket = webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070075 this.username = webSocket.userName();
Simon Huntcda9c032016-04-11 10:32:54 -070076 this.sharedModel = model;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070077 this.layoutService = layoutService;
Simon Hunted804d52016-03-30 09:51:40 -070078 }
79
Simon Hunt977aa052016-07-20 17:08:29 -070080 // constructs a neutered instance, for unit testing
81 UiTopoSession() {
82 webSocket = null;
83 username = null;
84 sharedModel = null;
85 }
86
Simon Hunted804d52016-03-30 09:51:40 -070087 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070088 * Initializes the session; registering with the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070089 */
90 public void init() {
91 if (!registered) {
Simon Hunt7092cc42016-04-06 18:40:17 -070092 log.debug("{} : Registering with shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070093 sharedModel.register(this);
Thomas Vachuska92b016b2016-05-20 11:37:57 -070094 currentLayout = layoutService.getRootLayout();
Simon Hunted804d52016-03-30 09:51:40 -070095 registered = true;
96 } else {
97 log.warn("already registered");
98 }
99 }
100
101 /**
Simon Hunt7092cc42016-04-06 18:40:17 -0700102 * Destroys the session; unregistering from the shared model.
Simon Hunted804d52016-03-30 09:51:40 -0700103 */
104 public void destroy() {
Simon Hunt7092cc42016-04-06 18:40:17 -0700105 if (registered) {
106 log.debug("{} : Unregistering from shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -0700107 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -0700108 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -0700109 } else {
110 log.warn("already unregistered");
111 }
112 }
113
114 @Override
115 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -0700116 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -0700117 }
Simon Huntcda9c032016-04-11 10:32:54 -0700118
119 @Override
120 public void event(UiModelEvent event) {
Yuta HIGUCHI28ad09a2016-06-28 17:08:29 -0700121 log.debug("Event received: {}", event);
Simon Huntcda9c032016-04-11 10:32:54 -0700122 // TODO: handle model events from the cache...
123 }
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700124
125 /**
126 * Returns the current layout context.
127 *
128 * @return current topology layout
129 */
130 public UiTopoLayout currentLayout() {
131 return currentLayout;
132 }
133
134 /**
135 * Changes the current layout context to the specified layout.
136 *
137 * @param topoLayout new topology layout context
138 */
139 public void setCurrentLayout(UiTopoLayout topoLayout) {
140 currentLayout = topoLayout;
141 }
142
143 /**
144 * Enables or disables the transmission of topology event update messages.
145 *
146 * @param enabled true if messages should be sent
147 */
148 public void enableEvent(boolean enabled) {
149 messagesEnabled = enabled;
150 }
Simon Huntd5b96732016-07-08 13:22:27 -0700151
152 /**
153 * Returns the list of ONOS instances (cluster members).
154 *
155 * @return the list of ONOS instances
156 */
157 public List<UiClusterMember> getAllInstances() {
158 return sharedModel.getClusterMembers();
159 }
160
161 /**
162 * Returns the region for the specified layout.
163 *
164 * @param layout layout filter
165 * @return region that the layout is based upon
166 */
167 public UiRegion getRegion(UiTopoLayout layout) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700168 RegionId rid = layout.regionId();
169 return rid == null ? sharedModel.getNullRegion() : sharedModel.getRegion(rid);
Simon Huntd5b96732016-07-08 13:22:27 -0700170 }
Simon Hunt977aa052016-07-20 17:08:29 -0700171
172 /**
173 * Returns the regions that are "peers" to this region. That is, based on
174 * the layout the user is viewing, all the regions that are associated with
Simon Huntb1ce2602016-07-23 14:04:31 -0700175 * layouts that share the same parent layout as this layout.
Simon Hunt977aa052016-07-20 17:08:29 -0700176 *
177 * @param layout the layout being viewed
178 * @return all regions that are "siblings" to this layout's region
179 */
180 public Set<UiRegion> getPeerRegions(UiTopoLayout layout) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700181 Set<UiTopoLayout> peerLayouts = layoutService.getPeers(layout.id());
182 Set<UiRegion> peers = new HashSet<>();
183 peerLayouts.forEach(l -> peers.add(sharedModel.getRegion(l.regionId())));
184 return peers;
Simon Hunt977aa052016-07-20 17:08:29 -0700185 }
186
187 /**
188 * Returns the subregions of the region in the specified layout.
189 *
190 * @param layout the layout being viewed
191 * @return all regions that are "contained within" this layout's region
192 */
193 public Set<UiRegion> getSubRegions(UiTopoLayout layout) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700194 Set<UiTopoLayout> kidLayouts = layoutService.getChildren(layout.id());
195 Set<UiRegion> kids = new HashSet<>();
196 kidLayouts.forEach(l -> kids.add(sharedModel.getRegion(l.regionId())));
197 return kids;
Simon Hunt977aa052016-07-20 17:08:29 -0700198 }
199
200 /**
Simon Huntb1ce2602016-07-23 14:04:31 -0700201 * Refreshes the model's internal state.
Simon Hunt977aa052016-07-20 17:08:29 -0700202 */
Simon Huntb1ce2602016-07-23 14:04:31 -0700203 public void refreshModel() {
204 sharedModel.refresh();
Simon Hunt977aa052016-07-20 17:08:29 -0700205 }
Simon Hunted804d52016-03-30 09:51:40 -0700206}