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