blob: f0541f672186310c7e52d8ea07f33f8e0205cf64 [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"),
Thomas Vachuskaef646762015-03-17 15:19:03 -070063 new UiView("sample", "Sample"));
Simon Hunt4c7edd32015-03-11 10:42:53 -070064 UiMessageHandlerFactory messageHandlerFactory =
65 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070066 new TopologyViewMessageHandler(),
67 new DeviceViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070068 new HostViewMessageHandler(),
69 new ApplicationViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -070070 );
Thomas Vachuska3553b302015-03-07 14:49:43 -080071 return new UiExtension(coreViews, messageHandlerFactory, "core",
72 UiExtensionManager.class.getClassLoader());
73 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080074
75 @Activate
76 public void activate() {
77 register(core);
78 log.info("Started");
79 }
80
81 @Deactivate
82 public void deactivate() {
83 unregister(core);
84 log.info("Stopped");
85 }
86
87 @Override
88 public synchronized void register(UiExtension extension) {
89 if (!extensions.contains(extension)) {
90 extensions.add(extension);
91 for (UiView view : extension.views()) {
92 views.put(view.id(), extension);
93 }
94 }
95 }
96
97 @Override
98 public synchronized void unregister(UiExtension extension) {
99 extensions.remove(extension);
100 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
101 }
102
103 @Override
104 public synchronized List<UiExtension> getExtensions() {
105 return ImmutableList.copyOf(extensions);
106 }
107
108 @Override
109 public synchronized UiExtension getViewExtension(String viewId) {
110 return views.get(viewId);
111 }
112}