blob: 611a22d44ea188672256f500703aa53e2f54b00d [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;
Simon Hunt977aa052016-07-20 17:08:29 -070025import org.onosproject.ui.model.topo.UiDevice;
26import org.onosproject.ui.model.topo.UiHost;
27import org.onosproject.ui.model.topo.UiLink;
Simon Huntd5b96732016-07-08 13:22:27 -070028import org.onosproject.ui.model.topo.UiRegion;
Simon Huntf679c4e2016-04-01 17:02:24 -070029import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunted804d52016-03-30 09:51:40 -070030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Simon Hunt977aa052016-07-20 17:08:29 -070033import java.util.Collections;
Simon Huntd5b96732016-07-08 13:22:27 -070034import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070035import java.util.Set;
Simon Huntd5b96732016-07-08 13:22:27 -070036
Simon Hunted804d52016-03-30 09:51:40 -070037/**
Simon Huntf679c4e2016-04-01 17:02:24 -070038 * Coordinates with the {@link UiTopoLayoutService} to access
39 * {@link UiTopoLayout}s, and with the {@link UiSharedTopologyModel} which
Simon Huntcda9c032016-04-11 10:32:54 -070040 * maintains a local model of the network entities, tailored specifically
41 * for displaying on the UI.
Simon Hunted804d52016-03-30 09:51:40 -070042 * <p>
43 * Note that an instance of this class will be created for each
Simon Huntf679c4e2016-04-01 17:02:24 -070044 * {@link UiWebSocket} connection, and will contain
Simon Hunted804d52016-03-30 09:51:40 -070045 * the state of how the topology is laid out for the logged-in user.
Simon Huntd5b96732016-07-08 13:22:27 -070046 * <p>
47 * The expected pattern is for the {@link Topo2ViewMessageHandler} to obtain
48 * a reference to the session instance (via the {@link UiWebSocket}), and
49 * interact with it when topo-related events come in from the client.
Simon Hunted804d52016-03-30 09:51:40 -070050 */
Simon Huntcda9c032016-04-11 10:32:54 -070051public class UiTopoSession implements UiModelListener {
Simon Hunt977aa052016-07-20 17:08:29 -070052
Simon Hunted804d52016-03-30 09:51:40 -070053 private final Logger log = LoggerFactory.getLogger(getClass());
54
Simon Huntf679c4e2016-04-01 17:02:24 -070055 private final UiWebSocket webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070056 private final String username;
57
58 final UiSharedTopologyModel sharedModel;
Simon Hunted804d52016-03-30 09:51:40 -070059
60 private boolean registered = false;
61
Thomas Vachuska92b016b2016-05-20 11:37:57 -070062 private UiTopoLayoutService layoutService;
Simon Hunt7092cc42016-04-06 18:40:17 -070063 private UiTopoLayout currentLayout;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070064 private boolean messagesEnabled;
Simon Huntf679c4e2016-04-01 17:02:24 -070065
Simon Hunted804d52016-03-30 09:51:40 -070066 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070067 * Creates a new topology session for the specified web socket connection.
68 *
Thomas Vachuska92b016b2016-05-20 11:37:57 -070069 * @param webSocket web socket
70 * @param model share topology model
71 * @param layoutService topology layout service
Simon Hunted804d52016-03-30 09:51:40 -070072 */
Thomas Vachuska92b016b2016-05-20 11:37:57 -070073 public UiTopoSession(UiWebSocket webSocket,
74 UiSharedTopologyModel model,
75 UiTopoLayoutService layoutService) {
Simon Huntf679c4e2016-04-01 17:02:24 -070076 this.webSocket = webSocket;
Simon Hunt7092cc42016-04-06 18:40:17 -070077 this.username = webSocket.userName();
Simon Huntcda9c032016-04-11 10:32:54 -070078 this.sharedModel = model;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070079 this.layoutService = layoutService;
Simon Hunted804d52016-03-30 09:51:40 -070080 }
81
Simon Hunt977aa052016-07-20 17:08:29 -070082 // constructs a neutered instance, for unit testing
83 UiTopoSession() {
84 webSocket = null;
85 username = null;
86 sharedModel = null;
87 }
88
Simon Hunted804d52016-03-30 09:51:40 -070089 /**
Simon Hunt7092cc42016-04-06 18:40:17 -070090 * Initializes the session; registering with the shared model.
Simon Hunted804d52016-03-30 09:51:40 -070091 */
92 public void init() {
93 if (!registered) {
Simon Hunt7092cc42016-04-06 18:40:17 -070094 log.debug("{} : Registering with shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -070095 sharedModel.register(this);
Thomas Vachuska92b016b2016-05-20 11:37:57 -070096 currentLayout = layoutService.getRootLayout();
Simon Hunted804d52016-03-30 09:51:40 -070097 registered = true;
98 } else {
99 log.warn("already registered");
100 }
101 }
102
103 /**
Simon Hunt7092cc42016-04-06 18:40:17 -0700104 * Destroys the session; unregistering from the shared model.
Simon Hunted804d52016-03-30 09:51:40 -0700105 */
106 public void destroy() {
Simon Hunt7092cc42016-04-06 18:40:17 -0700107 if (registered) {
108 log.debug("{} : Unregistering from shared model", this);
Simon Hunted804d52016-03-30 09:51:40 -0700109 sharedModel.unregister(this);
Simon Huntf679c4e2016-04-01 17:02:24 -0700110 registered = false;
Simon Hunted804d52016-03-30 09:51:40 -0700111 } else {
112 log.warn("already unregistered");
113 }
114 }
115
116 @Override
117 public String toString() {
Simon Huntf679c4e2016-04-01 17:02:24 -0700118 return String.format("{UiTopoSession for user <%s>}", username);
Simon Hunted804d52016-03-30 09:51:40 -0700119 }
Simon Huntcda9c032016-04-11 10:32:54 -0700120
121 @Override
122 public void event(UiModelEvent event) {
Yuta HIGUCHI28ad09a2016-06-28 17:08:29 -0700123 log.debug("Event received: {}", event);
Simon Huntcda9c032016-04-11 10:32:54 -0700124 // TODO: handle model events from the cache...
125 }
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700126
127 /**
128 * Returns the current layout context.
129 *
130 * @return current topology layout
131 */
132 public UiTopoLayout currentLayout() {
133 return currentLayout;
134 }
135
136 /**
137 * Changes the current layout context to the specified layout.
138 *
139 * @param topoLayout new topology layout context
140 */
141 public void setCurrentLayout(UiTopoLayout topoLayout) {
142 currentLayout = topoLayout;
143 }
144
145 /**
146 * Enables or disables the transmission of topology event update messages.
147 *
148 * @param enabled true if messages should be sent
149 */
150 public void enableEvent(boolean enabled) {
151 messagesEnabled = enabled;
152 }
Simon Huntd5b96732016-07-08 13:22:27 -0700153
154 /**
155 * Returns the list of ONOS instances (cluster members).
156 *
157 * @return the list of ONOS instances
158 */
159 public List<UiClusterMember> getAllInstances() {
160 return sharedModel.getClusterMembers();
161 }
162
163 /**
164 * Returns the region for the specified layout.
165 *
166 * @param layout layout filter
167 * @return region that the layout is based upon
168 */
169 public UiRegion getRegion(UiTopoLayout layout) {
Simon Hunt977aa052016-07-20 17:08:29 -0700170 return sharedModel.getRegion(layout.regionId());
Simon Huntd5b96732016-07-08 13:22:27 -0700171 }
Simon Hunt977aa052016-07-20 17:08:29 -0700172
173 /**
174 * Returns the regions that are "peers" to this region. That is, based on
175 * the layout the user is viewing, all the regions that are associated with
176 * layouts that are children of the parent layout to this layout.
177 *
178 * @param layout the layout being viewed
179 * @return all regions that are "siblings" to this layout's region
180 */
181 public Set<UiRegion> getPeerRegions(UiTopoLayout layout) {
182 UiRegion currentRegion = getRegion(layout);
183
184 // TODO: consult topo layout service to get hierarchy info...
185 // TODO: then consult shared model to get regions
186 return Collections.emptySet();
187 }
188
189 /**
190 * Returns the subregions of the region in the specified layout.
191 *
192 * @param layout the layout being viewed
193 * @return all regions that are "contained within" this layout's region
194 */
195 public Set<UiRegion> getSubRegions(UiTopoLayout layout) {
196 UiRegion currentRegion = getRegion(layout);
197
198 // TODO: consult topo layout service to get child layouts...
199 // TODO: then consult shared model to get regions
200 return Collections.emptySet();
201 }
202
203
204 /**
205 * Returns all devices that are not in a region.
206 *
207 * @return all devices not in a region
208 */
209 public Set<UiDevice> getOrphanDevices() {
210 // TODO: get devices with no region
211 return Collections.emptySet();
212 }
213
214 /**
215 * Returns all hosts that are not in a region.
216 *
217 * @return all hosts not in a region
218 */
219 public Set<UiHost> getOrphanHosts() {
220 // TODO: get hosts with no region
221 return Collections.emptySet();
222 }
223
224 /**
225 * Returns all links that are not in a region.
226 *
227 * @return all links not in a region
228 */
229 public Set<UiLink> getOrphanLinks() {
230 // TODO: get links with no region
231 return Collections.emptySet();
232 }
233
Simon Hunted804d52016-03-30 09:51:40 -0700234}