blob: c601a5f117a9a94f371109e34e16afe6e38fecae [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"));
Simon Hunt4c7edd32015-03-11 10:42:53 -070062 UiMessageHandlerFactory messageHandlerFactory =
63 () -> ImmutableList.of(
Simon Hunt4c7edd32015-03-11 10:42:53 -070064 new TopologyViewMessageHandler()
65 );
Thomas Vachuska3553b302015-03-07 14:49:43 -080066 return new UiExtension(coreViews, messageHandlerFactory, "core",
67 UiExtensionManager.class.getClassLoader());
68 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080069
70 @Activate
71 public void activate() {
72 register(core);
73 log.info("Started");
74 }
75
76 @Deactivate
77 public void deactivate() {
78 unregister(core);
79 log.info("Stopped");
80 }
81
82 @Override
83 public synchronized void register(UiExtension extension) {
84 if (!extensions.contains(extension)) {
85 extensions.add(extension);
86 for (UiView view : extension.views()) {
87 views.put(view.id(), extension);
88 }
89 }
90 }
91
92 @Override
93 public synchronized void unregister(UiExtension extension) {
94 extensions.remove(extension);
95 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
96 }
97
98 @Override
99 public synchronized List<UiExtension> getExtensions() {
100 return ImmutableList.copyOf(extensions);
101 }
102
103 @Override
104 public synchronized UiExtension getViewExtension(String viewId) {
105 return views.get(viewId);
106 }
107}