blob: 3bf6c2763542aa9bc755cc371adac077dc0525a6 [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 Vachuskafe8c98a2015-02-04 01:24:32 -080019import com.google.common.collect.ImmutableList;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070020import com.google.common.collect.ImmutableSet;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080021import com.google.common.collect.Lists;
22import com.google.common.collect.Maps;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska51f540f2015-05-27 17:26:57 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080028import org.apache.felix.scr.annotations.Service;
Thomas Vachuska51f540f2015-05-27 17:26:57 -070029import org.onosproject.mastership.MastershipService;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080030import org.onosproject.ui.UiExtension;
31import org.onosproject.ui.UiExtensionService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080032import org.onosproject.ui.UiMessageHandlerFactory;
Simon Hunte05cae42015-07-23 17:35:24 -070033import org.onosproject.ui.UiTopoOverlayFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080034import org.onosproject.ui.UiView;
Simon Hunt1002cd82015-04-23 14:44:03 -070035import org.onosproject.ui.UiViewHidden;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import java.util.List;
40import java.util.Map;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070041import java.util.Set;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080042
43import static com.google.common.collect.ImmutableList.of;
44import static java.util.stream.Collectors.toSet;
Thomas Vachuska8b91f4f2015-04-23 17:55:36 -070045import static org.onosproject.ui.UiView.Category.NETWORK;
46import static org.onosproject.ui.UiView.Category.PLATFORM;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080047
Heedo Kang4a47a302016-02-29 17:40:23 +090048import static org.onosproject.security.AppGuard.checkPermission;
49import static org.onosproject.security.AppPermission.Type.UI_READ;
50import static org.onosproject.security.AppPermission.Type.UI_WRITE;
51
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080052/**
53 * Manages the user interface extensions.
54 */
55@Component(immediate = true)
56@Service
Simon Hunt93735af2015-07-28 12:10:30 -070057public class UiExtensionManager implements UiExtensionService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080058
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070059 private static final ClassLoader CL = UiExtensionManager.class.getClassLoader();
Simon Hunte05cae42015-07-23 17:35:24 -070060 private static final String CORE = "core";
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070061 private static final String GUI_ADDED = "guiAdded";
62 private static final String GUI_REMOVED = "guiRemoved";
Simon Hunte05cae42015-07-23 17:35:24 -070063
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080064 private final Logger log = LoggerFactory.getLogger(getClass());
65
66 // List of all extensions
67 private final List<UiExtension> extensions = Lists.newArrayList();
68
69 // Map of views to extensions
70 private final Map<String, UiExtension> views = Maps.newHashMap();
71
72 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080073 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080074
Simon Huntc54cd1b2015-05-11 13:43:44 -070075
Thomas Vachuska51f540f2015-05-27 17:26:57 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080078
79 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -070080 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -070081 List<UiView> coreViews = of(
82 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070083 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -070084 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
Thomas Vachuska3ece3732015-09-22 23:58:50 -070085 new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"),
Simon Hunt20e16792015-04-24 14:29:39 -070086 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
87 new UiView(NETWORK, "device", "Devices", "nav_devs"),
88 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070089 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070090 new UiViewHidden("group"),
Jian Li1f544732015-12-30 23:36:37 -080091 new UiViewHidden("meter"),
Simon Hunt20e16792015-04-24 14:29:39 -070092 new UiView(NETWORK, "link", "Links", "nav_links"),
93 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -050094 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
95 //TODO add a new type of icon for tunnel
96 new UiView(NETWORK, "tunnel", "Tunnels", "nav_links")
Simon Hunt20e16792015-04-24 14:29:39 -070097 );
Simon Hunt1002cd82015-04-23 14:44:03 -070098
Simon Hunt4c7edd32015-03-11 10:42:53 -070099 UiMessageHandlerFactory messageHandlerFactory =
100 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -0700101 new TopologyViewMessageHandler(),
102 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700103 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700104 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700105 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700106 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700107 new GroupViewMessageHandler(),
Jian Li1f544732015-12-30 23:36:37 -0800108 new MeterViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700109 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700110 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700111 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500112 new ClusterViewMessageHandler(),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700113 new ProcessorViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500114 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700115 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700116
Simon Hunte05cae42015-07-23 17:35:24 -0700117 UiTopoOverlayFactory topoOverlayFactory =
118 () -> ImmutableList.of(
119 new TrafficOverlay()
120 );
121
122 return new UiExtension.Builder(CL, coreViews)
123 .messageHandlerFactory(messageHandlerFactory)
124 .topoOverlayFactory(topoOverlayFactory)
125 .resourcePath(CORE)
126 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800127 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800128
129 @Activate
130 public void activate() {
131 register(core);
132 log.info("Started");
133 }
134
135 @Deactivate
136 public void deactivate() {
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700137 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800138 unregister(core);
139 log.info("Stopped");
140 }
141
142 @Override
143 public synchronized void register(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900144 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800145 if (!extensions.contains(extension)) {
146 extensions.add(extension);
147 for (UiView view : extension.views()) {
148 views.put(view.id(), extension);
149 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700150 UiWebSocketServlet.sendToAll(GUI_ADDED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800151 }
152 }
153
154 @Override
155 public synchronized void unregister(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900156 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800157 extensions.remove(extension);
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700158 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
159 UiWebSocketServlet.sendToAll(GUI_REMOVED, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800160 }
161
162 @Override
163 public synchronized List<UiExtension> getExtensions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900164 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800165 return ImmutableList.copyOf(extensions);
166 }
167
168 @Override
169 public synchronized UiExtension getViewExtension(String viewId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900170 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800171 return views.get(viewId);
172 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700173
Simon Huntc54cd1b2015-05-11 13:43:44 -0700174 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700175 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700176
177 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700178
179 @Override
180 public Set<String> getNames() {
181 return ImmutableSet.copyOf(sprites.keySet());
182 }
183
184 @Override
185 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700186 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700187 sprites.put(name, spriteData);
188 }
189
190 @Override
191 public JsonNode get(String name) {
192 return sprites.get(name);
193 }
194
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800195}