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