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 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onosproject.ui.UiExtension; |
| 28 | import org.onosproject.ui.UiExtensionService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 29 | import org.onosproject.ui.UiMessageHandlerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 30 | import org.onosproject.ui.UiView; |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiViewHidden; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 37 | import java.util.Set; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 38 | |
| 39 | import static com.google.common.collect.ImmutableList.of; |
| 40 | import static java.util.stream.Collectors.toSet; |
| 41 | |
| 42 | /** |
| 43 | * Manages the user interface extensions. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 47 | public class UiExtensionManager implements UiExtensionService, SpriteService { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 48 | |
| 49 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
| 51 | // List of all extensions |
| 52 | private final List<UiExtension> extensions = Lists.newArrayList(); |
| 53 | |
| 54 | // Map of views to extensions |
| 55 | private final Map<String, UiExtension> views = Maps.newHashMap(); |
| 56 | |
| 57 | // Core views & core extension |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 58 | private final UiExtension core = createCoreExtension(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 59 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 60 | |
| 61 | // Creates core UI extension |
| 62 | private static UiExtension createCoreExtension() { |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 63 | List<UiView> coreViews = of(new UiView("topo", "Topology"), |
Thomas Vachuska | ef64676 | 2015-03-17 15:19:03 -0700 | [diff] [blame] | 64 | new UiView("device", "Devices"), |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 65 | new UiViewHidden("flow"), |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 66 | new UiView("link", "Links"), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 67 | new UiView("host", "Hosts"), |
| 68 | new UiView("intent", "Intents"), |
| 69 | new UiView("app", "Applications"), |
Simon Hunt | 07ee46e | 2015-04-17 09:59:20 -0700 | [diff] [blame] | 70 | new UiView("cluster", "Cluster Nodes")); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 71 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 72 | UiMessageHandlerFactory messageHandlerFactory = |
| 73 | () -> ImmutableList.of( |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 74 | new TopologyViewMessageHandler(), |
| 75 | new DeviceViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 76 | new LinkViewMessageHandler(), |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 77 | new HostViewMessageHandler(), |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 78 | new IntentViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 79 | new ApplicationViewMessageHandler(), |
| 80 | new ClusterViewMessageHandler() |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 81 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 82 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 83 | return new UiExtension(coreViews, messageHandlerFactory, "core", |
| 84 | UiExtensionManager.class.getClassLoader()); |
| 85 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 86 | |
| 87 | @Activate |
| 88 | public void activate() { |
| 89 | register(core); |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
| 95 | unregister(core); |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public synchronized void register(UiExtension extension) { |
| 101 | if (!extensions.contains(extension)) { |
| 102 | extensions.add(extension); |
| 103 | for (UiView view : extension.views()) { |
| 104 | views.put(view.id(), extension); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public synchronized void unregister(UiExtension extension) { |
| 111 | extensions.remove(extension); |
| 112 | extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public synchronized List<UiExtension> getExtensions() { |
| 117 | return ImmutableList.copyOf(extensions); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public synchronized UiExtension getViewExtension(String viewId) { |
| 122 | return views.get(viewId); |
| 123 | } |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 124 | |
| 125 | |
| 126 | // Provisional tracking of sprite definitions |
| 127 | private Map<String, JsonNode> sprites = Maps.newHashMap(); |
| 128 | |
| 129 | @Override |
| 130 | public Set<String> getNames() { |
| 131 | return ImmutableSet.copyOf(sprites.keySet()); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void put(String name, JsonNode spriteData) { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 136 | log.info("Registered sprite definition [{}]", name); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 137 | sprites.put(name, spriteData); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public JsonNode get(String name) { |
| 142 | return sprites.get(name); |
| 143 | } |
| 144 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 145 | } |