blob: e1e4b675ce7f5791d180a36ccf3c565d1a6ca9b6 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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
Simon Huntafae2f72016-03-04 21:18:23 -080052public class SimpleApplicationStore extends ApplicationArchive
53 implements ApplicationStore {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080054
55 private final Logger log = getLogger(getClass());
56
57 // App inventory & states
Simon Huntafae2f72016-03-04 21:18:23 -080058 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 Vachuska02aeb032015-01-06 22:36:30 -080064
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 Huntafae2f72016-03-04 21:18:23 -080079 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 Vachuska02aeb032015-01-06 22:36:30 -080093 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 Huntafae2f72016-03-04 21:18:23 -0800132 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 Vachuska02aeb032015-01-06 22:36:30 -0800146 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}