Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 18 | import java.io.InputStream; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 28 | import org.apache.karaf.features.Feature; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import org.apache.karaf.features.FeaturesService; |
| 30 | import org.onosproject.app.ApplicationAdminService; |
| 31 | import org.onosproject.app.ApplicationEvent; |
| 32 | import org.onosproject.app.ApplicationListener; |
| 33 | import org.onosproject.app.ApplicationService; |
| 34 | import org.onosproject.app.ApplicationState; |
| 35 | import org.onosproject.app.ApplicationStore; |
| 36 | import org.onosproject.app.ApplicationStoreDelegate; |
| 37 | import org.onosproject.core.Application; |
| 38 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 39 | import org.onosproject.event.AbstractListenerManager; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 40 | import org.onosproject.security.Permission; |
| 41 | import org.onosproject.security.SecurityUtil; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 44 | import com.google.common.collect.Maps; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 47 | import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED; |
| 48 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
| 49 | import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED; |
| 50 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 51 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 52 | import static org.onosproject.security.AppPermission.Type.APP_READ; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * Implementation of the application management service. |
| 57 | */ |
| 58 | @Component(immediate = true) |
| 59 | @Service |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 60 | public class ApplicationManager |
| 61 | extends AbstractListenerManager<ApplicationEvent, ApplicationListener> |
| 62 | implements ApplicationService, ApplicationAdminService { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 63 | |
| 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
| 66 | private static final String APP_ID_NULL = "Application ID cannot be null"; |
| 67 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 68 | private final ApplicationStoreDelegate delegate = new InternalStoreDelegate(); |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected ApplicationStore store; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected FeaturesService featuresService; |
| 75 | |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 76 | private boolean initializing; |
| 77 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 78 | // Application supplied hooks for pre-activation processing. |
| 79 | private final Map<String, Runnable> deactivateHooks = Maps.newConcurrentMap(); |
| 80 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 81 | @Activate |
| 82 | public void activate() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 83 | eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry); |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 84 | |
| 85 | initializing = true; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 86 | store.setDelegate(delegate); |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 87 | initializing = false; |
| 88 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 89 | log.info("Started"); |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | public void deactivate() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 94 | eventDispatcher.removeSink(ApplicationEvent.class); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 95 | store.unsetDelegate(delegate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Set<Application> getApplications() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 101 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 102 | return store.getApplications(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public ApplicationId getId(String name) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 107 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 108 | checkNotNull(name, "Name cannot be null"); |
| 109 | return store.getId(name); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Application getApplication(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 114 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 115 | checkNotNull(appId, APP_ID_NULL); |
| 116 | return store.getApplication(appId); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public ApplicationState getState(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 121 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 122 | checkNotNull(appId, APP_ID_NULL); |
| 123 | return store.getState(appId); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Set<Permission> getPermissions(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 128 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 129 | checkNotNull(appId, APP_ID_NULL); |
| 130 | return store.getPermissions(appId); |
| 131 | } |
| 132 | |
| 133 | @Override |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 134 | public void registerDeactivateHook(ApplicationId appId, Runnable hook) { |
| 135 | checkPermission(APP_READ); |
| 136 | checkNotNull(appId, APP_ID_NULL); |
| 137 | checkNotNull(hook, "Hook cannot be null"); |
| 138 | deactivateHooks.put(appId.name(), hook); |
| 139 | } |
| 140 | |
| 141 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 142 | public Application install(InputStream appDescStream) { |
| 143 | checkNotNull(appDescStream, "Application archive stream cannot be null"); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 144 | Application app = store.create(appDescStream); |
| 145 | SecurityUtil.register(app.id()); |
| 146 | return app; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void uninstall(ApplicationId appId) { |
| 151 | checkNotNull(appId, APP_ID_NULL); |
| 152 | try { |
| 153 | store.remove(appId); |
| 154 | } catch (Exception e) { |
| 155 | log.warn("Unable to purge application directory for {}", appId.name()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void activate(ApplicationId appId) { |
| 161 | checkNotNull(appId, APP_ID_NULL); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 162 | if (!SecurityUtil.isAppSecured(appId)) { |
| 163 | return; |
| 164 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 165 | store.activate(appId); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void deactivate(ApplicationId appId) { |
| 170 | checkNotNull(appId, APP_ID_NULL); |
| 171 | store.deactivate(appId); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 176 | checkNotNull(appId, APP_ID_NULL); |
| 177 | checkNotNull(permissions, "Permissions cannot be null"); |
| 178 | store.setPermissions(appId, permissions); |
| 179 | } |
| 180 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 181 | private class InternalStoreDelegate implements ApplicationStoreDelegate { |
| 182 | @Override |
| 183 | public void notify(ApplicationEvent event) { |
| 184 | ApplicationEvent.Type type = event.type(); |
| 185 | Application app = event.subject(); |
| 186 | try { |
| 187 | if (type == APP_ACTIVATED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 188 | if (installAppFeatures(app)) { |
| 189 | log.info("Application {} has been activated", app.id().name()); |
| 190 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 191 | |
| 192 | } else if (type == APP_DEACTIVATED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 193 | if (uninstallAppFeatures(app)) { |
| 194 | log.info("Application {} has been deactivated", app.id().name()); |
| 195 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 196 | |
| 197 | } else if (type == APP_INSTALLED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 198 | if (installAppArtifacts(app)) { |
| 199 | log.info("Application {} has been installed", app.id().name()); |
| 200 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 201 | |
| 202 | } else if (type == APP_UNINSTALLED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 203 | if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) { |
| 204 | log.info("Application {} has been uninstalled", app.id().name()); |
| 205 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 206 | |
| 207 | } |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 208 | post(event); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 209 | |
| 210 | } catch (Exception e) { |
| 211 | log.warn("Unable to perform operation on application " + app.id().name(), e); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Thomas Vachuska | 0d6af8d | 2015-03-23 15:54:47 -0700 | [diff] [blame] | 216 | // The following methods are fully synchronized to guard against remote vs. |
| 217 | // locally induced feature service interactions. |
| 218 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 219 | // Installs all feature repositories required by the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 220 | private synchronized boolean installAppArtifacts(Application app) throws Exception { |
| 221 | if (app.featuresRepo().isPresent() && |
| 222 | featuresService.getRepository(app.featuresRepo().get()) == null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 223 | featuresService.addRepository(app.featuresRepo().get()); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 224 | return true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 225 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 226 | return false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 229 | // Uninstalls all the feature repositories required by the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 230 | private synchronized boolean uninstallAppArtifacts(Application app) throws Exception { |
| 231 | if (app.featuresRepo().isPresent() && |
| 232 | featuresService.getRepository(app.featuresRepo().get()) != null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 233 | featuresService.removeRepository(app.featuresRepo().get()); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 234 | return true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 235 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 236 | return false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 239 | // Installs all features that define the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 240 | private synchronized boolean installAppFeatures(Application app) throws Exception { |
| 241 | boolean changed = false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 242 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 243 | Feature feature = featuresService.getFeature(name); |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 244 | |
| 245 | // If we see an attempt at activation of a non-existent feature |
| 246 | // attempt to install the app artifacts first and then retry. |
| 247 | // This can be triggered by a race condition between different ONOS |
| 248 | // instances "installing" the apps from disk at their own pace. |
| 249 | // Perhaps there is a more elegant solution to be explored in the |
| 250 | // future. |
| 251 | if (feature == null) { |
| 252 | installAppArtifacts(app); |
| 253 | feature = featuresService.getFeature(name); |
| 254 | } |
| 255 | |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 256 | if (feature != null && !featuresService.isInstalled(feature)) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 257 | featuresService.installFeature(name); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 258 | changed = true; |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 259 | } else if (feature == null) { |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 260 | log.warn("Feature {} not found", name); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 261 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 262 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 263 | return changed; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 266 | // Uninstalls all features that define the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 267 | private synchronized boolean uninstallAppFeatures(Application app) throws Exception { |
| 268 | boolean changed = false; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 269 | invokeHook(deactivateHooks.get(app.id().name()), app.id()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 270 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 271 | Feature feature = featuresService.getFeature(name); |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 272 | if (feature != null && featuresService.isInstalled(feature)) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 273 | featuresService.uninstallFeature(name); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 274 | changed = true; |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 275 | } else if (feature == null) { |
| 276 | log.warn("Feature {} not found", name); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 277 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 278 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 279 | return changed; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 282 | // Invokes the specified function, if not null. |
Ray Milkey | aef4585 | 2016-01-11 17:13:19 -0800 | [diff] [blame] | 283 | @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run() |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 284 | private void invokeHook(Runnable hook, ApplicationId appId) { |
| 285 | if (hook != null) { |
| 286 | try { |
| 287 | hook.run(); |
| 288 | } catch (Exception e) { |
| 289 | log.warn("Deactivate hook for application {} encountered an error", |
| 290 | appId.name(), e); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 295 | } |