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; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 27 | import org.onosproject.ui.UiMessageHandlerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 28 | import org.onosproject.ui.UiView; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | import static com.google.common.collect.ImmutableList.of; |
| 36 | import static java.util.stream.Collectors.toSet; |
| 37 | |
| 38 | /** |
| 39 | * Manages the user interface extensions. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public 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 Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 54 | private final UiExtension core = createCoreExtension(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 55 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 56 | |
| 57 | // Creates core UI extension |
| 58 | private static UiExtension createCoreExtension() { |
Thomas Vachuska | ef64676 | 2015-03-17 15:19:03 -0700 | [diff] [blame] | 59 | List<UiView> coreViews = of(new UiView("topo", "Topology View"), |
| 60 | new UiView("device", "Devices"), |
| 61 | new UiView("sample", "Sample")); |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 62 | UiMessageHandlerFactory messageHandlerFactory = |
| 63 | () -> ImmutableList.of( |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 64 | new TopologyViewMessageHandler() |
| 65 | ); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 66 | return new UiExtension(coreViews, messageHandlerFactory, "core", |
| 67 | UiExtensionManager.class.getClassLoader()); |
| 68 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 69 | |
| 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 | } |