blob: 98c18b40ebdc14cc2407ab5963dfb606c1d4f5c7 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
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 Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080017
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.app.ApplicationDescription;
26import org.onosproject.app.ApplicationEvent;
27import org.onosproject.app.ApplicationState;
28import org.onosproject.app.ApplicationStore;
29import org.onosproject.common.app.ApplicationArchive;
30import org.onosproject.core.Application;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.core.ApplicationIdStore;
33import org.onosproject.core.DefaultApplication;
Changhoon Yoonb856b812015-08-10 03:47:19 +090034import org.onosproject.security.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080035import org.slf4j.Logger;
36
37import java.io.InputStream;
38import java.util.Set;
39import java.util.concurrent.ConcurrentHashMap;
40import java.util.concurrent.ConcurrentMap;
41
42import static org.onosproject.app.ApplicationEvent.Type.*;
43import static org.onosproject.app.ApplicationState.ACTIVE;
44import static org.onosproject.app.ApplicationState.INSTALLED;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Manages inventory of network control applications.
49 */
50@Component(immediate = true)
51@Service
52public 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(),
Jian Lic35415d2016-01-14 17:22:31 -080077 appDesc.category(), appDesc.url(),
78 appDesc.readme(), appDesc.icon(),
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090079 appDesc.role(), appDesc.permissions(),
Thomas Vachuska761f0042015-11-11 19:10:17 -080080 appDesc.featuresRepo(), appDesc.features(),
81 appDesc.requiredApps());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082 apps.put(appId, app);
83 states.put(appId, isActive(name) ? INSTALLED : ACTIVE);
84 // load app permissions
85 }
86 }
87
88 @Deactivate
89 public void deactivate() {
90 apps.clear();
91 states.clear();
92 permissions.clear();
93 log.info("Stopped");
94 }
95
96 @Override
97 public Set<Application> getApplications() {
98 return ImmutableSet.copyOf(apps.values());
99 }
100
101 @Override
102 public ApplicationId getId(String name) {
103 return idStore.getAppId(name);
104 }
105
106 @Override
107 public Application getApplication(ApplicationId appId) {
108 return apps.get(appId);
109 }
110
111 @Override
112 public ApplicationState getState(ApplicationId appId) {
113 return states.get(appId);
114 }
115
116 @Override
117 public Application create(InputStream appDescStream) {
118 ApplicationDescription appDesc = saveApplication(appDescStream);
119 ApplicationId appId = idStore.registerApplication(appDesc.name());
120 DefaultApplication app =
121 new DefaultApplication(appId, appDesc.version(), appDesc.description(),
Jian Lic35415d2016-01-14 17:22:31 -0800122 appDesc.origin(), appDesc.category(), appDesc.url(),
123 appDesc.readme(), appDesc.icon(),
124 appDesc.role(), appDesc.permissions(),
Thomas Vachuska761f0042015-11-11 19:10:17 -0800125 appDesc.featuresRepo(), appDesc.features(),
126 appDesc.requiredApps());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800127 apps.put(appId, app);
128 states.put(appId, INSTALLED);
129 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
130 return app;
131 }
132
133 @Override
134 public void remove(ApplicationId appId) {
135 Application app = apps.remove(appId);
136 if (app != null) {
137 states.remove(appId);
138 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
139 purgeApplication(app.id().name());
140 }
141 }
142
143 @Override
144 public void activate(ApplicationId appId) {
145 Application app = apps.get(appId);
146 if (app != null) {
147 setActive(appId.name());
148 states.put(appId, ACTIVE);
149 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
150 }
151 }
152
153 @Override
154 public void deactivate(ApplicationId appId) {
155 Application app = apps.get(appId);
156 if (app != null) {
157 clearActive(appId.name());
158 states.put(appId, INSTALLED);
159 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
160 }
161 }
162
163 @Override
164 public Set<Permission> getPermissions(ApplicationId appId) {
165 return permissions.get(appId);
166 }
167
168 @Override
169 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
170 Application app = getApplication(appId);
171 if (app != null) {
172 this.permissions.put(appId, permissions);
173 delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app));
174 }
175 }
176}