blob: 55ff611dd79b945078df04d0bf92b582fe70e476 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Jian Li1f544732015-12-30 23:36:37 -08002 * Copyright 2015,2016 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
Thomas Vachuska0af26912016-03-21 21:37:30 -070077public class UiExtensionManager implements UiExtensionService, UiPreferencesService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080078
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070079 private static final ClassLoader CL = UiExtensionManager.class.getClassLoader();
Simon Hunte05cae42015-07-23 17:35:24 -070080 private static final String CORE = "core";
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070081 private static final String GUI_ADDED = "guiAdded";
82 private static final String GUI_REMOVED = "guiRemoved";
Thomas Vachuska0af26912016-03-21 21:37:30 -070083 private static final String UPDATE_PREFS = "updatePrefs";
Simon Hunte05cae42015-07-23 17:35:24 -070084
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080085 private final Logger log = LoggerFactory.getLogger(getClass());
86
87 // List of all extensions
88 private final List<UiExtension> extensions = Lists.newArrayList();
89
90 // Map of views to extensions
91 private final Map<String, UiExtension> views = Maps.newHashMap();
92
93 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080094 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080095
Thomas Vachuska51f540f2015-05-27 17:26:57 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080098
Thomas Vachuska0af26912016-03-21 21:37:30 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected StorageService storageService;
101
102 // User preferences
103 private EventuallyConsistentMap<String, ObjectNode> prefs;
104 private final EventuallyConsistentMapListener<String, ObjectNode> prefsListener =
105 new InternalPrefsListener();
106
107 private final ObjectMapper mapper = new ObjectMapper();
108
Thomas Vachuska3553b302015-03-07 14:49:43 -0800109 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -0700110 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -0700111 List<UiView> coreViews = of(
112 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700113 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -0700114 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700115 new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"),
Simon Hunt20e16792015-04-24 14:29:39 -0700116 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
117 new UiView(NETWORK, "device", "Devices", "nav_devs"),
118 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700119 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700120 new UiViewHidden("group"),
Jian Li1f544732015-12-30 23:36:37 -0800121 new UiViewHidden("meter"),
Simon Hunt20e16792015-04-24 14:29:39 -0700122 new UiView(NETWORK, "link", "Links", "nav_links"),
123 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -0500124 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
125 //TODO add a new type of icon for tunnel
126 new UiView(NETWORK, "tunnel", "Tunnels", "nav_links")
Simon Hunt20e16792015-04-24 14:29:39 -0700127 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700128
Simon Hunt4c7edd32015-03-11 10:42:53 -0700129 UiMessageHandlerFactory messageHandlerFactory =
130 () -> ImmutableList.of(
Thomas Vachuska0af26912016-03-21 21:37:30 -0700131 new UserPreferencesMessageHandler(),
Thomas Vachuskae586b792015-03-26 13:59:38 -0700132 new TopologyViewMessageHandler(),
133 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700134 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700135 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700136 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700137 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700138 new GroupViewMessageHandler(),
Jian Li1f544732015-12-30 23:36:37 -0800139 new MeterViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700140 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700141 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700142 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500143 new ClusterViewMessageHandler(),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700144 new ProcessorViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500145 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700146 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700147
Simon Hunte05cae42015-07-23 17:35:24 -0700148 UiTopoOverlayFactory topoOverlayFactory =
149 () -> ImmutableList.of(
150 new TrafficOverlay()
151 );
152
153 return new UiExtension.Builder(CL, coreViews)
154 .messageHandlerFactory(messageHandlerFactory)
155 .topoOverlayFactory(topoOverlayFactory)
156 .resourcePath(CORE)
157 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800158 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800159
160 @Activate
161 public void activate() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700162 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
163 .register(KryoNamespaces.API)
164 .register(ObjectNode.class, ArrayNode.class,
165 JsonNodeFactory.class, LinkedHashMap.class,
166 TextNode.class, BooleanNode.class,
167 LongNode.class, DoubleNode.class, ShortNode.class,
168 IntNode.class, NullNode.class);
169
170 prefs = storageService.<String, ObjectNode>eventuallyConsistentMapBuilder()
171 .withName("onos-user-preferences")
172 .withSerializer(kryoBuilder)
173 .withTimestampProvider((k, v) -> new WallClockTimestamp())
174 .withPersistence()
175 .build();
176 prefs.addListener(prefsListener);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800177 register(core);
178 log.info("Started");
179 }
180
181 @Deactivate
182 public void deactivate() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700183 prefs.removeListener(prefsListener);
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700184 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800185 unregister(core);
186 log.info("Stopped");
187 }
188
189 @Override
190 public synchronized void register(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900191 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800192 if (!extensions.contains(extension)) {
193 extensions.add(extension);
194 for (UiView view : extension.views()) {
195 views.put(view.id(), extension);
196 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700197 UiWebSocketServlet.sendToAll(GUI_ADDED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800198 }
199 }
200
201 @Override
202 public synchronized void unregister(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900203 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800204 extensions.remove(extension);
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700205 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
206 UiWebSocketServlet.sendToAll(GUI_REMOVED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800207 }
208
209 @Override
210 public synchronized List<UiExtension> getExtensions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900211 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800212 return ImmutableList.copyOf(extensions);
213 }
214
215 @Override
216 public synchronized UiExtension getViewExtension(String viewId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900217 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800218 return views.get(viewId);
219 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700220
Thomas Vachuska0af26912016-03-21 21:37:30 -0700221 @Override
222 public Set<String> getUserNames() {
223 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
224 prefs.keySet().forEach(k -> builder.add(userName(k)));
225 return builder.build();
226 }
227
228 @Override
229 public Map<String, ObjectNode> getPreferences(String userName) {
230 ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder();
231 prefs.entrySet().stream()
232 .filter(e -> e.getKey().startsWith(userName + "/"))
233 .forEach(e -> builder.put(keyName(e.getKey()), e.getValue()));
234 return builder.build();
235 }
236
237 @Override
238 public void setPreference(String userName, String preference, ObjectNode value) {
239 prefs.put(key(userName, preference), value);
240 }
241
Simon Huntc54cd1b2015-05-11 13:43:44 -0700242 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700243 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700244
245 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700246
247 @Override
248 public Set<String> getNames() {
249 return ImmutableSet.copyOf(sprites.keySet());
250 }
251
252 @Override
253 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700254 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700255 sprites.put(name, spriteData);
256 }
257
258 @Override
259 public JsonNode get(String name) {
260 return sprites.get(name);
261 }
262
Thomas Vachuska0af26912016-03-21 21:37:30 -0700263 private String key(String userName, String keyName) {
264 return userName + "/" + keyName;
265 }
266
267 private String userName(String key) {
268 return key.split("/")[0];
269 }
270
271 private String keyName(String key) {
272 return key.split("/")[1];
273 }
274
275 // Auxiliary listener to preference map events.
276 private class InternalPrefsListener
277 implements EventuallyConsistentMapListener<String, ObjectNode> {
278 @Override
279 public void event(EventuallyConsistentMapEvent<String, ObjectNode> event) {
280 String userName = userName(event.key());
281 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
282 UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs());
283 }
284 }
285
286 private ObjectNode jsonPrefs() {
287 ObjectNode json = mapper.createObjectNode();
288 prefs.entrySet().forEach(e -> json.set(keyName(e.getKey()), e.getValue()));
289 return json;
290 }
291 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800292}