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; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 29 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 30 | import org.onosproject.ui.UiExtension; |
| 31 | import org.onosproject.ui.UiExtensionService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 32 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiTopoOverlayFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 34 | import org.onosproject.ui.UiView; |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.UiViewHidden; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 41 | import java.util.Set; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 42 | |
| 43 | import static com.google.common.collect.ImmutableList.of; |
| 44 | import static java.util.stream.Collectors.toSet; |
Thomas Vachuska | 8b91f4f | 2015-04-23 17:55:36 -0700 | [diff] [blame] | 45 | import static org.onosproject.ui.UiView.Category.NETWORK; |
| 46 | import static org.onosproject.ui.UiView.Category.PLATFORM; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Manages the user interface extensions. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | @Service |
Simon Hunt | 93735af | 2015-07-28 12:10:30 -0700 | [diff] [blame] | 53 | public class UiExtensionManager implements UiExtensionService, SpriteService { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 54 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 55 | private static final ClassLoader CL = |
| 56 | UiExtensionManager.class.getClassLoader(); |
| 57 | private static final String CORE = "core"; |
| 58 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
| 61 | // List of all extensions |
| 62 | private final List<UiExtension> extensions = Lists.newArrayList(); |
| 63 | |
| 64 | // Map of views to extensions |
| 65 | private final Map<String, UiExtension> views = Maps.newHashMap(); |
| 66 | |
| 67 | // Core views & core extension |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 68 | private final UiExtension core = createCoreExtension(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 69 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 70 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected MastershipService mastershipService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 73 | |
| 74 | // Creates core UI extension |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 75 | private UiExtension createCoreExtension() { |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 76 | List<UiView> coreViews = of( |
| 77 | new UiView(PLATFORM, "app", "Applications", "nav_apps"), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 78 | new UiView(PLATFORM, "settings", "Settings", "nav_settings"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 79 | new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 80 | new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 81 | new UiView(NETWORK, "topo", "Topology", "nav_topo"), |
| 82 | new UiView(NETWORK, "device", "Devices", "nav_devs"), |
| 83 | new UiViewHidden("flow"), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 84 | new UiViewHidden("port"), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 85 | new UiViewHidden("group"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 86 | new UiView(NETWORK, "link", "Links", "nav_links"), |
| 87 | new UiView(NETWORK, "host", "Hosts", "nav_hosts"), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 88 | new UiView(NETWORK, "intent", "Intents", "nav_intents"), |
| 89 | //TODO add a new type of icon for tunnel |
| 90 | new UiView(NETWORK, "tunnel", "Tunnels", "nav_links") |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 91 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 92 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 93 | UiMessageHandlerFactory messageHandlerFactory = |
| 94 | () -> ImmutableList.of( |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 95 | new TopologyViewMessageHandler(), |
| 96 | new DeviceViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 97 | new LinkViewMessageHandler(), |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 98 | new HostViewMessageHandler(), |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 99 | new FlowViewMessageHandler(), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 100 | new PortViewMessageHandler(), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 101 | new GroupViewMessageHandler(), |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 102 | new IntentViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 103 | new ApplicationViewMessageHandler(), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 104 | new SettingsViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 105 | new ClusterViewMessageHandler(), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 106 | new ProcessorViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 107 | new TunnelViewMessageHandler() |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 108 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 109 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 110 | UiTopoOverlayFactory topoOverlayFactory = |
| 111 | () -> ImmutableList.of( |
| 112 | new TrafficOverlay() |
| 113 | ); |
| 114 | |
| 115 | return new UiExtension.Builder(CL, coreViews) |
| 116 | .messageHandlerFactory(messageHandlerFactory) |
| 117 | .topoOverlayFactory(topoOverlayFactory) |
| 118 | .resourcePath(CORE) |
| 119 | .build(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 120 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 121 | |
| 122 | @Activate |
| 123 | public void activate() { |
| 124 | register(core); |
| 125 | log.info("Started"); |
| 126 | } |
| 127 | |
| 128 | @Deactivate |
| 129 | public void deactivate() { |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 130 | UiWebSocketServlet.closeAll(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 131 | unregister(core); |
| 132 | log.info("Stopped"); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public synchronized void register(UiExtension extension) { |
| 137 | if (!extensions.contains(extension)) { |
| 138 | extensions.add(extension); |
| 139 | for (UiView view : extension.views()) { |
| 140 | views.put(view.id(), extension); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public synchronized void unregister(UiExtension extension) { |
| 147 | extensions.remove(extension); |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 148 | extension.views().stream() |
| 149 | .map(UiView::id).collect(toSet()).forEach(views::remove); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public synchronized List<UiExtension> getExtensions() { |
| 154 | return ImmutableList.copyOf(extensions); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public synchronized UiExtension getViewExtension(String viewId) { |
| 159 | return views.get(viewId); |
| 160 | } |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 161 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 162 | // ===================================================================== |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 163 | // Provisional tracking of sprite definitions |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 164 | |
| 165 | private final Map<String, JsonNode> sprites = Maps.newHashMap(); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 166 | |
| 167 | @Override |
| 168 | public Set<String> getNames() { |
| 169 | return ImmutableSet.copyOf(sprites.keySet()); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void put(String name, JsonNode spriteData) { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 174 | log.info("Registered sprite definition [{}]", name); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 175 | sprites.put(name, spriteData); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public JsonNode get(String name) { |
| 180 | return sprites.get(name); |
| 181 | } |
| 182 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 183 | } |