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; |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 35 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | import org.apache.felix.scr.annotations.Activate; |
| 37 | import org.apache.felix.scr.annotations.Component; |
| 38 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 39 | import org.apache.felix.scr.annotations.Reference; |
| 40 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 41 | import org.apache.felix.scr.annotations.Service; |
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 | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 53 | import org.onosproject.ui.UiTopoOverlayFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 54 | import org.onosproject.ui.UiView; |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 55 | import org.onosproject.ui.UiViewHidden; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | import org.slf4j.LoggerFactory; |
| 58 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 59 | import java.util.LinkedHashMap; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Map; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 62 | import java.util.Set; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 63 | |
| 64 | import static com.google.common.collect.ImmutableList.of; |
| 65 | import static java.util.stream.Collectors.toSet; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 66 | import static org.onosproject.security.AppGuard.checkPermission; |
| 67 | import static org.onosproject.security.AppPermission.Type.UI_READ; |
| 68 | import static org.onosproject.security.AppPermission.Type.UI_WRITE; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 69 | import static org.onosproject.ui.UiView.Category.NETWORK; |
| 70 | import static org.onosproject.ui.UiView.Category.PLATFORM; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 71 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Manages the user interface extensions. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | @Service |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 77 | public class UiExtensionManager |
| 78 | implements UiExtensionService, UiPreferencesService, SpriteService { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 79 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 80 | private static final ClassLoader CL = UiExtensionManager.class.getClassLoader(); |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 81 | |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 82 | private static final String ONOS_USER_PREFERENCES = "onos-ui-user-preferences"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 83 | private static final String CORE = "core"; |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 84 | private static final String GUI_ADDED = "guiAdded"; |
| 85 | private static final String GUI_REMOVED = "guiRemoved"; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 86 | private static final String UPDATE_PREFS = "updatePrefs"; |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 87 | private static final String SLASH = "/"; |
| 88 | |
| 89 | private static final int IDX_USER = 0; |
| 90 | private static final int IDX_KEY = 1; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 91 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 92 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 93 | |
| 94 | // List of all extensions |
| 95 | private final List<UiExtension> extensions = Lists.newArrayList(); |
| 96 | |
| 97 | // Map of views to extensions |
| 98 | private final Map<String, UiExtension> views = Maps.newHashMap(); |
| 99 | |
| 100 | // Core views & core extension |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 101 | private final UiExtension core = createCoreExtension(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 102 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected MastershipService mastershipService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 105 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected StorageService storageService; |
| 108 | |
| 109 | // User preferences |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 110 | private ConsistentMap<String, ObjectNode> prefsConsistentMap; |
| 111 | private Map<String, ObjectNode> prefs; |
| 112 | private final MapEventListener<String, ObjectNode> prefsListener = |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 113 | new InternalPrefsListener(); |
| 114 | |
| 115 | private final ObjectMapper mapper = new ObjectMapper(); |
| 116 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 117 | // Creates core UI extension |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 118 | private UiExtension createCoreExtension() { |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 119 | List<UiView> coreViews = of( |
| 120 | new UiView(PLATFORM, "app", "Applications", "nav_apps"), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 121 | new UiView(PLATFORM, "settings", "Settings", "nav_settings"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 122 | new UiView(PLATFORM, "cluster", "Cluster Nodes", "nav_cluster"), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 123 | new UiView(PLATFORM, "processor", "Packet Processors", "nav_processors"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 124 | new UiView(NETWORK, "topo", "Topology", "nav_topo"), |
| 125 | new UiView(NETWORK, "device", "Devices", "nav_devs"), |
| 126 | new UiViewHidden("flow"), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 127 | new UiViewHidden("port"), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 128 | new UiViewHidden("group"), |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 129 | new UiViewHidden("meter"), |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 130 | new UiView(NETWORK, "link", "Links", "nav_links"), |
| 131 | new UiView(NETWORK, "host", "Hosts", "nav_hosts"), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 132 | new UiView(NETWORK, "intent", "Intents", "nav_intents"), |
| 133 | //TODO add a new type of icon for tunnel |
| 134 | new UiView(NETWORK, "tunnel", "Tunnels", "nav_links") |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 135 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 136 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 137 | UiMessageHandlerFactory messageHandlerFactory = |
| 138 | () -> ImmutableList.of( |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 139 | new UserPreferencesMessageHandler(), |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 140 | new TopologyViewMessageHandler(), |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 141 | new MapSelectorMessageHandler(), |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 142 | new DeviceViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 143 | new LinkViewMessageHandler(), |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 144 | new HostViewMessageHandler(), |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 145 | new FlowViewMessageHandler(), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 146 | new PortViewMessageHandler(), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 147 | new GroupViewMessageHandler(), |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 148 | new MeterViewMessageHandler(), |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 149 | new IntentViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 150 | new ApplicationViewMessageHandler(), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 151 | new SettingsViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 152 | new ClusterViewMessageHandler(), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 153 | new ProcessorViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 154 | new TunnelViewMessageHandler() |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 155 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 156 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 157 | UiTopoOverlayFactory topoOverlayFactory = |
| 158 | () -> ImmutableList.of( |
| 159 | new TrafficOverlay() |
| 160 | ); |
| 161 | |
| 162 | return new UiExtension.Builder(CL, coreViews) |
| 163 | .messageHandlerFactory(messageHandlerFactory) |
| 164 | .topoOverlayFactory(topoOverlayFactory) |
| 165 | .resourcePath(CORE) |
| 166 | .build(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 167 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 168 | |
| 169 | @Activate |
| 170 | public void activate() { |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 171 | Serializer serializer = Serializer.using(KryoNamespaces.API, |
| 172 | ObjectNode.class, ArrayNode.class, |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 173 | JsonNodeFactory.class, LinkedHashMap.class, |
| 174 | TextNode.class, BooleanNode.class, |
| 175 | LongNode.class, DoubleNode.class, ShortNode.class, |
| 176 | IntNode.class, NullNode.class); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 177 | |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 178 | prefsConsistentMap = storageService.<String, ObjectNode>consistentMapBuilder() |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 179 | .withName(ONOS_USER_PREFERENCES) |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 180 | .withSerializer(serializer) |
| 181 | .withRelaxedReadConsistency() |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 182 | .build(); |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 183 | prefsConsistentMap.addListener(prefsListener); |
| 184 | prefs = prefsConsistentMap.asJavaMap(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 185 | register(core); |
| 186 | log.info("Started"); |
| 187 | } |
| 188 | |
| 189 | @Deactivate |
| 190 | public void deactivate() { |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 191 | prefsConsistentMap.removeListener(prefsListener); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 192 | UiWebSocketServlet.closeAll(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 193 | unregister(core); |
| 194 | log.info("Stopped"); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public synchronized void register(UiExtension extension) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 199 | checkPermission(UI_WRITE); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 200 | if (!extensions.contains(extension)) { |
| 201 | extensions.add(extension); |
| 202 | for (UiView view : extension.views()) { |
| 203 | views.put(view.id(), extension); |
| 204 | } |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 205 | UiWebSocketServlet.sendToAll(GUI_ADDED, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public synchronized void unregister(UiExtension extension) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 211 | checkPermission(UI_WRITE); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 212 | extensions.remove(extension); |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 213 | extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove); |
| 214 | UiWebSocketServlet.sendToAll(GUI_REMOVED, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public synchronized List<UiExtension> getExtensions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 219 | checkPermission(UI_READ); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 220 | return ImmutableList.copyOf(extensions); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public synchronized UiExtension getViewExtension(String viewId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 225 | checkPermission(UI_READ); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 226 | return views.get(viewId); |
| 227 | } |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 228 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 229 | @Override |
| 230 | public Set<String> getUserNames() { |
| 231 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 232 | prefs.keySet().forEach(k -> builder.add(userName(k))); |
| 233 | return builder.build(); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Map<String, ObjectNode> getPreferences(String userName) { |
| 238 | ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder(); |
| 239 | prefs.entrySet().stream() |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 240 | .filter(e -> e.getKey().startsWith(userName + SLASH)) |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 241 | .forEach(e -> builder.put(keyName(e.getKey()), e.getValue())); |
| 242 | return builder.build(); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void setPreference(String userName, String preference, ObjectNode value) { |
| 247 | prefs.put(key(userName, preference), value); |
| 248 | } |
| 249 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 250 | // ===================================================================== |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 251 | // Provisional tracking of sprite definitions |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 252 | |
| 253 | private final Map<String, JsonNode> sprites = Maps.newHashMap(); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 254 | |
| 255 | @Override |
| 256 | public Set<String> getNames() { |
| 257 | return ImmutableSet.copyOf(sprites.keySet()); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void put(String name, JsonNode spriteData) { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 262 | log.info("Registered sprite definition [{}]", name); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 263 | sprites.put(name, spriteData); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public JsonNode get(String name) { |
| 268 | return sprites.get(name); |
| 269 | } |
| 270 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 271 | private String key(String userName, String keyName) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 272 | return userName + SLASH + keyName; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 275 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 276 | private String userName(String key) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 277 | return key.split(SLASH)[IDX_USER]; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private String keyName(String key) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 281 | return key.split(SLASH)[IDX_KEY]; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Auxiliary listener to preference map events. |
| 285 | private class InternalPrefsListener |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 286 | implements MapEventListener<String, ObjectNode> { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 287 | @Override |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 288 | public void event(MapEvent<String, ObjectNode> event) { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 289 | String userName = userName(event.key()); |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame^] | 290 | if (event.type() == MapEvent.Type.INSERT || event.type() == MapEvent.Type.UPDATE) { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 291 | UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs()); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | private ObjectNode jsonPrefs() { |
| 296 | ObjectNode json = mapper.createObjectNode(); |
| 297 | prefs.entrySet().forEach(e -> json.set(keyName(e.getKey()), e.getValue())); |
| 298 | return json; |
| 299 | } |
| 300 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 301 | } |