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