blob: 84e999583f49e0e7fca9f07799d40d8d718acb88 [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"),
87 new UiView(NETWORK, "intent", "Intents", "nav_intents")
88 );
Simon Hunt1002cd82015-04-23 14:44:03 -070089
Simon Hunt4c7edd32015-03-11 10:42:53 -070090 UiMessageHandlerFactory messageHandlerFactory =
91 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070092 new TopologyViewMessageHandler(),
93 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070094 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070095 new HostViewMessageHandler(),
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070096 new FlowViewMessageHandler(),
Bri Prebilic Coleac829e42015-05-05 13:42:06 -070097 new PortViewMessageHandler(),
Bri Prebilic Coleff3dc672015-05-06 12:59:38 -070098 new GroupViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070099 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700100 new ApplicationViewMessageHandler(),
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700101 new SettingsViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -0700102 new ClusterViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -0700103 );
Simon Hunt1002cd82015-04-23 14:44:03 -0700104
Simon Hunte05cae42015-07-23 17:35:24 -0700105 UiTopoOverlayFactory topoOverlayFactory =
106 () -> ImmutableList.of(
107 new TrafficOverlay()
108 );
109
110 return new UiExtension.Builder(CL, coreViews)
111 .messageHandlerFactory(messageHandlerFactory)
112 .topoOverlayFactory(topoOverlayFactory)
113 .resourcePath(CORE)
114 .build();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800115 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800116
117 @Activate
118 public void activate() {
119 register(core);
120 log.info("Started");
121 }
122
123 @Deactivate
124 public void deactivate() {
Thomas Vachuska51f540f2015-05-27 17:26:57 -0700125 UiWebSocketServlet.closeAll();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800126 unregister(core);
127 log.info("Stopped");
128 }
129
130 @Override
131 public synchronized void register(UiExtension extension) {
132 if (!extensions.contains(extension)) {
133 extensions.add(extension);
134 for (UiView view : extension.views()) {
135 views.put(view.id(), extension);
136 }
137 }
138 }
139
140 @Override
141 public synchronized void unregister(UiExtension extension) {
142 extensions.remove(extension);
Simon Huntc54cd1b2015-05-11 13:43:44 -0700143 extension.views().stream()
144 .map(UiView::id).collect(toSet()).forEach(views::remove);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800145 }
146
147 @Override
148 public synchronized List<UiExtension> getExtensions() {
149 return ImmutableList.copyOf(extensions);
150 }
151
152 @Override
153 public synchronized UiExtension getViewExtension(String viewId) {
154 return views.get(viewId);
155 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700156
Simon Huntc54cd1b2015-05-11 13:43:44 -0700157 // =====================================================================
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700158 // Provisional tracking of sprite definitions
Simon Huntc54cd1b2015-05-11 13:43:44 -0700159
160 private final Map<String, JsonNode> sprites = Maps.newHashMap();
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700161
162 @Override
163 public Set<String> getNames() {
164 return ImmutableSet.copyOf(sprites.keySet());
165 }
166
167 @Override
168 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700169 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700170 sprites.put(name, spriteData);
171 }
172
173 @Override
174 public JsonNode get(String name) {
175 return sprites.get(name);
176 }
177
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800178}