Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -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 | 02aeb03 | 2015-01-06 22:36:30 -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.app.impl; |
| 17 | |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 22 | import com.google.common.cache.Cache; |
| 23 | import com.google.common.cache.CacheBuilder; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 24 | import com.google.common.collect.HashMultimap; |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
| 26 | import com.google.common.collect.ImmutableSet; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 27 | import com.google.common.collect.Multimap; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 28 | import com.google.common.util.concurrent.Uninterruptibles; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 29 | import org.apache.karaf.features.Feature; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 30 | import org.apache.karaf.features.FeaturesService; |
| 31 | import org.onosproject.app.ApplicationAdminService; |
| 32 | import org.onosproject.app.ApplicationEvent; |
| 33 | import org.onosproject.app.ApplicationListener; |
| 34 | import org.onosproject.app.ApplicationService; |
| 35 | import org.onosproject.app.ApplicationState; |
| 36 | import org.onosproject.app.ApplicationStore; |
| 37 | import org.onosproject.app.ApplicationStoreDelegate; |
| 38 | import org.onosproject.core.Application; |
| 39 | import org.onosproject.core.ApplicationId; |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 40 | import org.onosproject.core.DefaultApplication; |
| 41 | import org.onosproject.core.DefaultApplicationId; |
| 42 | import org.onosproject.core.Version; |
| 43 | import org.onosproject.core.VersionService; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 44 | import org.onosproject.event.AbstractListenerManager; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 45 | import org.onosproject.security.Permission; |
| 46 | import org.onosproject.security.SecurityUtil; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | import org.osgi.service.component.annotations.Activate; |
| 48 | import org.osgi.service.component.annotations.Component; |
| 49 | import org.osgi.service.component.annotations.Deactivate; |
| 50 | import org.osgi.service.component.annotations.Reference; |
| 51 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 54 | import java.io.IOException; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 55 | import java.io.InputStream; |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 56 | import java.net.HttpURLConnection; |
| 57 | import java.net.MalformedURLException; |
| 58 | import java.net.URL; |
| 59 | import java.util.Iterator; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 60 | import java.util.Set; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 61 | import java.util.concurrent.CountDownLatch; |
| 62 | import java.util.concurrent.TimeUnit; |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 63 | import java.util.function.Consumer; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 64 | |
| 65 | import static com.google.common.base.Preconditions.checkNotNull; |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 66 | import static org.onosproject.app.ApplicationEvent.Type.*; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 67 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 68 | import static org.onosproject.security.AppPermission.Type.APP_READ; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 69 | import static org.slf4j.LoggerFactory.getLogger; |
| 70 | |
| 71 | /** |
| 72 | * Implementation of the application management service. |
| 73 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Component(immediate = true, service = {ApplicationService.class, ApplicationAdminService.class}) |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 75 | public class ApplicationManager |
| 76 | extends AbstractListenerManager<ApplicationEvent, ApplicationListener> |
| 77 | implements ApplicationService, ApplicationAdminService { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | |
| 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 81 | private static final String APP_REGISTRY_URL = "http://api.onosproject.org:8080/api/applications"; |
| 82 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 83 | private static final String APP_ID_NULL = "Application ID cannot be null"; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 84 | private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 2000; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 85 | private final ApplicationStoreDelegate delegate = new InternalStoreDelegate(); |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 88 | protected ApplicationStore store; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 91 | protected FeaturesService featuresService; |
| 92 | |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 94 | protected VersionService versionService; |
| 95 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 96 | // Application supplied hooks for pre-activation processing. |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 97 | private final Multimap<String, Runnable> deactivateHooks = HashMultimap.create(); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 98 | private final Cache<ApplicationId, CountDownLatch> pendingOperations = |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 99 | CacheBuilder.newBuilder() |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 100 | .expireAfterWrite(DEFAULT_OPERATION_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS) |
| 101 | .build(); |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 102 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 103 | @Activate |
| 104 | public void activate() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 105 | eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 106 | store.setDelegate(delegate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 112 | eventDispatcher.removeSink(ApplicationEvent.class); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 113 | store.unsetDelegate(delegate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Set<Application> getApplications() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 119 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 120 | return store.getApplications(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public ApplicationId getId(String name) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 125 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 126 | checkNotNull(name, "Name cannot be null"); |
| 127 | return store.getId(name); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Application getApplication(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 132 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 133 | checkNotNull(appId, APP_ID_NULL); |
| 134 | return store.getApplication(appId); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public ApplicationState getState(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 139 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 140 | checkNotNull(appId, APP_ID_NULL); |
| 141 | return store.getState(appId); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Set<Permission> getPermissions(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 146 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 147 | checkNotNull(appId, APP_ID_NULL); |
| 148 | return store.getPermissions(appId); |
| 149 | } |
| 150 | |
| 151 | @Override |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 152 | public void registerDeactivateHook(ApplicationId appId, Runnable hook) { |
| 153 | checkPermission(APP_READ); |
| 154 | checkNotNull(appId, APP_ID_NULL); |
| 155 | checkNotNull(hook, "Hook cannot be null"); |
| 156 | deactivateHooks.put(appId.name(), hook); |
| 157 | } |
| 158 | |
| 159 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 160 | public Application install(InputStream appDescStream) { |
| 161 | checkNotNull(appDescStream, "Application archive stream cannot be null"); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 162 | Application app = store.create(appDescStream); |
| 163 | SecurityUtil.register(app.id()); |
| 164 | return app; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void uninstall(ApplicationId appId) { |
| 169 | checkNotNull(appId, APP_ID_NULL); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 170 | updateStoreAndWaitForNotificationHandling(appId, store::remove); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void activate(ApplicationId appId) { |
| 175 | checkNotNull(appId, APP_ID_NULL); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 176 | if (!SecurityUtil.isAppSecured(appId)) { |
| 177 | return; |
| 178 | } |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 179 | updateStoreAndWaitForNotificationHandling(appId, store::activate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void deactivate(ApplicationId appId) { |
| 184 | checkNotNull(appId, APP_ID_NULL); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 185 | updateStoreAndWaitForNotificationHandling(appId, store::deactivate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 190 | checkNotNull(appId, APP_ID_NULL); |
| 191 | checkNotNull(permissions, "Permissions cannot be null"); |
| 192 | store.setPermissions(appId, permissions); |
| 193 | } |
| 194 | |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 195 | @Override |
| 196 | public InputStream getApplicationArchive(ApplicationId appId) { |
| 197 | checkNotNull(appId, APP_ID_NULL); |
| 198 | return store.getApplicationArchive(appId); |
| 199 | } |
| 200 | |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 201 | private void updateStoreAndWaitForNotificationHandling(ApplicationId appId, |
| 202 | Consumer<ApplicationId> storeUpdateTask) { |
| 203 | CountDownLatch latch = new CountDownLatch(1); |
| 204 | try { |
| 205 | pendingOperations.put(appId, latch); |
| 206 | storeUpdateTask.accept(appId); |
| 207 | } catch (Exception e) { |
| 208 | pendingOperations.invalidate(appId); |
| 209 | latch.countDown(); |
| 210 | log.warn("Failed to update store for {}", appId.name(), e); |
| 211 | } |
| 212 | Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 213 | } |
| 214 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 215 | private class InternalStoreDelegate implements ApplicationStoreDelegate { |
| 216 | @Override |
| 217 | public void notify(ApplicationEvent event) { |
| 218 | ApplicationEvent.Type type = event.type(); |
| 219 | Application app = event.subject(); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 220 | CountDownLatch latch = pendingOperations.getIfPresent(app.id()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 221 | try { |
| 222 | if (type == APP_ACTIVATED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 223 | if (installAppFeatures(app)) { |
| 224 | log.info("Application {} has been activated", app.id().name()); |
| 225 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 226 | |
| 227 | } else if (type == APP_DEACTIVATED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 228 | if (uninstallAppFeatures(app)) { |
| 229 | log.info("Application {} has been deactivated", app.id().name()); |
| 230 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 231 | |
| 232 | } else if (type == APP_INSTALLED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 233 | if (installAppArtifacts(app)) { |
| 234 | log.info("Application {} has been installed", app.id().name()); |
| 235 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 236 | |
| 237 | } else if (type == APP_UNINSTALLED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 238 | if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) { |
| 239 | log.info("Application {} has been uninstalled", app.id().name()); |
| 240 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 241 | |
| 242 | } |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 243 | post(event); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 244 | } catch (Exception e) { |
| 245 | log.warn("Unable to perform operation on application " + app.id().name(), e); |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 246 | } finally { |
| 247 | if (latch != null) { |
| 248 | latch.countDown(); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 249 | pendingOperations.invalidate(app.id()); |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 250 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Thomas Vachuska | 0d6af8d | 2015-03-23 15:54:47 -0700 | [diff] [blame] | 255 | // The following methods are fully synchronized to guard against remote vs. |
| 256 | // locally induced feature service interactions. |
| 257 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 258 | // Installs all feature repositories required by the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 259 | private synchronized boolean installAppArtifacts(Application app) throws Exception { |
| 260 | if (app.featuresRepo().isPresent() && |
| 261 | featuresService.getRepository(app.featuresRepo().get()) == null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 262 | featuresService.addRepository(app.featuresRepo().get()); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 263 | return true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 264 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 265 | return false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 268 | // Uninstalls all the feature repositories required by the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 269 | private synchronized boolean uninstallAppArtifacts(Application app) throws Exception { |
| 270 | if (app.featuresRepo().isPresent() && |
| 271 | featuresService.getRepository(app.featuresRepo().get()) != null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 272 | featuresService.removeRepository(app.featuresRepo().get()); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 273 | return true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 274 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 275 | return false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 278 | // Installs all features that define the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 279 | private synchronized boolean installAppFeatures(Application app) throws Exception { |
| 280 | boolean changed = false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 281 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 282 | Feature feature = featuresService.getFeature(name); |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 283 | |
| 284 | // If we see an attempt at activation of a non-existent feature |
| 285 | // attempt to install the app artifacts first and then retry. |
| 286 | // This can be triggered by a race condition between different ONOS |
| 287 | // instances "installing" the apps from disk at their own pace. |
| 288 | // Perhaps there is a more elegant solution to be explored in the |
| 289 | // future. |
| 290 | if (feature == null) { |
| 291 | installAppArtifacts(app); |
| 292 | feature = featuresService.getFeature(name); |
| 293 | } |
| 294 | |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 295 | if (feature != null && !featuresService.isInstalled(feature)) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 296 | featuresService.installFeature(name); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 297 | changed = true; |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 298 | } else if (feature == null) { |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 299 | log.warn("Feature {} not found", name); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 300 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 301 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 302 | return changed; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 305 | // Uninstalls all features that define the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 306 | private synchronized boolean uninstallAppFeatures(Application app) throws Exception { |
| 307 | boolean changed = false; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 308 | deactivateHooks.removeAll(app.id().name()).forEach(hook -> invokeHook(hook, app.id())); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 309 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 310 | Feature feature = featuresService.getFeature(name); |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 311 | if (feature != null && featuresService.isInstalled(feature)) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 312 | featuresService.uninstallFeature(name); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 313 | changed = true; |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 314 | } else if (feature == null) { |
| 315 | log.warn("Feature {} not found", name); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 316 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 317 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 318 | return changed; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 321 | // Invokes the specified function, if not null. |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 322 | @java.lang.SuppressWarnings("squid:S1217") |
| 323 | // We really do mean to call run() |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 324 | private void invokeHook(Runnable hook, ApplicationId appId) { |
| 325 | if (hook != null) { |
| 326 | try { |
| 327 | hook.run(); |
| 328 | } catch (Exception e) { |
| 329 | log.warn("Deactivate hook for application {} encountered an error", |
| 330 | appId.name(), e); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public Set<Application> getRegisteredApplications() { |
| 337 | ImmutableSet.Builder<Application> builder = ImmutableSet.builder(); |
| 338 | ObjectMapper mapper = new ObjectMapper(); |
Arnav Jain | a359b18 | 2019-07-19 11:00:49 -0700 | [diff] [blame] | 339 | String vers = versionService.version().toString(); |
| 340 | vers = vers.substring(0, vers.lastIndexOf(".") + 2); |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 341 | // Get input stream from the URL |
| 342 | try { |
Arnav Jain | a359b18 | 2019-07-19 11:00:49 -0700 | [diff] [blame] | 343 | URL serverUrl = new URL(APP_REGISTRY_URL + "?onosVersion=" + vers); |
| 344 | HttpURLConnection serverHttp = (HttpURLConnection) serverUrl.openConnection(); |
| 345 | InputStream githubStream = serverHttp.getInputStream(); |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 346 | |
| 347 | // Read input stream into an ArrayNode |
| 348 | ArrayNode rootTree = (ArrayNode) mapper.readTree(githubStream); |
| 349 | |
| 350 | // Iterate over the array node for each object add each version as application object to the set |
| 351 | rootTree.forEach(n -> { |
| 352 | mapObject(builder, (ObjectNode) n); |
| 353 | }); |
| 354 | |
| 355 | //Iterate through Builder to remove unnecessary apps |
| 356 | Set<Application> apps = builder.build(); |
| 357 | |
| 358 | return apps; |
| 359 | } catch (MalformedURLException e) { |
Arnav Jain | a359b18 | 2019-07-19 11:00:49 -0700 | [diff] [blame] | 360 | throw new IllegalStateException("Bad URL: " + APP_REGISTRY_URL + "?onosVersion=" + vers, e); |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 361 | } catch (IOException e) { |
Arnav Jain | a359b18 | 2019-07-19 11:00:49 -0700 | [diff] [blame] | 362 | throw new IllegalStateException("Unable to fetch URL: " + APP_REGISTRY_URL + "?onosVersion=" + vers, e); |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | private void mapObject(ImmutableSet.Builder<Application> apps, ObjectNode node) { |
| 367 | String appIDs = node.get("id").asText(); |
| 368 | ApplicationId appID = new DefaultApplicationId(1, appIDs); |
| 369 | String title = node.get("title").asText(); |
| 370 | String readme = node.get("readme").asText(); |
| 371 | String category = node.get("category").asText(); |
| 372 | String url = node.get("url").asText(); |
| 373 | String origin = node.get("maintainer").asText(); |
| 374 | JsonNode it = node.get("versions"); |
| 375 | Iterator iterate = it.iterator(); |
| 376 | while (iterate.hasNext()) { |
| 377 | DefaultApplication.Builder app = new DefaultApplication.Builder(); |
| 378 | JsonNode jsonNode = (JsonNode) iterate.next(); |
| 379 | URL imageUrl = null; |
| 380 | try { |
| 381 | imageUrl = new URL(jsonNode.get("oarURL").asText()); |
| 382 | } catch (MalformedURLException e) { |
| 383 | e.printStackTrace(); |
| 384 | } |
| 385 | Version version1 = Version.version(jsonNode.get("onosVersion").asText()); |
| 386 | app.withImageUrl(imageUrl) |
| 387 | .withAppId(new DefaultApplicationId(1, node.get("id").asText())) |
| 388 | .withVersion(version1) |
| 389 | .withAppId(appID) |
| 390 | .withReadme(readme) |
| 391 | .withDescription(readme) |
| 392 | .withTitle(title) |
| 393 | .withFeatures(ImmutableList.of("none")) |
| 394 | .withCategory(category) |
| 395 | .withUrl(url) |
| 396 | .withOrigin(origin); |
| 397 | apps.add(app.build()); |
| 398 | } |
| 399 | } |
| 400 | } |