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; |
| 33 | import org.onosproject.core.Application; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.Permission; |
| 36 | import org.onosproject.event.AbstractListenerRegistry; |
| 37 | import org.onosproject.event.EventDeliveryService; |
| 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.*; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Implementation of the application management service. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | @Service |
| 52 | public class ApplicationManager implements ApplicationService, ApplicationAdminService { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | private static final String APP_ID_NULL = "Application ID cannot be null"; |
| 57 | |
| 58 | protected final AbstractListenerRegistry<ApplicationEvent, ApplicationListener> |
| 59 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 60 | |
| 61 | private final ApplicationStoreDelegate delegate = new InternalStoreDelegate(); |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected ApplicationStore store; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected FeaturesService featuresService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected EventDeliveryService eventDispatcher; |
| 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
| 74 | store.setDelegate(delegate); |
| 75 | eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry); |
| 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
| 81 | store.unsetDelegate(delegate); |
| 82 | eventDispatcher.removeSink(ApplicationEvent.class); |
| 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Set<Application> getApplications() { |
| 88 | return store.getApplications(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public ApplicationId getId(String name) { |
| 93 | checkNotNull(name, "Name cannot be null"); |
| 94 | return store.getId(name); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Application getApplication(ApplicationId appId) { |
| 99 | checkNotNull(appId, APP_ID_NULL); |
| 100 | return store.getApplication(appId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public ApplicationState getState(ApplicationId appId) { |
| 105 | checkNotNull(appId, APP_ID_NULL); |
| 106 | return store.getState(appId); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Set<Permission> getPermissions(ApplicationId appId) { |
| 111 | checkNotNull(appId, APP_ID_NULL); |
| 112 | return store.getPermissions(appId); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Application install(InputStream appDescStream) { |
| 117 | checkNotNull(appDescStream, "Application archive stream cannot be null"); |
| 118 | return store.create(appDescStream); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void uninstall(ApplicationId appId) { |
| 123 | checkNotNull(appId, APP_ID_NULL); |
| 124 | try { |
| 125 | store.remove(appId); |
| 126 | } catch (Exception e) { |
| 127 | log.warn("Unable to purge application directory for {}", appId.name()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void activate(ApplicationId appId) { |
| 133 | checkNotNull(appId, APP_ID_NULL); |
| 134 | store.activate(appId); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void deactivate(ApplicationId appId) { |
| 139 | checkNotNull(appId, APP_ID_NULL); |
| 140 | store.deactivate(appId); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 145 | checkNotNull(appId, APP_ID_NULL); |
| 146 | checkNotNull(permissions, "Permissions cannot be null"); |
| 147 | store.setPermissions(appId, permissions); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void addListener(ApplicationListener listener) { |
| 152 | listenerRegistry.addListener(listener); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void removeListener(ApplicationListener listener) { |
| 157 | listenerRegistry.removeListener(listener); |
| 158 | } |
| 159 | |
| 160 | private class InternalStoreDelegate implements ApplicationStoreDelegate { |
| 161 | @Override |
| 162 | public void notify(ApplicationEvent event) { |
| 163 | ApplicationEvent.Type type = event.type(); |
| 164 | Application app = event.subject(); |
| 165 | try { |
| 166 | if (type == APP_ACTIVATED) { |
| 167 | installAppFeatures(app); |
| 168 | log.info("Application {} has been activated", app.id().name()); |
| 169 | |
| 170 | } else if (type == APP_DEACTIVATED) { |
| 171 | uninstallAppFeatures(app); |
| 172 | log.info("Application {} has been deactivated", app.id().name()); |
| 173 | |
| 174 | } else if (type == APP_INSTALLED) { |
| 175 | installAppArtifacts(app); |
| 176 | log.info("Application {} has been installed", app.id().name()); |
| 177 | |
| 178 | } else if (type == APP_UNINSTALLED) { |
| 179 | uninstallAppFeatures(app); |
| 180 | uninstallAppArtifacts(app); |
| 181 | log.info("Application {} has been uninstalled", app.id().name()); |
| 182 | |
| 183 | } |
| 184 | eventDispatcher.post(event); |
| 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 | |
| 195 | private synchronized void installAppArtifacts(Application app) throws Exception { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 196 | if (app.featuresRepo().isPresent()) { |
| 197 | featuresService.addRepository(app.featuresRepo().get()); |
| 198 | } |
| 199 | } |
| 200 | |
Thomas Vachuska | 0d6af8d | 2015-03-23 15:54:47 -0700 | [diff] [blame] | 201 | private synchronized void uninstallAppArtifacts(Application app) throws Exception { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 202 | if (app.featuresRepo().isPresent()) { |
| 203 | featuresService.removeRepository(app.featuresRepo().get()); |
| 204 | } |
| 205 | } |
| 206 | |
Thomas Vachuska | 0d6af8d | 2015-03-23 15:54:47 -0700 | [diff] [blame] | 207 | private synchronized void installAppFeatures(Application app) throws Exception { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 208 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 209 | Feature feature = featuresService.getFeature(name); |
| 210 | if (!featuresService.isInstalled(feature)) { |
| 211 | featuresService.installFeature(name); |
| 212 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Thomas Vachuska | 0d6af8d | 2015-03-23 15:54:47 -0700 | [diff] [blame] | 216 | private synchronized void uninstallAppFeatures(Application app) throws Exception { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 217 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 218 | Feature feature = featuresService.getFeature(name); |
| 219 | if (featuresService.isInstalled(feature)) { |
| 220 | featuresService.uninstallFeature(name); |
| 221 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | } |