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