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