blob: ea9a773e63b97ab62b01e33b326b01b3616daf9d [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(),
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090077 appDesc.role(), appDesc.permissions(),
Thomas Vachuska02aeb032015-01-06 22:36:30 -080078 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 Yoonbdeb88a2015-05-12 20:35:31 +0900119 appDesc.origin(), appDesc.role(), appDesc.permissions(),
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800120 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}