Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 35 | import org.onlab.util.Tools; |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 36 | import org.onosproject.cfg.ComponentConfigService; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 37 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 38 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.ConsistentMap; |
| 40 | import org.onosproject.store.service.MapEvent; |
| 41 | import org.onosproject.store.service.MapEventListener; |
| 42 | import org.onosproject.store.service.Serializer; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 43 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 44 | import org.onosproject.ui.UiExtension; |
| 45 | import org.onosproject.ui.UiExtensionService; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 46 | import org.onosproject.ui.UiGlyph; |
| 47 | import org.onosproject.ui.UiGlyphFactory; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 48 | import org.onosproject.ui.UiMessageHandlerFactory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 49 | import org.onosproject.ui.UiPreferencesService; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 50 | import org.onosproject.ui.UiSessionToken; |
| 51 | import org.onosproject.ui.UiTokenService; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 52 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 95f2d3e | 2021-03-30 16:31:34 -0700 | [diff] [blame] | 53 | import org.onosproject.ui.UiTopoHighlighterFactory; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 54 | import org.onosproject.ui.UiTopoMap; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 55 | import org.onosproject.ui.UiTopoMapFactory; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 56 | import org.onosproject.ui.UiTopoOverlayFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 57 | import org.onosproject.ui.UiView; |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 58 | import org.onosproject.ui.UiViewHidden; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 59 | import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 60 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 61 | import org.onosproject.ui.impl.topo.Traffic2Overlay; |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 62 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 63 | import org.onosproject.ui.lion.LionBundle; |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 64 | import org.onosproject.ui.lion.LionUtils; |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 65 | import org.onosproject.ui.topo.AbstractTopoMonitor; |
| 66 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | import org.osgi.service.component.annotations.Activate; |
| 68 | import org.osgi.service.component.annotations.Component; |
| 69 | import org.osgi.service.component.annotations.Deactivate; |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 70 | import org.osgi.service.component.annotations.Modified; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | import org.osgi.service.component.annotations.Reference; |
| 72 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 73 | import org.slf4j.Logger; |
| 74 | import org.slf4j.LoggerFactory; |
| 75 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 76 | import java.math.BigInteger; |
| 77 | import java.security.SecureRandom; |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 78 | import java.util.Dictionary; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 79 | import java.util.LinkedHashMap; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 80 | import java.util.List; |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 81 | import java.util.Locale; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 82 | import java.util.Map; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 83 | import java.util.Set; |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 84 | import java.util.concurrent.ExecutorService; |
| 85 | import java.util.concurrent.Executors; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 86 | |
| 87 | import static com.google.common.collect.ImmutableList.of; |
| 88 | import static java.util.stream.Collectors.toSet; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 89 | import static org.onosproject.security.AppGuard.checkPermission; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 90 | import static org.onosproject.security.AppPermission.Type.GLYPH_READ; |
| 91 | import static org.onosproject.security.AppPermission.Type.GLYPH_WRITE; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 92 | import static org.onosproject.security.AppPermission.Type.UI_READ; |
| 93 | import static org.onosproject.security.AppPermission.Type.UI_WRITE; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 94 | import static org.onosproject.ui.UiView.Category.NETWORK; |
| 95 | import static org.onosproject.ui.UiView.Category.PLATFORM; |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 96 | import static org.onosproject.ui.impl.OsgiPropertyConstants.TRAFFIC_REFRESH_MS; |
| 97 | import static org.onosproject.ui.impl.OsgiPropertyConstants.TRAFFIC_REFRESH_MS_DEFAULT; |
Simon Hunt | d875465 | 2017-06-21 11:45:22 -0700 | [diff] [blame] | 98 | import static org.onosproject.ui.impl.lion.BundleStitcher.generateBundles; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 99 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Manages the user interface extensions. |
| 102 | */ |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 103 | @Component(immediate = true, |
| 104 | service = { |
| 105 | UiExtensionService.class, |
| 106 | UiPreferencesService.class, |
| 107 | SpriteService.class, |
| 108 | UiTokenService.class |
| 109 | }, |
| 110 | property = { |
| 111 | TRAFFIC_REFRESH_MS + ":Integer=" + TRAFFIC_REFRESH_MS_DEFAULT, |
| 112 | }) |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 113 | public class UiExtensionManager |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 114 | implements UiExtensionService, UiPreferencesService, SpriteService, |
| 115 | UiTokenService { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 116 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 117 | private static final ClassLoader CL = UiExtensionManager.class.getClassLoader(); |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 118 | |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 119 | private static final String ONOS_USER_PREFERENCES = "onos-ui-user-preferences"; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 120 | private static final String ONOS_SESSION_TOKENS = "onos-ui-session-tokens"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 121 | private static final String CORE = "core"; |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 122 | private static final String GUI_ADDED = "guiAdded"; |
| 123 | private static final String GUI_REMOVED = "guiRemoved"; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 124 | private static final String GLYPH_ADDED = "glyphAdded"; |
| 125 | private static final String GLYPH_REMOVED = "glyphRemoved"; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 126 | private static final String UPDATE_PREFS = "updatePrefs"; |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 127 | private static final String SLASH = "/"; |
| 128 | |
| 129 | private static final int IDX_USER = 0; |
| 130 | private static final int IDX_KEY = 1; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 131 | |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 132 | private static final String LION_BASE = "/org/onosproject/ui/lion"; |
| 133 | |
| 134 | private static final String[] LION_TAGS = { |
Simon Hunt | cffb286 | 2017-06-23 12:02:43 -0700 | [diff] [blame] | 135 | // framework component localization |
| 136 | "core.fw.Mast", |
| 137 | "core.fw.Nav", |
| 138 | "core.fw.QuickHelp", |
| 139 | |
| 140 | // view component localization |
| 141 | "core.view.App", |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 142 | "core.view.Cluster", |
Simon Hunt | 5989ddf | 2017-08-02 20:38:12 -0700 | [diff] [blame] | 143 | "core.view.Topo", |
ArturoF Velasco | 604ad0b | 2018-01-03 00:43:58 +0100 | [diff] [blame] | 144 | "core.view.Flow", |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 145 | |
Simon Hunt | cffb286 | 2017-06-23 12:02:43 -0700 | [diff] [blame] | 146 | // TODO: More to come... |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 150 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 151 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 152 | // First thing to do is to set the locale (before creating core extension). |
| 153 | private final Locale runtimeLocale = LionUtils.setupRuntimeLocale(); |
| 154 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 155 | // List of all extensions |
| 156 | private final List<UiExtension> extensions = Lists.newArrayList(); |
| 157 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 158 | private final List<UiGlyph> glyphs = Lists.newArrayList(); |
Thomas Vachuska | 95f2d3e | 2021-03-30 16:31:34 -0700 | [diff] [blame] | 159 | private final List<UiTopoHighlighterFactory> highlighterFactories = Lists.newArrayList(); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 160 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 161 | // Map of views to extensions |
| 162 | private final Map<String, UiExtension> views = Maps.newHashMap(); |
| 163 | |
| 164 | // Core views & core extension |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 165 | private final UiExtension core = createCoreExtension(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 166 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 167 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 168 | protected MastershipService mastershipService; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 169 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 171 | protected StorageService storageService; |
| 172 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 173 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 174 | protected ComponentConfigService cfgService; |
| 175 | |
| 176 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 177 | private UiSharedTopologyModel sharedModel; |
| 178 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 179 | // User preferences |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 180 | private ConsistentMap<String, ObjectNode> prefsConsistentMap; |
| 181 | private Map<String, ObjectNode> prefs; |
| 182 | private final MapEventListener<String, ObjectNode> prefsListener = |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 183 | new InternalPrefsListener(); |
| 184 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 185 | // Session tokens |
| 186 | private ConsistentMap<UiSessionToken, String> tokensConsistentMap; |
| 187 | private Map<UiSessionToken, String> tokens; |
| 188 | private final SessionTokenGenerator tokenGen = |
| 189 | new SessionTokenGenerator(); |
| 190 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 191 | private final ObjectMapper mapper = new ObjectMapper(); |
| 192 | |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 193 | private final ExecutorService eventHandlingExecutor = |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 194 | Executors.newSingleThreadExecutor( |
| 195 | Tools.groupedThreads("onos/ui-ext-manager", "event-handler", log)); |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 196 | |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 197 | private LionBundle navLion; |
| 198 | |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 199 | protected int trafficRefreshMs = TRAFFIC_REFRESH_MS_DEFAULT; |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 200 | |
| 201 | private String lionNavText(String id) { |
| 202 | return navLion.getValue("nav_item_" + id); |
| 203 | } |
| 204 | |
| 205 | private UiView mkView(UiView.Category cat, String id, String iconId) { |
| 206 | return new UiView(cat, id, lionNavText(id), iconId); |
| 207 | } |
| 208 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 209 | private UiExtension createCoreExtension() { |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 210 | List<LionBundle> lionBundles = generateBundles(LION_BASE, LION_TAGS); |
| 211 | |
| 212 | navLion = lionBundles.stream() |
| 213 | .filter(f -> f.id().equals("core.fw.Nav")).findFirst().get(); |
| 214 | |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 215 | List<UiView> coreViews = of( |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 216 | mkView(PLATFORM, "app", "nav_apps"), |
| 217 | mkView(PLATFORM, "settings", "nav_settings"), |
| 218 | mkView(PLATFORM, "cluster", "nav_cluster"), |
| 219 | mkView(PLATFORM, "processor", "nav_processors"), |
| 220 | mkView(PLATFORM, "partition", "nav_partitions"), |
| 221 | |
| 222 | mkView(NETWORK, "topo", "nav_topo"), |
Thomas Vachuska | 175c33e | 2019-01-31 15:02:27 -0800 | [diff] [blame] | 223 | // mkView(NETWORK, "topo2", "nav_topo2"), |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 224 | mkView(NETWORK, "device", "nav_devs"), |
| 225 | |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 226 | new UiViewHidden("flow"), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 227 | new UiViewHidden("port"), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 228 | new UiViewHidden("group"), |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 229 | new UiViewHidden("meter"), |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 230 | new UiViewHidden("pipeconf"), |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 231 | |
| 232 | mkView(NETWORK, "link", "nav_links"), |
| 233 | mkView(NETWORK, "host", "nav_hosts"), |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 234 | mkView(NETWORK, "intent", "nav_intents") |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 235 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 236 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 237 | UiMessageHandlerFactory messageHandlerFactory = |
| 238 | () -> ImmutableList.of( |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 239 | new UserPreferencesMessageHandler(), |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 240 | new TopologyViewMessageHandler(), |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 241 | new Topo2ViewMessageHandler(), |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 242 | new Topo2TrafficMessageHandler(), |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 243 | new MapSelectorMessageHandler(), |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 244 | new DeviceViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 245 | new LinkViewMessageHandler(), |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 246 | new HostViewMessageHandler(), |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 247 | new FlowViewMessageHandler(), |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 248 | new PortViewMessageHandler(), |
Bri Prebilic Cole | ff3dc67 | 2015-05-06 12:59:38 -0700 | [diff] [blame] | 249 | new GroupViewMessageHandler(), |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 250 | new MeterViewMessageHandler(), |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 251 | new IntentViewMessageHandler(), |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 252 | new ApplicationViewMessageHandler(), |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 253 | new SettingsViewMessageHandler(), |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 254 | new ClusterViewMessageHandler(), |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 255 | new ProcessorViewMessageHandler(), |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 256 | new PartitionViewMessageHandler(), |
| 257 | new PipeconfViewMessageHandler() |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 258 | ); |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 259 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 260 | UiTopoOverlayFactory topoOverlayFactory = |
| 261 | () -> ImmutableList.of( |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 262 | new TrafficOverlay(), |
| 263 | new ProtectedIntentOverlay() |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 264 | ); |
| 265 | |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 266 | UiTopo2OverlayFactory topo2OverlayFactory = |
| 267 | () -> ImmutableList.of( |
| 268 | new Traffic2Overlay() |
| 269 | ); |
| 270 | |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 271 | UiTopoMapFactory topoMapFactory = |
| 272 | () -> ImmutableList.of( |
| 273 | new UiTopoMap("australia", "Australia", "*australia", 1.0), |
| 274 | new UiTopoMap("americas", "North, Central and South America", "*americas", 0.7), |
| 275 | new UiTopoMap("n_america", "North America", "*n_america", 0.9), |
| 276 | new UiTopoMap("s_america", "South America", "*s_america", 0.9), |
Thomas Vachuska | cc0b7d6 | 2016-07-12 14:03:11 -0700 | [diff] [blame] | 277 | new UiTopoMap("usa", "United States", "*continental_us", 1.3), |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 278 | new UiTopoMap("bayareaGEO", "Bay Area, California", "*bayarea", 1.0), |
Thomas Vachuska | cc0b7d6 | 2016-07-12 14:03:11 -0700 | [diff] [blame] | 279 | new UiTopoMap("europe", "Europe", "*europe", 10.0), |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 280 | new UiTopoMap("italy", "Italy", "*italy", 0.8), |
Thomas Vachuska | cc0b7d6 | 2016-07-12 14:03:11 -0700 | [diff] [blame] | 281 | new UiTopoMap("uk", "United Kingdom and Ireland", "*uk", 2.0), |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 282 | new UiTopoMap("japan", "Japan", "*japan", 0.8), |
| 283 | new UiTopoMap("s_korea", "South Korea", "*s_korea", 0.75), |
| 284 | new UiTopoMap("taiwan", "Taiwan", "*taiwan", 0.7), |
| 285 | new UiTopoMap("africa", "Africa", "*africa", 0.7), |
| 286 | new UiTopoMap("oceania", "Oceania", "*oceania", 0.7), |
| 287 | new UiTopoMap("asia", "Asia", "*asia", 0.7) |
| 288 | ); |
| 289 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 290 | return new UiExtension.Builder(CL, coreViews) |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 291 | .lionBundles(lionBundles) |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 292 | .messageHandlerFactory(messageHandlerFactory) |
| 293 | .topoOverlayFactory(topoOverlayFactory) |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 294 | .topo2OverlayFactory(topo2OverlayFactory) |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 295 | .topoMapFactory(topoMapFactory) |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 296 | .resourcePath(CORE) |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 297 | .ui2() |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 298 | .build(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 299 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 300 | |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 301 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 302 | @Activate |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 303 | public void activate(ComponentContext context) { |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 304 | Serializer serializer = Serializer.using(KryoNamespaces.API, |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 305 | ObjectNode.class, ArrayNode.class, |
| 306 | JsonNodeFactory.class, LinkedHashMap.class, |
| 307 | TextNode.class, BooleanNode.class, |
| 308 | LongNode.class, DoubleNode.class, ShortNode.class, |
| 309 | IntNode.class, NullNode.class, UiSessionToken.class); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 310 | |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 311 | prefsConsistentMap = storageService.<String, ObjectNode>consistentMapBuilder() |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 312 | .withName(ONOS_USER_PREFERENCES) |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 313 | .withSerializer(serializer) |
| 314 | .withRelaxedReadConsistency() |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 315 | .build(); |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 316 | prefsConsistentMap.addListener(prefsListener); |
| 317 | prefs = prefsConsistentMap.asJavaMap(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 318 | |
| 319 | tokensConsistentMap = storageService.<UiSessionToken, String>consistentMapBuilder() |
| 320 | .withName(ONOS_SESSION_TOKENS) |
| 321 | .withSerializer(serializer) |
| 322 | .withRelaxedReadConsistency() |
| 323 | .build(); |
| 324 | tokens = tokensConsistentMap.asJavaMap(); |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 325 | cfgService.registerProperties(getClass()); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 326 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 327 | register(core); |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 328 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 329 | log.info("Started"); |
| 330 | } |
| 331 | |
| 332 | @Deactivate |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 333 | public void deactivate(ComponentContext context) { |
| 334 | cfgService.unregisterProperties(getClass(), false); |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 335 | prefsConsistentMap.removeListener(prefsListener); |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 336 | eventHandlingExecutor.shutdown(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 337 | UiWebSocketServlet.closeAll(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 338 | unregister(core); |
| 339 | log.info("Stopped"); |
| 340 | } |
| 341 | |
Thomas Vachuska | e33e2dc | 2021-04-06 11:14:09 -0700 | [diff] [blame] | 342 | @Modified |
| 343 | protected void modified(ComponentContext context) { |
| 344 | Dictionary<?, ?> properties = context.getProperties(); |
| 345 | Integer trafficRefresh = Tools.getIntegerProperty(properties, TRAFFIC_REFRESH_MS); |
| 346 | |
| 347 | if (trafficRefresh != null && trafficRefresh > 10) { |
| 348 | AbstractTopoMonitor.setTrafficPeriod(trafficRefresh); |
| 349 | } else if (trafficRefresh != null) { |
| 350 | log.warn("trafficRefresh must be greater than 10"); |
| 351 | } |
| 352 | |
| 353 | log.info("Settings: trafficRefresh={}", trafficRefresh); |
| 354 | } |
| 355 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 356 | @Override |
| 357 | public synchronized void register(UiExtension extension) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 358 | checkPermission(UI_WRITE); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 359 | if (!extensions.contains(extension)) { |
| 360 | extensions.add(extension); |
| 361 | for (UiView view : extension.views()) { |
| 362 | views.put(view.id(), extension); |
| 363 | } |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 364 | UiWebSocketServlet.sendToAll(GUI_ADDED, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public synchronized void unregister(UiExtension extension) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 370 | checkPermission(UI_WRITE); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 371 | extensions.remove(extension); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 372 | extension.views().stream() |
| 373 | .map(UiView::id).collect(toSet()).forEach(views::remove); |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 374 | UiWebSocketServlet.sendToAll(GUI_REMOVED, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 378 | public synchronized void register(UiGlyphFactory glyphFactory) { |
| 379 | checkPermission(GLYPH_WRITE); |
| 380 | boolean glyphAdded = false; |
| 381 | for (UiGlyph glyph : glyphFactory.glyphs()) { |
| 382 | if (!glyphs.contains(glyph)) { |
| 383 | glyphs.add(glyph); |
| 384 | glyphAdded = true; |
| 385 | } |
| 386 | } |
| 387 | if (glyphAdded) { |
| 388 | UiWebSocketServlet.sendToAll(GLYPH_ADDED, null); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public synchronized void unregister(UiGlyphFactory glyphFactory) { |
| 394 | checkPermission(GLYPH_WRITE); |
| 395 | boolean glyphRemoved = false; |
| 396 | for (UiGlyph glyph : glyphFactory.glyphs()) { |
| 397 | glyphs.remove(glyph); |
| 398 | glyphRemoved = true; |
| 399 | } |
| 400 | if (glyphRemoved) { |
| 401 | UiWebSocketServlet.sendToAll(GLYPH_REMOVED, null); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | @Override |
Thomas Vachuska | 95f2d3e | 2021-03-30 16:31:34 -0700 | [diff] [blame] | 406 | public synchronized void register(UiTopoHighlighterFactory factory) { |
| 407 | checkPermission(UI_WRITE); |
| 408 | if (!highlighterFactories.contains(factory)) { |
| 409 | highlighterFactories.add(factory); |
| 410 | UiWebSocketServlet.sendToAll(GUI_ADDED, null); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public synchronized void unregister(UiTopoHighlighterFactory factory) { |
| 416 | checkPermission(UI_WRITE); |
| 417 | highlighterFactories.remove(factory); |
| 418 | UiWebSocketServlet.sendToAll(GUI_REMOVED, null); |
| 419 | } |
| 420 | |
| 421 | @Override |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 422 | public synchronized List<UiExtension> getExtensions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 423 | checkPermission(UI_READ); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 424 | return ImmutableList.copyOf(extensions); |
| 425 | } |
| 426 | |
| 427 | @Override |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 428 | public synchronized List<UiGlyph> getGlyphs() { |
| 429 | checkPermission(GLYPH_READ); |
| 430 | return ImmutableList.copyOf(glyphs); |
| 431 | } |
| 432 | |
| 433 | @Override |
Thomas Vachuska | 95f2d3e | 2021-03-30 16:31:34 -0700 | [diff] [blame] | 434 | public synchronized List<UiTopoHighlighterFactory> getTopoHighlighterFactories() { |
| 435 | checkPermission(UI_READ); |
| 436 | return ImmutableList.copyOf(highlighterFactories); |
| 437 | } |
| 438 | |
| 439 | @Override |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 440 | public synchronized UiExtension getViewExtension(String viewId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 441 | checkPermission(UI_READ); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 442 | return views.get(viewId); |
| 443 | } |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 444 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 445 | @Override |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 446 | public synchronized LionBundle getNavLionBundle() { |
| 447 | return navLion; |
| 448 | } |
| 449 | |
| 450 | @Override |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 451 | public void refreshModel() { |
| 452 | sharedModel.reload(); |
| 453 | } |
| 454 | |
| 455 | @Override |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 456 | public Set<String> getUserNames() { |
| 457 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 458 | prefs.keySet().forEach(k -> builder.add(userName(k))); |
| 459 | return builder.build(); |
| 460 | } |
| 461 | |
| 462 | @Override |
Simon Hunt | cfef6f9 | 2017-07-25 16:58:42 -0700 | [diff] [blame] | 463 | public Map<String, ObjectNode> getPreferences(String username) { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 464 | ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder(); |
| 465 | prefs.entrySet().stream() |
Simon Hunt | cfef6f9 | 2017-07-25 16:58:42 -0700 | [diff] [blame] | 466 | .filter(e -> e.getKey().startsWith(username + SLASH)) |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 467 | .forEach(e -> builder.put(keyName(e.getKey()), e.getValue())); |
| 468 | return builder.build(); |
| 469 | } |
| 470 | |
| 471 | @Override |
Simon Hunt | cfef6f9 | 2017-07-25 16:58:42 -0700 | [diff] [blame] | 472 | public ObjectNode getPreference(String username, String key) { |
| 473 | return prefs.get(key(username, key)); |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void setPreference(String username, String key, ObjectNode value) { |
Thomas Vachuska | 99b7bbe | 2018-02-01 15:29:46 -0800 | [diff] [blame] | 478 | if (value != null) { |
| 479 | prefs.put(key(username, key), value); |
| 480 | } else { |
| 481 | prefs.remove(key(username, key)); |
| 482 | } |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 485 | // ===================================================================== |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 486 | // Provisional tracking of sprite definitions |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 487 | |
| 488 | private final Map<String, JsonNode> sprites = Maps.newHashMap(); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 489 | |
| 490 | @Override |
| 491 | public Set<String> getNames() { |
| 492 | return ImmutableSet.copyOf(sprites.keySet()); |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public void put(String name, JsonNode spriteData) { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 497 | log.info("Registered sprite definition [{}]", name); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 498 | sprites.put(name, spriteData); |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public JsonNode get(String name) { |
| 503 | return sprites.get(name); |
| 504 | } |
| 505 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 506 | private String key(String userName, String keyName) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 507 | return userName + SLASH + keyName; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 510 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 511 | private String userName(String key) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 512 | return key.split(SLASH)[IDX_USER]; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | private String keyName(String key) { |
Simon Hunt | 3678c2a | 2016-03-28 14:48:07 -0700 | [diff] [blame] | 516 | return key.split(SLASH)[IDX_KEY]; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 519 | |
| 520 | // ===================================================================== |
| 521 | // UiTokenService |
| 522 | |
| 523 | @Override |
| 524 | public UiSessionToken issueToken(String username) { |
| 525 | UiSessionToken token = new UiSessionToken(tokenGen.nextSessionId()); |
| 526 | tokens.put(token, username); |
| 527 | log.debug("UiSessionToken issued: {}", token); |
| 528 | return token; |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public void revokeToken(UiSessionToken token) { |
| 533 | if (token != null) { |
| 534 | tokens.remove(token); |
| 535 | log.debug("UiSessionToken revoked: {}", token); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public boolean isTokenValid(UiSessionToken token) { |
| 541 | return token != null && tokens.containsKey(token); |
| 542 | } |
| 543 | |
| 544 | private final class SessionTokenGenerator { |
| 545 | private final SecureRandom random = new SecureRandom(); |
| 546 | |
| 547 | /* |
| 548 | This works by choosing 130 bits from a cryptographically secure |
| 549 | random bit generator, and encoding them in base-32. |
| 550 | |
| 551 | 128 bits is considered to be cryptographically strong, but each |
| 552 | digit in a base 32 number can encode 5 bits, so 128 is rounded up |
| 553 | to the next multiple of 5. |
| 554 | |
| 555 | This encoding is compact and efficient, with 5 random bits per |
| 556 | character. Compare this to a random UUID, which only has 3.4 bits |
| 557 | per character in standard layout, and only 122 random bits in total. |
| 558 | |
| 559 | Note that SecureRandom objects are expensive to initialize, so |
| 560 | we'll want to keep it around and re-use it. |
| 561 | */ |
| 562 | |
| 563 | private String nextSessionId() { |
| 564 | return new BigInteger(130, random).toString(32); |
| 565 | } |
| 566 | } |
| 567 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 568 | // Auxiliary listener to preference map events. |
| 569 | private class InternalPrefsListener |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 570 | implements MapEventListener<String, ObjectNode> { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 571 | @Override |
Madan Jampani | 7b93ceb | 2016-05-04 09:58:40 -0700 | [diff] [blame] | 572 | public void event(MapEvent<String, ObjectNode> event) { |
Madan Jampani | bf8ee80 | 2016-05-04 14:07:36 -0700 | [diff] [blame] | 573 | eventHandlingExecutor.execute(() -> { |
| 574 | String userName = userName(event.key()); |
| 575 | if (event.type() == MapEvent.Type.INSERT || event.type() == MapEvent.Type.UPDATE) { |
| 576 | UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs()); |
| 577 | } |
| 578 | }); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | private ObjectNode jsonPrefs() { |
| 582 | ObjectNode json = mapper.createObjectNode(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 583 | prefs.forEach((key, value) -> json.set(keyName(key), value)); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 584 | return json; |
| 585 | } |
| 586 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 587 | } |