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