blob: 2bd0bb61edcc36af5d3870cf5fbc8ca7af15b568 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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"),
80 new UiView(NETWORK, "topo", "Topology", "nav_topo"),
81 new UiView(NETWORK, "device", "Devices", "nav_devs"),
82 new UiViewHidden("flow"),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070083 new UiViewHidden("port"),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070084 new UiViewHidden("group"),
Simon Hunt20e16792015-04-24 14:29:39 -070085 new UiView(NETWORK, "link", "Links", "nav_links"),
86 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
chengfanb466a7e2015-08-21 09:59:29 -050087 new UiView(NETWORK, "intent", "Intents", "nav_intents"),
88 //TODO add a new type of icon for tunnel
89 new UiView(NETWORK, "tunnel", "Tunnels", "nav_links")
Simon Hunt20e16792015-04-24 14:29:39 -070090 );
Simon Hunt1002cd82015-04-23 14:44:03 -070091
Simon Hunt4c7edd32015-03-11 10:42:53 -070092 UiMessageHandlerFactory messageHandlerFactory =
93 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070094 new TopologyViewMessageHandler(),
95 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070096 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070097 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070098 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070099 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -0700100 new GroupViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700101 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700102 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700103 new SettingsViewMessageHandler(),
chengfanb466a7e2015-08-21 09:59:29 -0500104 new ClusterViewMessageHandler(),
105 new TunnelViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700106 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700107
Simon Hunte05cae42015-07-23 17:35:24 -0700108 UiTopoOverlayFactory topoOverlayFactory =
109 () -> ImmutableList.of(
110 new TrafficOverlay()
111 );
112
113 return new UiExtension.Builder(CL, coreViews)
114 .messageHandlerFactory(messageHandlerFactory)
115 .topoOverlayFactory(topoOverlayFactory)
116 .resourcePath(CORE)
117 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800118 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800119
120 @Activate
121 public void activate() {
122 register(core);
123 log.info("Started");
124 }
125
126 @Deactivate
127 public void deactivate() {
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700128 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800129 unregister(core);
130 log.info("Stopped");
131 }
132
133 @Override
134 public synchronized void register(UiExtension extension) {
135 if (!extensions.contains(extension)) {
136 extensions.add(extension);
137 for (UiView view : extension.views()) {
138 views.put(view.id(), extension);
139 }
140 }
141 }
142
143 @Override
144 public synchronized void unregister(UiExtension extension) {
145 extensions.remove(extension);
Simon Huntc54cd1b2015-05-11 13:43:44 -0700146 extension.views().stream()
147 .map(UiView::id).collect(toSet()).forEach(views::remove);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800148 }
149
150 @Override
151 public synchronized List<UiExtension> getExtensions() {
152 return ImmutableList.copyOf(extensions);
153 }
154
155 @Override
156 public synchronized UiExtension getViewExtension(String viewId) {
157 return views.get(viewId);
158 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700159
Simon Huntc54cd1b2015-05-11 13:43:44 -0700160 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700161 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700162
163 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700164
165 @Override
166 public Set<String> getNames() {
167 return ImmutableSet.copyOf(sprites.keySet());
168 }
169
170 @Override
171 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700172 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700173 sprites.put(name, spriteData);
174 }
175
176 @Override
177 public JsonNode get(String name) {
178 return sprites.get(name);
179 }
180
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800181}