blob: 17188cb56019d8e53e9a5e7f29898bdc15d059a0 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08003 *
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 */
16package org.onosproject.ui.impl;
17
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska0af26912016-03-21 21:37:30 -070019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.BooleanNode;
22import com.fasterxml.jackson.databind.node.DoubleNode;
23import com.fasterxml.jackson.databind.node.IntNode;
24import com.fasterxml.jackson.databind.node.JsonNodeFactory;
25import com.fasterxml.jackson.databind.node.LongNode;
26import com.fasterxml.jackson.databind.node.NullNode;
27import com.fasterxml.jackson.databind.node.ObjectNode;
28import com.fasterxml.jackson.databind.node.ShortNode;
29import com.fasterxml.jackson.databind.node.TextNode;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080030import com.google.common.collect.ImmutableList;
Thomas Vachuska0af26912016-03-21 21:37:30 -070031import com.google.common.collect.ImmutableMap;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070032import com.google.common.collect.ImmutableSet;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080033import com.google.common.collect.Lists;
34import com.google.common.collect.Maps;
35import org.apache.felix.scr.annotations.Activate;
36import org.apache.felix.scr.annotations.Component;
37import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska51f540f2015-05-27 17:26:57 -070038import org.apache.felix.scr.annotations.Reference;
39import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040import org.apache.felix.scr.annotations.Service;
Madan Jampanibf8ee802016-05-04 14:07:36 -070041import org.onlab.util.Tools;
Thomas Vachuska51f540f2015-05-27 17:26:57 -070042import org.onosproject.mastership.MastershipService;
Thomas Vachuska0af26912016-03-21 21:37:30 -070043import org.onosproject.store.serializers.KryoNamespaces;
Madan Jampani7b93ceb2016-05-04 09:58:40 -070044import org.onosproject.store.service.ConsistentMap;
45import org.onosproject.store.service.MapEvent;
46import org.onosproject.store.service.MapEventListener;
47import org.onosproject.store.service.Serializer;
Thomas Vachuska0af26912016-03-21 21:37:30 -070048import org.onosproject.store.service.StorageService;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080049import org.onosproject.ui.UiExtension;
50import org.onosproject.ui.UiExtensionService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080051import org.onosproject.ui.UiMessageHandlerFactory;
Thomas Vachuska0af26912016-03-21 21:37:30 -070052import org.onosproject.ui.UiPreferencesService;
Simon Huntd5b96732016-07-08 13:22:27 -070053import org.onosproject.ui.UiTopoMap;
Steven Burrows3a9a6442016-05-05 15:31:16 +010054import org.onosproject.ui.UiTopoMapFactory;
Simon Huntd5b96732016-07-08 13:22:27 -070055import org.onosproject.ui.UiTopoOverlayFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080056import org.onosproject.ui.UiView;
Simon Hunt1002cd82015-04-23 14:44:03 -070057import org.onosproject.ui.UiViewHidden;
Simon Huntd5b96732016-07-08 13:22:27 -070058import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080059import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
61
Thomas Vachuska0af26912016-03-21 21:37:30 -070062import java.util.LinkedHashMap;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063import java.util.List;
64import java.util.Map;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070065import java.util.Set;
Madan Jampanibf8ee802016-05-04 14:07:36 -070066import java.util.concurrent.ExecutorService;
67import java.util.concurrent.Executors;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080068
69import static com.google.common.collect.ImmutableList.of;
70import static java.util.stream.Collectors.toSet;
Heedo Kang4a47a302016-02-29 17:40:23 +090071import static org.onosproject.security.AppGuard.checkPermission;
72import static org.onosproject.security.AppPermission.Type.UI_READ;
73import static org.onosproject.security.AppPermission.Type.UI_WRITE;
Thomas Vachuska0af26912016-03-21 21:37:30 -070074import static org.onosproject.ui.UiView.Category.NETWORK;
75import static org.onosproject.ui.UiView.Category.PLATFORM;
Heedo Kang4a47a302016-02-29 17:40:23 +090076
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080077/**
78 * Manages the user interface extensions.
79 */
80@Component(immediate = true)
81@Service
Simon Hunt3678c2a2016-03-28 14:48:07 -070082public class UiExtensionManager
83 implements UiExtensionService, UiPreferencesService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080084
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070085 private static final ClassLoader CL = UiExtensionManager.class.getClassLoader();
Simon Hunt3678c2a2016-03-28 14:48:07 -070086
Madan Jampani7b93ceb2016-05-04 09:58:40 -070087 private static final String ONOS_USER_PREFERENCES = "onos-ui-user-preferences";
Simon Hunte05cae42015-07-23 17:35:24 -070088 private static final String CORE = "core";
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070089 private static final String GUI_ADDED = "guiAdded";
90 private static final String GUI_REMOVED = "guiRemoved";
Thomas Vachuska0af26912016-03-21 21:37:30 -070091 private static final String UPDATE_PREFS = "updatePrefs";
Simon Hunt3678c2a2016-03-28 14:48:07 -070092 private static final String SLASH = "/";
93
94 private static final int IDX_USER = 0;
95 private static final int IDX_KEY = 1;
Simon Hunte05cae42015-07-23 17:35:24 -070096
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080097 private final Logger log = LoggerFactory.getLogger(getClass());
98
99 // List of all extensions
100 private final List<UiExtension> extensions = Lists.newArrayList();
101
102 // Map of views to extensions
103 private final Map<String, UiExtension> views = Maps.newHashMap();
104
105 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -0800106 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800107
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800110
Thomas Vachuska0af26912016-03-21 21:37:30 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected StorageService storageService;
113
114 // User preferences
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700115 private ConsistentMap<String, ObjectNode> prefsConsistentMap;
116 private Map<String, ObjectNode> prefs;
117 private final MapEventListener<String, ObjectNode> prefsListener =
Thomas Vachuska0af26912016-03-21 21:37:30 -0700118 new InternalPrefsListener();
119
120 private final ObjectMapper mapper = new ObjectMapper();
121
Madan Jampanibf8ee802016-05-04 14:07:36 -0700122 private final ExecutorService eventHandlingExecutor =
Simon Huntd5b96732016-07-08 13:22:27 -0700123 Executors.newSingleThreadExecutor(
124 Tools.groupedThreads("onos/ui-ext-manager", "event-handler", log));
Madan Jampanibf8ee802016-05-04 14:07:36 -0700125
Thomas Vachuska3553b302015-03-07 14:49:43 -0800126 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -0700127 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -0700128 List<UiView> coreViews = of(
129 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700130 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -0700131 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700132 new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"),
Simon Hunt20e16792015-04-24 14:29:39 -0700133 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
Simon Huntd5b96732016-07-08 13:22:27 -0700134
135 // FIXME: leave commented out for now, while still under development
Simon Hunt1bee5292016-10-14 11:02:33 -0700136 // (remember to also comment out inclusions in index.html)
Simon Huntd5b96732016-07-08 13:22:27 -0700137// new UiView(NETWORK, "topo2", "New-Topo"),
Simon Hunt98189192016-07-29 19:02:27 -0700138// new UiView(NETWORK, "topoX", "Topo-X"),
Simon Huntd5b96732016-07-08 13:22:27 -0700139
Simon Hunt20e16792015-04-24 14:29:39 -0700140 new UiView(NETWORK, "device", "Devices", "nav_devs"),
141 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700142 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700143 new UiViewHidden("group"),
Jian Li1f544732015-12-30 23:36:37 -0800144 new UiViewHidden("meter"),
Simon Hunt20e16792015-04-24 14:29:39 -0700145 new UiView(NETWORK, "link", "Links", "nav_links"),
146 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -0500147 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
Simon Hunt986b92f2016-06-03 15:46:59 -0700148 new UiView(NETWORK, "tunnel", "Tunnels", "nav_tunnels")
Simon Hunt20e16792015-04-24 14:29:39 -0700149 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700150
Simon Hunt4c7edd32015-03-11 10:42:53 -0700151 UiMessageHandlerFactory messageHandlerFactory =
152 () -> ImmutableList.of(
Thomas Vachuska0af26912016-03-21 21:37:30 -0700153 new UserPreferencesMessageHandler(),
Thomas Vachuskae586b792015-03-26 13:59:38 -0700154 new TopologyViewMessageHandler(),
Simon Huntd5b96732016-07-08 13:22:27 -0700155 new Topo2ViewMessageHandler(),
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700156 new MapSelectorMessageHandler(),
Thomas Vachuskae586b792015-03-26 13:59:38 -0700157 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700158 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700159 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700160 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700161 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700162 new GroupViewMessageHandler(),
Jian Li1f544732015-12-30 23:36:37 -0800163 new MeterViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700164 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700165 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700166 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500167 new ClusterViewMessageHandler(),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700168 new ProcessorViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500169 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700170 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700171
Simon Hunte05cae42015-07-23 17:35:24 -0700172 UiTopoOverlayFactory topoOverlayFactory =
173 () -> ImmutableList.of(
174 new TrafficOverlay()
175 );
176
Steven Burrows3a9a6442016-05-05 15:31:16 +0100177 UiTopoMapFactory topoMapFactory =
178 () -> ImmutableList.of(
179 new UiTopoMap("australia", "Australia", "*australia", 1.0),
180 new UiTopoMap("americas", "North, Central and South America", "*americas", 0.7),
181 new UiTopoMap("n_america", "North America", "*n_america", 0.9),
182 new UiTopoMap("s_america", "South America", "*s_america", 0.9),
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700183 new UiTopoMap("usa", "United States", "*continental_us", 1.3),
Steven Burrows3a9a6442016-05-05 15:31:16 +0100184 new UiTopoMap("bayareaGEO", "Bay Area, California", "*bayarea", 1.0),
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700185 new UiTopoMap("europe", "Europe", "*europe", 10.0),
Steven Burrows3a9a6442016-05-05 15:31:16 +0100186 new UiTopoMap("italy", "Italy", "*italy", 0.8),
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700187 new UiTopoMap("uk", "United Kingdom and Ireland", "*uk", 2.0),
Steven Burrows3a9a6442016-05-05 15:31:16 +0100188 new UiTopoMap("japan", "Japan", "*japan", 0.8),
189 new UiTopoMap("s_korea", "South Korea", "*s_korea", 0.75),
190 new UiTopoMap("taiwan", "Taiwan", "*taiwan", 0.7),
191 new UiTopoMap("africa", "Africa", "*africa", 0.7),
192 new UiTopoMap("oceania", "Oceania", "*oceania", 0.7),
193 new UiTopoMap("asia", "Asia", "*asia", 0.7)
194 );
195
Simon Hunte05cae42015-07-23 17:35:24 -0700196 return new UiExtension.Builder(CL, coreViews)
197 .messageHandlerFactory(messageHandlerFactory)
198 .topoOverlayFactory(topoOverlayFactory)
Steven Burrows3a9a6442016-05-05 15:31:16 +0100199 .topoMapFactory(topoMapFactory)
Simon Hunte05cae42015-07-23 17:35:24 -0700200 .resourcePath(CORE)
201 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800202 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800203
204 @Activate
205 public void activate() {
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700206 Serializer serializer = Serializer.using(KryoNamespaces.API,
Simon Huntd5b96732016-07-08 13:22:27 -0700207 ObjectNode.class, ArrayNode.class,
208 JsonNodeFactory.class, LinkedHashMap.class,
209 TextNode.class, BooleanNode.class,
210 LongNode.class, DoubleNode.class, ShortNode.class,
211 IntNode.class, NullNode.class);
Thomas Vachuska0af26912016-03-21 21:37:30 -0700212
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700213 prefsConsistentMap = storageService.<String, ObjectNode>consistentMapBuilder()
Simon Hunt3678c2a2016-03-28 14:48:07 -0700214 .withName(ONOS_USER_PREFERENCES)
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700215 .withSerializer(serializer)
216 .withRelaxedReadConsistency()
Thomas Vachuska0af26912016-03-21 21:37:30 -0700217 .build();
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700218 prefsConsistentMap.addListener(prefsListener);
219 prefs = prefsConsistentMap.asJavaMap();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800220 register(core);
221 log.info("Started");
222 }
223
224 @Deactivate
225 public void deactivate() {
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700226 prefsConsistentMap.removeListener(prefsListener);
Madan Jampanibf8ee802016-05-04 14:07:36 -0700227 eventHandlingExecutor.shutdown();
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700228 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800229 unregister(core);
230 log.info("Stopped");
231 }
232
233 @Override
234 public synchronized void register(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900235 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800236 if (!extensions.contains(extension)) {
237 extensions.add(extension);
238 for (UiView view : extension.views()) {
239 views.put(view.id(), extension);
240 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700241 UiWebSocketServlet.sendToAll(GUI_ADDED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800242 }
243 }
244
245 @Override
246 public synchronized void unregister(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900247 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800248 extensions.remove(extension);
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700249 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
250 UiWebSocketServlet.sendToAll(GUI_REMOVED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800251 }
252
253 @Override
254 public synchronized List<UiExtension> getExtensions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900255 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800256 return ImmutableList.copyOf(extensions);
257 }
258
259 @Override
260 public synchronized UiExtension getViewExtension(String viewId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900261 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800262 return views.get(viewId);
263 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700264
Thomas Vachuska0af26912016-03-21 21:37:30 -0700265 @Override
266 public Set<String> getUserNames() {
267 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
268 prefs.keySet().forEach(k -> builder.add(userName(k)));
269 return builder.build();
270 }
271
272 @Override
273 public Map<String, ObjectNode> getPreferences(String userName) {
274 ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder();
275 prefs.entrySet().stream()
Simon Hunt3678c2a2016-03-28 14:48:07 -0700276 .filter(e -> e.getKey().startsWith(userName + SLASH))
Thomas Vachuska0af26912016-03-21 21:37:30 -0700277 .forEach(e -> builder.put(keyName(e.getKey()), e.getValue()));
278 return builder.build();
279 }
280
281 @Override
282 public void setPreference(String userName, String preference, ObjectNode value) {
283 prefs.put(key(userName, preference), value);
284 }
285
Simon Huntc54cd1b2015-05-11 13:43:44 -0700286 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700287 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700288
289 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700290
291 @Override
292 public Set<String> getNames() {
293 return ImmutableSet.copyOf(sprites.keySet());
294 }
295
296 @Override
297 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700298 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700299 sprites.put(name, spriteData);
300 }
301
302 @Override
303 public JsonNode get(String name) {
304 return sprites.get(name);
305 }
306
Thomas Vachuska0af26912016-03-21 21:37:30 -0700307 private String key(String userName, String keyName) {
Simon Hunt3678c2a2016-03-28 14:48:07 -0700308 return userName + SLASH + keyName;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700309 }
310
Simon Hunt3678c2a2016-03-28 14:48:07 -0700311
Thomas Vachuska0af26912016-03-21 21:37:30 -0700312 private String userName(String key) {
Simon Hunt3678c2a2016-03-28 14:48:07 -0700313 return key.split(SLASH)[IDX_USER];
Thomas Vachuska0af26912016-03-21 21:37:30 -0700314 }
315
316 private String keyName(String key) {
Simon Hunt3678c2a2016-03-28 14:48:07 -0700317 return key.split(SLASH)[IDX_KEY];
Thomas Vachuska0af26912016-03-21 21:37:30 -0700318 }
319
320 // Auxiliary listener to preference map events.
321 private class InternalPrefsListener
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700322 implements MapEventListener<String, ObjectNode> {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700323 @Override
Madan Jampani7b93ceb2016-05-04 09:58:40 -0700324 public void event(MapEvent<String, ObjectNode> event) {
Madan Jampanibf8ee802016-05-04 14:07:36 -0700325 eventHandlingExecutor.execute(() -> {
326 String userName = userName(event.key());
327 if (event.type() == MapEvent.Type.INSERT || event.type() == MapEvent.Type.UPDATE) {
328 UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs());
329 }
330 });
Thomas Vachuska0af26912016-03-21 21:37:30 -0700331 }
332
333 private ObjectNode jsonPrefs() {
334 ObjectNode json = mapper.createObjectNode();
335 prefs.entrySet().forEach(e -> json.set(keyName(e.getKey()), e.getValue()));
336 return json;
337 }
338 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800339}