blob: ed9eead1f177382d1966c4519baad7d23fe9fb66 [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;
Thomas Vachuska0af26912016-03-21 21:37:30 -070041import org.onlab.util.KryoNamespace;
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;
44import org.onosproject.store.service.EventuallyConsistentMap;
45import org.onosproject.store.service.EventuallyConsistentMapEvent;
46import org.onosproject.store.service.EventuallyConsistentMapListener;
47import org.onosproject.store.service.StorageService;
48import org.onosproject.store.service.WallClockTimestamp;
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 Hunte05cae42015-07-23 17:35:24 -070053import org.onosproject.ui.UiTopoOverlayFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080054import org.onosproject.ui.UiView;
Simon Hunt1002cd82015-04-23 14:44:03 -070055import org.onosproject.ui.UiViewHidden;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
Thomas Vachuska0af26912016-03-21 21:37:30 -070059import java.util.LinkedHashMap;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080060import java.util.List;
61import java.util.Map;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070062import java.util.Set;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063
64import static com.google.common.collect.ImmutableList.of;
65import static java.util.stream.Collectors.toSet;
Heedo Kang4a47a302016-02-29 17:40:23 +090066import static org.onosproject.security.AppGuard.checkPermission;
67import static org.onosproject.security.AppPermission.Type.UI_READ;
68import static org.onosproject.security.AppPermission.Type.UI_WRITE;
Thomas Vachuska0af26912016-03-21 21:37:30 -070069import static org.onosproject.ui.UiView.Category.NETWORK;
70import static org.onosproject.ui.UiView.Category.PLATFORM;
Heedo Kang4a47a302016-02-29 17:40:23 +090071
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080072/**
73 * Manages the user interface extensions.
74 */
75@Component(immediate = true)
76@Service
Simon Hunt3678c2a2016-03-28 14:48:07 -070077public class UiExtensionManager
78 implements UiExtensionService, UiPreferencesService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080079
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070080 private static final ClassLoader CL = UiExtensionManager.class.getClassLoader();
Simon Hunt3678c2a2016-03-28 14:48:07 -070081
82 private static final String ONOS_USER_PREFERENCES = "onos-user-preferences";
Simon Hunte05cae42015-07-23 17:35:24 -070083 private static final String CORE = "core";
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070084 private static final String GUI_ADDED = "guiAdded";
85 private static final String GUI_REMOVED = "guiRemoved";
Thomas Vachuska0af26912016-03-21 21:37:30 -070086 private static final String UPDATE_PREFS = "updatePrefs";
Simon Hunt3678c2a2016-03-28 14:48:07 -070087 private static final String SLASH = "/";
88
89 private static final int IDX_USER = 0;
90 private static final int IDX_KEY = 1;
Simon Hunte05cae42015-07-23 17:35:24 -070091
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080092 private final Logger log = LoggerFactory.getLogger(getClass());
93
94 // List of all extensions
95 private final List<UiExtension> extensions = Lists.newArrayList();
96
97 // Map of views to extensions
98 private final Map<String, UiExtension> views = Maps.newHashMap();
99
100 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -0800101 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800102
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800105
Thomas Vachuska0af26912016-03-21 21:37:30 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected StorageService storageService;
108
109 // User preferences
110 private EventuallyConsistentMap<String, ObjectNode> prefs;
111 private final EventuallyConsistentMapListener<String, ObjectNode> prefsListener =
112 new InternalPrefsListener();
113
114 private final ObjectMapper mapper = new ObjectMapper();
115
Thomas Vachuska3553b302015-03-07 14:49:43 -0800116 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -0700117 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -0700118 List<UiView> coreViews = of(
119 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700120 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -0700121 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700122 new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"),
Simon Hunt20e16792015-04-24 14:29:39 -0700123 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
124 new UiView(NETWORK, "device", "Devices", "nav_devs"),
125 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700126 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700127 new UiViewHidden("group"),
Jian Li1f544732015-12-30 23:36:37 -0800128 new UiViewHidden("meter"),
Simon Hunt20e16792015-04-24 14:29:39 -0700129 new UiView(NETWORK, "link", "Links", "nav_links"),
130 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -0500131 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
132 //TODO add a new type of icon for tunnel
133 new UiView(NETWORK, "tunnel", "Tunnels", "nav_links")
Simon Hunt20e16792015-04-24 14:29:39 -0700134 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700135
Simon Hunt4c7edd32015-03-11 10:42:53 -0700136 UiMessageHandlerFactory messageHandlerFactory =
137 () -> ImmutableList.of(
Thomas Vachuska0af26912016-03-21 21:37:30 -0700138 new UserPreferencesMessageHandler(),
Thomas Vachuskae586b792015-03-26 13:59:38 -0700139 new TopologyViewMessageHandler(),
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700140 new MapSelectorMessageHandler(),
Thomas Vachuskae586b792015-03-26 13:59:38 -0700141 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700142 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700143 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700144 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700145 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700146 new GroupViewMessageHandler(),
Jian Li1f544732015-12-30 23:36:37 -0800147 new MeterViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700148 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700149 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700150 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500151 new ClusterViewMessageHandler(),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700152 new ProcessorViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500153 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700154 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700155
Simon Hunte05cae42015-07-23 17:35:24 -0700156 UiTopoOverlayFactory topoOverlayFactory =
157 () -> ImmutableList.of(
158 new TrafficOverlay()
159 );
160
161 return new UiExtension.Builder(CL, coreViews)
162 .messageHandlerFactory(messageHandlerFactory)
163 .topoOverlayFactory(topoOverlayFactory)
164 .resourcePath(CORE)
165 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800166 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800167
168 @Activate
169 public void activate() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700170 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
171 .register(KryoNamespaces.API)
172 .register(ObjectNode.class, ArrayNode.class,
Simon Hunt3678c2a2016-03-28 14:48:07 -0700173 JsonNodeFactory.class, LinkedHashMap.class,
174 TextNode.class, BooleanNode.class,
175 LongNode.class, DoubleNode.class, ShortNode.class,
176 IntNode.class, NullNode.class);
Thomas Vachuska0af26912016-03-21 21:37:30 -0700177
178 prefs = storageService.<String, ObjectNode>eventuallyConsistentMapBuilder()
Simon Hunt3678c2a2016-03-28 14:48:07 -0700179 .withName(ONOS_USER_PREFERENCES)
Thomas Vachuska0af26912016-03-21 21:37:30 -0700180 .withSerializer(kryoBuilder)
181 .withTimestampProvider((k, v) -> new WallClockTimestamp())
182 .withPersistence()
183 .build();
184 prefs.addListener(prefsListener);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800185 register(core);
186 log.info("Started");
187 }
188
189 @Deactivate
190 public void deactivate() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700191 prefs.removeListener(prefsListener);
HIGUCHI Yuta44b3d2d2016-04-14 13:43:48 -0700192 prefs.destroy();
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700193 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800194 unregister(core);
195 log.info("Stopped");
196 }
197
198 @Override
199 public synchronized void register(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900200 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800201 if (!extensions.contains(extension)) {
202 extensions.add(extension);
203 for (UiView view : extension.views()) {
204 views.put(view.id(), extension);
205 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700206 UiWebSocketServlet.sendToAll(GUI_ADDED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800207 }
208 }
209
210 @Override
211 public synchronized void unregister(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900212 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800213 extensions.remove(extension);
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700214 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
215 UiWebSocketServlet.sendToAll(GUI_REMOVED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800216 }
217
218 @Override
219 public synchronized List<UiExtension> getExtensions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900220 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800221 return ImmutableList.copyOf(extensions);
222 }
223
224 @Override
225 public synchronized UiExtension getViewExtension(String viewId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900226 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800227 return views.get(viewId);
228 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700229
Thomas Vachuska0af26912016-03-21 21:37:30 -0700230 @Override
231 public Set<String> getUserNames() {
232 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
233 prefs.keySet().forEach(k -> builder.add(userName(k)));
234 return builder.build();
235 }
236
237 @Override
238 public Map<String, ObjectNode> getPreferences(String userName) {
239 ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder();
240 prefs.entrySet().stream()
Simon Hunt3678c2a2016-03-28 14:48:07 -0700241 .filter(e -> e.getKey().startsWith(userName + SLASH))
Thomas Vachuska0af26912016-03-21 21:37:30 -0700242 .forEach(e -> builder.put(keyName(e.getKey()), e.getValue()));
243 return builder.build();
244 }
245
246 @Override
247 public void setPreference(String userName, String preference, ObjectNode value) {
248 prefs.put(key(userName, preference), value);
249 }
250
Simon Huntc54cd1b2015-05-11 13:43:44 -0700251 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700252 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700253
254 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700255
256 @Override
257 public Set<String> getNames() {
258 return ImmutableSet.copyOf(sprites.keySet());
259 }
260
261 @Override
262 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700263 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700264 sprites.put(name, spriteData);
265 }
266
267 @Override
268 public JsonNode get(String name) {
269 return sprites.get(name);
270 }
271
Thomas Vachuska0af26912016-03-21 21:37:30 -0700272 private String key(String userName, String keyName) {
Simon Hunt3678c2a2016-03-28 14:48:07 -0700273 return userName + SLASH + keyName;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700274 }
275
Simon Hunt3678c2a2016-03-28 14:48:07 -0700276
Thomas Vachuska0af26912016-03-21 21:37:30 -0700277 private String userName(String key) {
Simon Hunt3678c2a2016-03-28 14:48:07 -0700278 return key.split(SLASH)[IDX_USER];
Thomas Vachuska0af26912016-03-21 21:37:30 -0700279 }
280
281 private String keyName(String key) {
Simon Hunt3678c2a2016-03-28 14:48:07 -0700282 return key.split(SLASH)[IDX_KEY];
Thomas Vachuska0af26912016-03-21 21:37:30 -0700283 }
284
285 // Auxiliary listener to preference map events.
286 private class InternalPrefsListener
287 implements EventuallyConsistentMapListener<String, ObjectNode> {
288 @Override
289 public void event(EventuallyConsistentMapEvent<String, ObjectNode> event) {
290 String userName = userName(event.key());
291 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
292 UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs());
293 }
294 }
295
296 private ObjectNode jsonPrefs() {
297 ObjectNode json = mapper.createObjectNode();
298 prefs.entrySet().forEach(e -> json.set(keyName(e.getKey()), e.getValue()));
299 return json;
300 }
301 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800302}