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 | */ |
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 |
| 52 | public class SimpleApplicationStore extends ApplicationArchive implements ApplicationStore { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | // App inventory & states |
| 57 | private final ConcurrentMap<ApplicationId, DefaultApplication> apps = new ConcurrentHashMap<>(); |
| 58 | private final ConcurrentMap<ApplicationId, ApplicationState> states = new ConcurrentHashMap<>(); |
| 59 | private final ConcurrentMap<ApplicationId, Set<Permission>> permissions = new ConcurrentHashMap<>(); |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected ApplicationIdStore idStore; |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | loadFromDisk(); |
| 67 | log.info("Started"); |
| 68 | } |
| 69 | |
| 70 | private void loadFromDisk() { |
| 71 | for (String name : getApplicationNames()) { |
| 72 | ApplicationId appId = idStore.registerApplication(name); |
| 73 | ApplicationDescription appDesc = getApplicationDescription(name); |
| 74 | DefaultApplication app = |
| 75 | new DefaultApplication(appId, appDesc.version(), |
| 76 | appDesc.description(), appDesc.origin(), |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 77 | appDesc.role(), appDesc.permissions(), |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | appDesc.featuresRepo(), appDesc.features()); |
| 79 | apps.put(appId, app); |
| 80 | states.put(appId, isActive(name) ? INSTALLED : ACTIVE); |
| 81 | // load app permissions |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
| 87 | apps.clear(); |
| 88 | states.clear(); |
| 89 | permissions.clear(); |
| 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Set<Application> getApplications() { |
| 95 | return ImmutableSet.copyOf(apps.values()); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public ApplicationId getId(String name) { |
| 100 | return idStore.getAppId(name); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Application getApplication(ApplicationId appId) { |
| 105 | return apps.get(appId); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public ApplicationState getState(ApplicationId appId) { |
| 110 | return states.get(appId); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Application create(InputStream appDescStream) { |
| 115 | ApplicationDescription appDesc = saveApplication(appDescStream); |
| 116 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
| 117 | DefaultApplication app = |
| 118 | new DefaultApplication(appId, appDesc.version(), appDesc.description(), |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 119 | appDesc.origin(), appDesc.role(), appDesc.permissions(), |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 120 | appDesc.featuresRepo(), appDesc.features()); |
| 121 | apps.put(appId, app); |
| 122 | states.put(appId, INSTALLED); |
| 123 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 124 | return app; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void remove(ApplicationId appId) { |
| 129 | Application app = apps.remove(appId); |
| 130 | if (app != null) { |
| 131 | states.remove(appId); |
| 132 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 133 | purgeApplication(app.id().name()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void activate(ApplicationId appId) { |
| 139 | Application app = apps.get(appId); |
| 140 | if (app != null) { |
| 141 | setActive(appId.name()); |
| 142 | states.put(appId, ACTIVE); |
| 143 | delegate.notify(new ApplicationEvent(APP_ACTIVATED, app)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void deactivate(ApplicationId appId) { |
| 149 | Application app = apps.get(appId); |
| 150 | if (app != null) { |
| 151 | clearActive(appId.name()); |
| 152 | states.put(appId, INSTALLED); |
| 153 | delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Set<Permission> getPermissions(ApplicationId appId) { |
| 159 | return permissions.get(appId); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 164 | Application app = getApplication(appId); |
| 165 | if (app != null) { |
| 166 | this.permissions.put(appId, permissions); |
| 167 | delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app)); |
| 168 | } |
| 169 | } |
| 170 | } |