Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.BooleanNode; |
| 22 | import com.fasterxml.jackson.databind.node.DoubleNode; |
| 23 | import com.fasterxml.jackson.databind.node.IntNode; |
| 24 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 25 | import com.fasterxml.jackson.databind.node.LongNode; |
| 26 | import com.fasterxml.jackson.databind.node.NullNode; |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | import com.fasterxml.jackson.databind.node.ShortNode; |
| 29 | import com.fasterxml.jackson.databind.node.TextNode; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableMap; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 33 | import com.google.common.collect.Lists; |
| 34 | import com.google.common.collect.Maps; |
| 35 | import org.apache.felix.scr.annotations.Activate; |
| 36 | import org.apache.felix.scr.annotations.Component; |
| 37 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 38 | import org.apache.felix.scr.annotations.Reference; |
| 39 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 40 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 41 | import org.onlab.util.Tools; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 42 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 43 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 44 | import org.onosproject.store.service.ConsistentMap; |
| 45 | import org.onosproject.store.service.MapEvent; |
| 46 | import org.onosproject.store.service.MapEventListener; |
| 47 | import org.onosproject.store.service.Serializer; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 48 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 49 | import org.onosproject.ui.UiExtension; |
| 50 | import org.onosproject.ui.UiExtensionService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 51 | import org.onosproject.ui.UiMessageHandlerFactory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 52 | import org.onosproject.ui.UiPreferencesService; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 53 | import org.onosproject.ui.UiTopoMap; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 54 | import org.onosproject.ui.UiTopoMapFactory; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 55 | import org.onosproject.ui.UiTopoOverlayFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 56 | import org.onosproject.ui.UiView; |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 57 | import org.onosproject.ui.UiViewHidden; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 58 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 62 | import java.util.LinkedHashMap; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 63 | import java.util.List; |
| 64 | import java.util.Map; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 65 | import java.util.Set; |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 66 | import java.util.concurrent.ExecutorService; |
| 67 | import java.util.concurrent.Executors; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 68 | |
| 69 | import static com.google.common.collect.ImmutableList.of; |
| 70 | import static java.util.stream.Collectors.toSet; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 71 | import static org.onosproject.security.AppGuard.checkPermission; |
| 72 | import static org.onosproject.security.AppPermission.Type.UI_READ; |
| 73 | import static org.onosproject.security.AppPermission.Type.UI_WRITE; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 74 | import static org.onosproject.ui.UiView.Category.NETWORK; |
| 75 | import static org.onosproject.ui.UiView.Category.PLATFORM; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 76 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Manages the user interface extensions. |
| 79 | */ |
| 80 | @Component(immediate = true) |
| 81 | @Service |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 82 | public class UiExtensionManager |
| 83 | implements UiExtensionService, UiPreferencesService, SpriteService { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 84 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 85 | private static final ClassLoader CL = UiExtensionManager.class.getClassLoader(); |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 86 | |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 87 | private static final String ONOS_USER_PREFERENCES = "onos-ui-user-preferences"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 88 | private static final String CORE = "core"; |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 89 | private static final String GUI_ADDED = "guiAdded"; |
| 90 | private static final String GUI_REMOVED = "guiRemoved"; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 91 | private static final String UPDATE_PREFS = "updatePrefs"; |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 92 | private static final String SLASH = "/"; |
| 93 | |
| 94 | private static final int IDX_USER = 0; |
| 95 | private static final int IDX_KEY = 1; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 96 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 97 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 98 | |
| 99 | // List of all extensions |
| 100 | private final List<UiExtension> extensions = Lists.newArrayList(); |
| 101 | |
| 102 | // Map of views to extensions |
| 103 | private final Map<String, UiExtension> views = Maps.newHashMap(); |
| 104 | |
| 105 | // Core views & core extension |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 106 | private final UiExtension core = createCoreExtension(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 107 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected MastershipService mastershipService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 110 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected StorageService storageService; |
| 113 | |
| 114 | // User preferences |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 115 | private ConsistentMap<String, ObjectNode> prefsConsistentMap; |
| 116 | private Map<String, ObjectNode> prefs; |
| 117 | private final MapEventListener<String, ObjectNode> prefsListener = |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 118 | new InternalPrefsListener(); |
| 119 | |
| 120 | private final ObjectMapper mapper = new ObjectMapper(); |
| 121 | |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 122 | private final ExecutorService eventHandlingExecutor = |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 123 | Executors.newSingleThreadExecutor( |
| 124 | Tools.groupedThreads("onos/ui-ext-manager", "event-handler", log)); |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 125 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 126 | // Creates core UI extension |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 127 | private UiExtension createCoreExtension() { |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 128 | List<UiView> coreViews = of( |
| 129 | new UiView(PLATFORM, "app", "Applications", "nav_apps"), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 130 | new UiView(PLATFORM, "settings", "Settings", "nav_settings"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 131 | new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 132 | new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 133 | new UiView(NETWORK, "topo", "Topology", "nav_topo"), |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 134 | |
| 135 | // FIXME: leave commented out for now, while still under development |
Simon Hunt | 1bee529 | 2016-10-14 11:02:33 -0700 | [diff] [blame] | 136 | // (remember to also comment out inclusions in index.html) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 137 | // new UiView(NETWORK, "topo2", "New-Topo"), |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 138 | // new UiView(NETWORK, "topoX", "Topo-X"), |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 139 | |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 140 | new UiView(NETWORK, "device", "Devices", "nav_devs"), |
| 141 | new UiViewHidden("flow"), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 142 | new UiViewHidden("port"), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 143 | new UiViewHidden("group"), |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 144 | new UiViewHidden("meter"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 145 | new UiView(NETWORK, "link", "Links", "nav_links"), |
| 146 | new UiView(NETWORK, "host", "Hosts", "nav_hosts"), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 147 | new UiView(NETWORK, "intent", "Intents", "nav_intents"), |
Simon Hunt | 986b92f | 2016-06-03 15:46:59 -0700 | [diff] [blame] | 148 | new UiView(NETWORK, "tunnel", "Tunnels", "nav_tunnels") |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 149 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 150 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 151 | UiMessageHandlerFactory messageHandlerFactory = |
| 152 | () -> ImmutableList.of( |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 153 | new UserPreferencesMessageHandler(), |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 154 | new TopologyViewMessageHandler(), |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 155 | new Topo2ViewMessageHandler(), |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 156 | new MapSelectorMessageHandler(), |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 157 | new DeviceViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 158 | new LinkViewMessageHandler(), |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 159 | new HostViewMessageHandler(), |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 160 | new FlowViewMessageHandler(), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 161 | new PortViewMessageHandler(), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 162 | new GroupViewMessageHandler(), |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 163 | new MeterViewMessageHandler(), |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 164 | new IntentViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 165 | new ApplicationViewMessageHandler(), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 166 | new SettingsViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 167 | new ClusterViewMessageHandler(), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 168 | new ProcessorViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 169 | new TunnelViewMessageHandler() |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 170 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 171 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 172 | UiTopoOverlayFactory topoOverlayFactory = |
| 173 | () -> ImmutableList.of( |
| 174 | new TrafficOverlay() |
| 175 | ); |
| 176 | |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 177 | UiTopoMapFactory topoMapFactory = |
| 178 | () -> ImmutableList.of( |
| 179 | new UiTopoMap("australia", "Australia", "*australia", 1.0), |
| 180 | new UiTopoMap("americas", "North, Central and South America", "*americas", 0.7), |
| 181 | new UiTopoMap("n_america", "North America", "*n_america", 0.9), |
| 182 | new UiTopoMap("s_america", "South America", "*s_america", 0.9), |
Thomas Vachuska | cc0b7d6 | 2016-07-12 14:03:11 -0700 | [diff] [blame] | 183 | new UiTopoMap("usa", "United States", "*continental_us", 1.3), |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 184 | new UiTopoMap("bayareaGEO", "Bay Area, California", "*bayarea", 1.0), |
Thomas Vachuska | cc0b7d6 | 2016-07-12 14:03:11 -0700 | [diff] [blame] | 185 | new UiTopoMap("europe", "Europe", "*europe", 10.0), |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 186 | new UiTopoMap("italy", "Italy", "*italy", 0.8), |
Thomas Vachuska | cc0b7d6 | 2016-07-12 14:03:11 -0700 | [diff] [blame] | 187 | new UiTopoMap("uk", "United Kingdom and Ireland", "*uk", 2.0), |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 188 | new UiTopoMap("japan", "Japan", "*japan", 0.8), |
| 189 | new UiTopoMap("s_korea", "South Korea", "*s_korea", 0.75), |
| 190 | new UiTopoMap("taiwan", "Taiwan", "*taiwan", 0.7), |
| 191 | new UiTopoMap("africa", "Africa", "*africa", 0.7), |
| 192 | new UiTopoMap("oceania", "Oceania", "*oceania", 0.7), |
| 193 | new UiTopoMap("asia", "Asia", "*asia", 0.7) |
| 194 | ); |
| 195 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 196 | return new UiExtension.Builder(CL, coreViews) |
| 197 | .messageHandlerFactory(messageHandlerFactory) |
| 198 | .topoOverlayFactory(topoOverlayFactory) |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 199 | .topoMapFactory(topoMapFactory) |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 200 | .resourcePath(CORE) |
| 201 | .build(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 202 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 203 | |
| 204 | @Activate |
| 205 | public void activate() { |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 206 | Serializer serializer = Serializer.using(KryoNamespaces.API, |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 207 | ObjectNode.class, ArrayNode.class, |
| 208 | JsonNodeFactory.class, LinkedHashMap.class, |
| 209 | TextNode.class, BooleanNode.class, |
| 210 | LongNode.class, DoubleNode.class, ShortNode.class, |
| 211 | IntNode.class, NullNode.class); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 212 | |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 213 | prefsConsistentMap = storageService.<String, ObjectNode>consistentMapBuilder() |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 214 | .withName(ONOS_USER_PREFERENCES) |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 215 | .withSerializer(serializer) |
| 216 | .withRelaxedReadConsistency() |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 217 | .build(); |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 218 | prefsConsistentMap.addListener(prefsListener); |
| 219 | prefs = prefsConsistentMap.asJavaMap(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 220 | register(core); |
| 221 | log.info("Started"); |
| 222 | } |
| 223 | |
| 224 | @Deactivate |
| 225 | public void deactivate() { |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 226 | prefsConsistentMap.removeListener(prefsListener); |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 227 | eventHandlingExecutor.shutdown(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 228 | UiWebSocketServlet.closeAll(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 229 | unregister(core); |
| 230 | log.info("Stopped"); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public synchronized void register(UiExtension extension) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 235 | checkPermission(UI_WRITE); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 236 | if (!extensions.contains(extension)) { |
| 237 | extensions.add(extension); |
| 238 | for (UiView view : extension.views()) { |
| 239 | views.put(view.id(), extension); |
| 240 | } |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 241 | UiWebSocketServlet.sendToAll(GUI_ADDED, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public synchronized void unregister(UiExtension extension) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 247 | checkPermission(UI_WRITE); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 248 | extensions.remove(extension); |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 249 | extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove); |
| 250 | UiWebSocketServlet.sendToAll(GUI_REMOVED, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public synchronized List<UiExtension> getExtensions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 255 | checkPermission(UI_READ); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 256 | return ImmutableList.copyOf(extensions); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public synchronized UiExtension getViewExtension(String viewId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 261 | checkPermission(UI_READ); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 262 | return views.get(viewId); |
| 263 | } |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 264 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 265 | @Override |
| 266 | public Set<String> getUserNames() { |
| 267 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 268 | prefs.keySet().forEach(k -> builder.add(userName(k))); |
| 269 | return builder.build(); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public Map<String, ObjectNode> getPreferences(String userName) { |
| 274 | ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder(); |
| 275 | prefs.entrySet().stream() |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 276 | .filter(e -> e.getKey().startsWith(userName + SLASH)) |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 277 | .forEach(e -> builder.put(keyName(e.getKey()), e.getValue())); |
| 278 | return builder.build(); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void setPreference(String userName, String preference, ObjectNode value) { |
| 283 | prefs.put(key(userName, preference), value); |
| 284 | } |
| 285 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 286 | // ===================================================================== |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 287 | // Provisional tracking of sprite definitions |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 288 | |
| 289 | private final Map<String, JsonNode> sprites = Maps.newHashMap(); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 290 | |
| 291 | @Override |
| 292 | public Set<String> getNames() { |
| 293 | return ImmutableSet.copyOf(sprites.keySet()); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void put(String name, JsonNode spriteData) { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 298 | log.info("Registered sprite definition [{}]", name); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 299 | sprites.put(name, spriteData); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public JsonNode get(String name) { |
| 304 | return sprites.get(name); |
| 305 | } |
| 306 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 307 | private String key(String userName, String keyName) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 308 | return userName + SLASH + keyName; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 311 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 312 | private String userName(String key) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 313 | return key.split(SLASH)[IDX_USER]; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | private String keyName(String key) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 317 | return key.split(SLASH)[IDX_KEY]; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Auxiliary listener to preference map events. |
| 321 | private class InternalPrefsListener |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 322 | implements MapEventListener<String, ObjectNode> { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 323 | @Override |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 324 | public void event(MapEvent<String, ObjectNode> event) { |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 325 | eventHandlingExecutor.execute(() -> { |
| 326 | String userName = userName(event.key()); |
| 327 | if (event.type() == MapEvent.Type.INSERT || event.type() == MapEvent.Type.UPDATE) { |
| 328 | UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs()); |
| 329 | } |
| 330 | }); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | private ObjectNode jsonPrefs() { |
| 334 | ObjectNode json = mapper.createObjectNode(); |
| 335 | prefs.entrySet().forEach(e -> json.set(keyName(e.getKey()), e.getValue())); |
| 336 | return json; |
| 337 | } |
| 338 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 339 | } |