blob: 3b9156d2c7f583107f1447f1740ed6f094b73fc0 [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() {
59 List<UiView> coreViews = of(new UiView("sample", "Sample"),
60 new UiView("topo", "Topology View"),
61 new UiView("device", "Devices"));
62 UiMessageHandlerFactory messageHandlerFactory = null;
63 return new UiExtension(coreViews, messageHandlerFactory, "core",
64 UiExtensionManager.class.getClassLoader());
65 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080066
67 @Activate
68 public void activate() {
69 register(core);
70 log.info("Started");
71 }
72
73 @Deactivate
74 public void deactivate() {
75 unregister(core);
76 log.info("Stopped");
77 }
78
79 @Override
80 public synchronized void register(UiExtension extension) {
81 if (!extensions.contains(extension)) {
82 extensions.add(extension);
83 for (UiView view : extension.views()) {
84 views.put(view.id(), extension);
85 }
86 }
87 }
88
89 @Override
90 public synchronized void unregister(UiExtension extension) {
91 extensions.remove(extension);
92 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
93 }
94
95 @Override
96 public synchronized List<UiExtension> getExtensions() {
97 return ImmutableList.copyOf(extensions);
98 }
99
100 @Override
101 public synchronized UiExtension getViewExtension(String viewId) {
102 return views.get(viewId);
103 }
104}