blob: c29ebe1731ed5a559a79596985c044cb30eff07b [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
48/**
49 * Manages the user interface extensions.
50 */
51@Component(immediate = true)
52@Service
Simon Hunt93735af2015-07-28 12:10:30 -070053public class UiExtensionManager implements UiExtensionService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080054
Simon Hunte05cae42015-07-23 17:35:24 -070055 private static final ClassLoader CL =
56 UiExtensionManager.class.getClassLoader();
57 private static final String CORE = "core";
58
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080059 private final Logger log = LoggerFactory.getLogger(getClass());
60
61 // List of all extensions
62 private final List<UiExtension> extensions = Lists.newArrayList();
63
64 // Map of views to extensions
65 private final Map<String, UiExtension> views = Maps.newHashMap();
66
67 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080068 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080069
Simon Huntc54cd1b2015-05-11 13:43:44 -070070
Thomas Vachuska51f540f2015-05-27 17:26:57 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080073
74 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -070075 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -070076 List<UiView> coreViews = of(
77 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070078 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -070079 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
Thomas Vachuska3ece3732015-09-22 23:58:50 -070080 new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"),
Simon Hunt20e16792015-04-24 14:29:39 -070081 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
82 new UiView(NETWORK, "device", "Devices", "nav_devs"),
83 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070084 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070085 new UiViewHidden("group"),
Jian Li1f544732015-12-30 23:36:37 -080086 new UiViewHidden("meter"),
Simon Hunt20e16792015-04-24 14:29:39 -070087 new UiView(NETWORK, "link", "Links", "nav_links"),
88 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -050089 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
90 //TODO add a new type of icon for tunnel
91 new UiView(NETWORK, "tunnel", "Tunnels", "nav_links")
Simon Hunt20e16792015-04-24 14:29:39 -070092 );
Simon Hunt1002cd82015-04-23 14:44:03 -070093
Simon Hunt4c7edd32015-03-11 10:42:53 -070094 UiMessageHandlerFactory messageHandlerFactory =
95 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070096 new TopologyViewMessageHandler(),
97 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070098 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070099 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700100 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700101 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700102 new GroupViewMessageHandler(),
Jian Li1f544732015-12-30 23:36:37 -0800103 new MeterViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700104 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700105 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700106 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500107 new ClusterViewMessageHandler(),
Thomas Vachuska3ece3732015-09-22 23:58:50 -0700108 new ProcessorViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500109 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700110 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700111
Simon Hunte05cae42015-07-23 17:35:24 -0700112 UiTopoOverlayFactory topoOverlayFactory =
113 () -> ImmutableList.of(
114 new TrafficOverlay()
115 );
116
117 return new UiExtension.Builder(CL, coreViews)
118 .messageHandlerFactory(messageHandlerFactory)
119 .topoOverlayFactory(topoOverlayFactory)
120 .resourcePath(CORE)
121 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800122 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800123
124 @Activate
125 public void activate() {
126 register(core);
127 log.info("Started");
128 }
129
130 @Deactivate
131 public void deactivate() {
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700132 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800133 unregister(core);
134 log.info("Stopped");
135 }
136
137 @Override
138 public synchronized void register(UiExtension extension) {
139 if (!extensions.contains(extension)) {
140 extensions.add(extension);
141 for (UiView view : extension.views()) {
142 views.put(view.id(), extension);
143 }
144 }
145 }
146
147 @Override
148 public synchronized void unregister(UiExtension extension) {
149 extensions.remove(extension);
Simon Huntc54cd1b2015-05-11 13:43:44 -0700150 extension.views().stream()
151 .map(UiView::id).collect(toSet()).forEach(views::remove);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800152 }
153
154 @Override
155 public synchronized List<UiExtension> getExtensions() {
156 return ImmutableList.copyOf(extensions);
157 }
158
159 @Override
160 public synchronized UiExtension getViewExtension(String viewId) {
161 return views.get(viewId);
162 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700163
Simon Huntc54cd1b2015-05-11 13:43:44 -0700164 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700165 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700166
167 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700168
169 @Override
170 public Set<String> getNames() {
171 return ImmutableSet.copyOf(sprites.keySet());
172 }
173
174 @Override
175 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700176 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700177 sprites.put(name, spriteData);
178 }
179
180 @Override
181 public JsonNode get(String name) {
182 return sprites.get(name);
183 }
184
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800185}