Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.ui.UiExtension; |
| 26 | import org.onosproject.ui.UiExtensionService; |
| 27 | import org.onosproject.ui.UiView; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | |
| 34 | import static com.google.common.collect.ImmutableList.of; |
| 35 | import static java.util.stream.Collectors.toSet; |
| 36 | |
| 37 | /** |
| 38 | * Manages the user interface extensions. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service |
| 42 | public 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 Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 57 | private final UiExtension core = new UiExtension(coreViews, "core", |
| 58 | getClass().getClassLoader()); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 59 | |
| 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 | } |