Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | import org.onosproject.app.ApplicationDescription; |
| 20 | import org.onosproject.app.ApplicationEvent; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.onosproject.app.ApplicationIdStore; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationState; |
| 23 | import org.onosproject.app.ApplicationStore; |
| 24 | import org.onosproject.common.app.ApplicationArchive; |
| 25 | import org.onosproject.core.Application; |
| 26 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 27 | import org.onosproject.core.DefaultApplication; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 28 | import org.onosproject.security.Permission; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.io.InputStream; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ConcurrentHashMap; |
| 39 | import java.util.concurrent.ConcurrentMap; |
| 40 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED; |
| 42 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
| 43 | import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED; |
| 44 | import static org.onosproject.app.ApplicationEvent.Type.APP_PERMISSIONS_CHANGED; |
| 45 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 46 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 47 | import static org.onosproject.app.ApplicationState.INSTALLED; |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Manages inventory of network control applications. |
| 52 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Component(immediate = true, service = ApplicationStore.class) |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 54 | public class SimpleApplicationStore extends ApplicationArchive |
| 55 | implements ApplicationStore { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 56 | |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | // App inventory & states |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 60 | private final ConcurrentMap<ApplicationId, DefaultApplication> apps = |
| 61 | new ConcurrentHashMap<>(); |
| 62 | private final ConcurrentMap<ApplicationId, ApplicationState> states = |
| 63 | new ConcurrentHashMap<>(); |
| 64 | private final ConcurrentMap<ApplicationId, Set<Permission>> permissions = |
| 65 | new ConcurrentHashMap<>(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 68 | protected ApplicationIdStore idStore; |
| 69 | |
| 70 | @Activate |
| 71 | public void activate() { |
| 72 | loadFromDisk(); |
| 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | private void loadFromDisk() { |
| 77 | for (String name : getApplicationNames()) { |
| 78 | ApplicationId appId = idStore.registerApplication(name); |
| 79 | ApplicationDescription appDesc = getApplicationDescription(name); |
| 80 | DefaultApplication app = |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 81 | DefaultApplication |
| 82 | .builder(appDesc) |
| 83 | .withAppId(appId) |
| 84 | .build(); |
| 85 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 86 | apps.put(appId, app); |
| 87 | states.put(appId, isActive(name) ? INSTALLED : ACTIVE); |
| 88 | // load app permissions |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | public void deactivate() { |
| 94 | apps.clear(); |
| 95 | states.clear(); |
| 96 | permissions.clear(); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Set<Application> getApplications() { |
| 102 | return ImmutableSet.copyOf(apps.values()); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public ApplicationId getId(String name) { |
| 107 | return idStore.getAppId(name); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Application getApplication(ApplicationId appId) { |
| 112 | return apps.get(appId); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public ApplicationState getState(ApplicationId appId) { |
| 117 | return states.get(appId); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Application create(InputStream appDescStream) { |
| 122 | ApplicationDescription appDesc = saveApplication(appDescStream); |
| 123 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
| 124 | DefaultApplication app = |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 125 | DefaultApplication |
| 126 | .builder(appDesc) |
| 127 | .withAppId(appId) |
| 128 | .build(); |
| 129 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 130 | apps.put(appId, app); |
| 131 | states.put(appId, INSTALLED); |
| 132 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 133 | return app; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void remove(ApplicationId appId) { |
| 138 | Application app = apps.remove(appId); |
| 139 | if (app != null) { |
| 140 | states.remove(appId); |
| 141 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 142 | purgeApplication(app.id().name()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void activate(ApplicationId appId) { |
| 148 | Application app = apps.get(appId); |
| 149 | if (app != null) { |
| 150 | setActive(appId.name()); |
| 151 | states.put(appId, ACTIVE); |
| 152 | delegate.notify(new ApplicationEvent(APP_ACTIVATED, app)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void deactivate(ApplicationId appId) { |
| 158 | Application app = apps.get(appId); |
| 159 | if (app != null) { |
| 160 | clearActive(appId.name()); |
| 161 | states.put(appId, INSTALLED); |
| 162 | delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Set<Permission> getPermissions(ApplicationId appId) { |
| 168 | return permissions.get(appId); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 173 | Application app = getApplication(appId); |
| 174 | if (app != null) { |
| 175 | this.permissions.put(appId, permissions); |
| 176 | delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app)); |
| 177 | } |
| 178 | } |
| 179 | } |