blob: 6ca1e2173af06d2fe3664cac6a2d51e23a5b9bf2 [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
57 private final UiExtension core = new UiExtension(coreViews, getClass().getClassLoader());
58
59 @Activate
60 public void activate() {
61 register(core);
62 log.info("Started");
63 }
64
65 @Deactivate
66 public void deactivate() {
67 unregister(core);
68 log.info("Stopped");
69 }
70
71 @Override
72 public synchronized void register(UiExtension extension) {
73 if (!extensions.contains(extension)) {
74 extensions.add(extension);
75 for (UiView view : extension.views()) {
76 views.put(view.id(), extension);
77 }
78 }
79 }
80
81 @Override
82 public synchronized void unregister(UiExtension extension) {
83 extensions.remove(extension);
84 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
85 }
86
87 @Override
88 public synchronized List<UiExtension> getExtensions() {
89 return ImmutableList.copyOf(extensions);
90 }
91
92 @Override
93 public synchronized UiExtension getViewExtension(String viewId) {
94 return views.get(viewId);
95 }
96}