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