blob: 3604919c7a6265d43c0472da18eba750b0d6178a [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;
27import org.onosproject.ui.UiView;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.util.List;
32import java.util.Map;
33
34import static com.google.common.collect.ImmutableList.of;
35import static java.util.stream.Collectors.toSet;
36
37/**
38 * Manages the user interface extensions.
39 */
40@Component(immediate = true)
41@Service
42public class UiExtensionManager implements UiExtensionService {
43
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 // List of all extensions
47 private final List<UiExtension> extensions = Lists.newArrayList();
48
49 // Map of views to extensions
50 private final Map<String, UiExtension> views = Maps.newHashMap();
51
52 // Core views & core extension
53 private final List<UiView> coreViews = of(new UiView("sample", "Sample"),
54 new UiView("topo", "Topology View"),
55 new UiView("device", "Devices"));
56
Thomas Vachuskaa0509892015-02-21 22:18:41 -080057 private final UiExtension core = new UiExtension(coreViews, "core",
58 getClass().getClassLoader());
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080059
60 @Activate
61 public void activate() {
62 register(core);
63 log.info("Started");
64 }
65
66 @Deactivate
67 public void deactivate() {
68 unregister(core);
69 log.info("Stopped");
70 }
71
72 @Override
73 public synchronized void register(UiExtension extension) {
74 if (!extensions.contains(extension)) {
75 extensions.add(extension);
76 for (UiView view : extension.views()) {
77 views.put(view.id(), extension);
78 }
79 }
80 }
81
82 @Override
83 public synchronized void unregister(UiExtension extension) {
84 extensions.remove(extension);
85 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
86 }
87
88 @Override
89 public synchronized List<UiExtension> getExtensions() {
90 return ImmutableList.copyOf(extensions);
91 }
92
93 @Override
94 public synchronized UiExtension getViewExtension(String viewId) {
95 return views.get(viewId);
96 }
97}