blob: 9bfa3e3b0efffaba107fb8963e9d554f631b8d79 [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
Simon Hunte05cae42015-07-23 17:35:24 -070059 private static final ClassLoader CL =
60 UiExtensionManager.class.getClassLoader();
61 private static final String CORE = "core";
62
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063 private final Logger log = LoggerFactory.getLogger(getClass());
64
65 // List of all extensions
66 private final List<UiExtension> extensions = Lists.newArrayList();
67
68 // Map of views to extensions
69 private final Map<String, UiExtension> views = Maps.newHashMap();
70
71 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080072 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080073
Simon Huntc54cd1b2015-05-11 13:43:44 -070074
Thomas Vachuska51f540f2015-05-27 17:26:57 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080077
78 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -070079 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -070080 List<UiView> coreViews = of(
81 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070082 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -070083 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
Thomas Vachuska3ece3732015-09-22 23:58:50 -070084 new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"),
Simon Hunt20e16792015-04-24 14:29:39 -070085 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
86 new UiView(NETWORK, "device", "Devices", "nav_devs"),
87 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070088 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070089 new UiViewHidden("group"),
Jian Li1f544732015-12-30 23:36:37 -080090 new UiViewHidden("meter"),
Simon Hunt20e16792015-04-24 14:29:39 -070091 new UiView(NETWORK, "link", "Links", "nav_links"),
92 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -050093 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
94 //TODO add a new type of icon for tunnel
95 new UiView(NETWORK, "tunnel", "Tunnels", "nav_links")
Simon Hunt20e16792015-04-24 14:29:39 -070096 );
Simon Hunt1002cd82015-04-23 14:44:03 -070097
Simon Hunt4c7edd32015-03-11 10:42:53 -070098 UiMessageHandlerFactory messageHandlerFactory =
99 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -0700100 new TopologyViewMessageHandler(),
101 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700102 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700103 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700104 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700105 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700106 new GroupViewMessageHandler(),
Jian Li1f544732015-12-30 23:36:37 -0800107 new MeterViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700108 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700109 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700110 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500111 new ClusterViewMessageHandler(),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700112 new ProcessorViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500113 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700114 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700115
Simon Hunte05cae42015-07-23 17:35:24 -0700116 UiTopoOverlayFactory topoOverlayFactory =
117 () -> ImmutableList.of(
118 new TrafficOverlay()
119 );
120
121 return new UiExtension.Builder(CL, coreViews)
122 .messageHandlerFactory(messageHandlerFactory)
123 .topoOverlayFactory(topoOverlayFactory)
124 .resourcePath(CORE)
125 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800126 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800127
128 @Activate
129 public void activate() {
130 register(core);
131 log.info("Started");
132 }
133
134 @Deactivate
135 public void deactivate() {
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700136 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800137 unregister(core);
138 log.info("Stopped");
139 }
140
141 @Override
142 public synchronized void register(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900143 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800144 if (!extensions.contains(extension)) {
145 extensions.add(extension);
146 for (UiView view : extension.views()) {
147 views.put(view.id(), extension);
148 }
149 }
150 }
151
152 @Override
153 public synchronized void unregister(UiExtension extension) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900154 checkPermission(UI_WRITE);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800155 extensions.remove(extension);
Simon Huntc54cd1b2015-05-11 13:43:44 -0700156 extension.views().stream()
157 .map(UiView::id).collect(toSet()).forEach(views::remove);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800158 }
159
160 @Override
161 public synchronized List<UiExtension> getExtensions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900162 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800163 return ImmutableList.copyOf(extensions);
164 }
165
166 @Override
167 public synchronized UiExtension getViewExtension(String viewId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900168 checkPermission(UI_READ);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800169 return views.get(viewId);
170 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700171
Simon Huntc54cd1b2015-05-11 13:43:44 -0700172 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700173 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700174
175 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700176
177 @Override
178 public Set<String> getNames() {
179 return ImmutableSet.copyOf(sprites.keySet());
180 }
181
182 @Override
183 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700184 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700185 sprites.put(name, spriteData);
186 }
187
188 @Override
189 public JsonNode get(String name) {
190 return sprites.get(name);
191 }
192
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800193}