blob: 5706734aa6cf7c320411904700db60f56a32a44a [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;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080033import org.onosproject.ui.UiView;
Simon Hunt1002cd82015-04-23 14:44:03 -070034import org.onosproject.ui.UiViewHidden;
Simon Huntc54cd1b2015-05-11 13:43:44 -070035import org.onosproject.ui.impl.topo.OverlayService;
36import org.onosproject.ui.impl.topo.overlay.SummaryGenerator;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import java.util.List;
41import java.util.Map;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070042import java.util.Set;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080043
44import static com.google.common.collect.ImmutableList.of;
45import static java.util.stream.Collectors.toSet;
Thomas Vachuska8b91f4f2015-04-23 17:55:36 -070046import static org.onosproject.ui.UiView.Category.NETWORK;
47import static org.onosproject.ui.UiView.Category.PLATFORM;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080048
49/**
50 * Manages the user interface extensions.
51 */
52@Component(immediate = true)
53@Service
Simon Huntc54cd1b2015-05-11 13:43:44 -070054public class UiExtensionManager
55 implements UiExtensionService, SpriteService, OverlayService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080056
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
59 // List of all extensions
60 private final List<UiExtension> extensions = Lists.newArrayList();
61
62 // Map of views to extensions
63 private final Map<String, UiExtension> views = Maps.newHashMap();
64
65 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080066 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080067
Simon Huntc54cd1b2015-05-11 13:43:44 -070068 // Topology Message Handler
69 private final AltTopoViewMessageHandler topoHandler =
70 new AltTopoViewMessageHandler();
71
Thomas Vachuska51f540f2015-05-27 17:26:57 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected MastershipService mastershipService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080074
75 // Creates core UI extension
Simon Huntc54cd1b2015-05-11 13:43:44 -070076 private UiExtension createCoreExtension() {
Simon Hunt20e16792015-04-24 14:29:39 -070077 List<UiView> coreViews = of(
78 new UiView(PLATFORM, "app", "Applications", "nav_apps"),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070079 new UiView(PLATFORM, "settings", "Settings", "nav_settings"),
Simon Hunt20e16792015-04-24 14:29:39 -070080 new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"),
81 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"),
Simon Hunt20e16792015-04-24 14:29:39 -070086 new UiView(NETWORK, "link", "Links", "nav_links"),
87 new UiView(NETWORK, "host", "Hosts", "nav_hosts"),
88 new UiView(NETWORK, "intent", "Intents", "nav_intents")
89 );
Simon Hunt1002cd82015-04-23 14:44:03 -070090
Simon Hunt4c7edd32015-03-11 10:42:53 -070091 UiMessageHandlerFactory messageHandlerFactory =
92 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070093 new TopologyViewMessageHandler(),
Simon Huntc54cd1b2015-05-11 13:43:44 -070094// topoHandler,
Thomas Vachuskae586b792015-03-26 13:59:38 -070095 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(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700104 new ClusterViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700105 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700106
Thomas Vachuska3553b302015-03-07 14:49:43 -0800107 return new UiExtension(coreViews, messageHandlerFactory, "core",
108 UiExtensionManager.class.getClassLoader());
109 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800110
111 @Activate
112 public void activate() {
113 register(core);
114 log.info("Started");
115 }
116
117 @Deactivate
118 public void deactivate() {
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700119 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800120 unregister(core);
121 log.info("Stopped");
122 }
123
124 @Override
125 public synchronized void register(UiExtension extension) {
126 if (!extensions.contains(extension)) {
127 extensions.add(extension);
128 for (UiView view : extension.views()) {
129 views.put(view.id(), extension);
130 }
131 }
132 }
133
134 @Override
135 public synchronized void unregister(UiExtension extension) {
136 extensions.remove(extension);
Simon Huntc54cd1b2015-05-11 13:43:44 -0700137 extension.views().stream()
138 .map(UiView::id).collect(toSet()).forEach(views::remove);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800139 }
140
141 @Override
142 public synchronized List<UiExtension> getExtensions() {
143 return ImmutableList.copyOf(extensions);
144 }
145
146 @Override
147 public synchronized UiExtension getViewExtension(String viewId) {
148 return views.get(viewId);
149 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700150
Simon Huntc54cd1b2015-05-11 13:43:44 -0700151 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700152 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700153
154 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700155
156 @Override
157 public Set<String> getNames() {
158 return ImmutableSet.copyOf(sprites.keySet());
159 }
160
161 @Override
162 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700163 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700164 sprites.put(name, spriteData);
165 }
166
167 @Override
168 public JsonNode get(String name) {
169 return sprites.get(name);
170 }
171
Simon Huntc54cd1b2015-05-11 13:43:44 -0700172
173 // =====================================================================
174 // Topology Overlay API -- pass through to topology message handler
175
176 // NOTE: while WIP, comment out calls to topoHandler (for checked in code)
177 @Override
178 public void addSummaryGenerator(String overlayId, SummaryGenerator generator) {
179 topoHandler.addSummaryGenerator(overlayId, generator);
180 }
181
182 @Override
183 public void removeSummaryGenerator(String overlayId) {
184 topoHandler.removeSummaryGenerator(overlayId);
185 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800186}