blob: 080f5e3f9f724bcbe93fc5630568c248922fa447 [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
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.ui.UiExtension;
26import org.onosproject.ui.UiExtensionService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080027import org.onosproject.ui.UiMessageHandlerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080028import org.onosproject.ui.UiView;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.List;
33import java.util.Map;
34
35import static com.google.common.collect.ImmutableList.of;
36import static java.util.stream.Collectors.toSet;
37
38/**
39 * Manages the user interface extensions.
40 */
41@Component(immediate = true)
42@Service
43public class UiExtensionManager implements UiExtensionService {
44
45 private final Logger log = LoggerFactory.getLogger(getClass());
46
47 // List of all extensions
48 private final List<UiExtension> extensions = Lists.newArrayList();
49
50 // Map of views to extensions
51 private final Map<String, UiExtension> views = Maps.newHashMap();
52
53 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080054 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080055
Thomas Vachuska3553b302015-03-07 14:49:43 -080056
57 // Creates core UI extension
58 private static UiExtension createCoreExtension() {
Thomas Vachuskaef646762015-03-17 15:19:03 -070059 List<UiView> coreViews = of(new UiView("topo", "Topology View"),
60 new UiView("device", "Devices"),
Thomas Vachuskae586b792015-03-26 13:59:38 -070061 new UiView("host", "Hosts"),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070062 new UiView("app", "Applications"),
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070063 new UiView("intent", "Intents"),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070064 new UiView("cluster", "Cluster Nodes"),
Thomas Vachuskaef646762015-03-17 15:19:03 -070065 new UiView("sample", "Sample"));
Simon Hunt4c7edd32015-03-11 10:42:53 -070066 UiMessageHandlerFactory messageHandlerFactory =
67 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070068 new TopologyViewMessageHandler(),
69 new DeviceViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070070 new HostViewMessageHandler(),
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070071 new ApplicationViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070072 new IntentViewMessageHandler(),
73 new ClusterViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -070074 );
Thomas Vachuska3553b302015-03-07 14:49:43 -080075 return new UiExtension(coreViews, messageHandlerFactory, "core",
76 UiExtensionManager.class.getClassLoader());
77 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080078
79 @Activate
80 public void activate() {
81 register(core);
82 log.info("Started");
83 }
84
85 @Deactivate
86 public void deactivate() {
87 unregister(core);
88 log.info("Stopped");
89 }
90
91 @Override
92 public synchronized void register(UiExtension extension) {
93 if (!extensions.contains(extension)) {
94 extensions.add(extension);
95 for (UiView view : extension.views()) {
96 views.put(view.id(), extension);
97 }
98 }
99 }
100
101 @Override
102 public synchronized void unregister(UiExtension extension) {
103 extensions.remove(extension);
104 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
105 }
106
107 @Override
108 public synchronized List<UiExtension> getExtensions() {
109 return ImmutableList.copyOf(extensions);
110 }
111
112 @Override
113 public synchronized UiExtension getViewExtension(String viewId) {
114 return views.get(viewId);
115 }
116}