blob: c904455bcd7921d39c9bf322dee4a3d4a5e7a9cb [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
sangyun-han6d33e802016-08-05 13:36:33 +090032import org.onosproject.app.ApplicationIdStore;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080033import 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 =
Ray Milkey47c95412017-09-15 10:40:48 -070079 DefaultApplication
80 .builder(appDesc)
81 .withAppId(appId)
82 .build();
83
Thomas Vachuska02aeb032015-01-06 22:36:30 -080084 apps.put(appId, app);
85 states.put(appId, isActive(name) ? INSTALLED : ACTIVE);
86 // load app permissions
87 }
88 }
89
90 @Deactivate
91 public void deactivate() {
92 apps.clear();
93 states.clear();
94 permissions.clear();
95 log.info("Stopped");
96 }
97
98 @Override
99 public Set<Application> getApplications() {
100 return ImmutableSet.copyOf(apps.values());
101 }
102
103 @Override
104 public ApplicationId getId(String name) {
105 return idStore.getAppId(name);
106 }
107
108 @Override
109 public Application getApplication(ApplicationId appId) {
110 return apps.get(appId);
111 }
112
113 @Override
114 public ApplicationState getState(ApplicationId appId) {
115 return states.get(appId);
116 }
117
118 @Override
119 public Application create(InputStream appDescStream) {
120 ApplicationDescription appDesc = saveApplication(appDescStream);
121 ApplicationId appId = idStore.registerApplication(appDesc.name());
122 DefaultApplication app =
Ray Milkey47c95412017-09-15 10:40:48 -0700123 DefaultApplication
124 .builder(appDesc)
125 .withAppId(appId)
126 .build();
127
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800128 apps.put(appId, app);
129 states.put(appId, INSTALLED);
130 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
131 return app;
132 }
133
134 @Override
135 public void remove(ApplicationId appId) {
136 Application app = apps.remove(appId);
137 if (app != null) {
138 states.remove(appId);
139 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
140 purgeApplication(app.id().name());
141 }
142 }
143
144 @Override
145 public void activate(ApplicationId appId) {
146 Application app = apps.get(appId);
147 if (app != null) {
148 setActive(appId.name());
149 states.put(appId, ACTIVE);
150 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
151 }
152 }
153
154 @Override
155 public void deactivate(ApplicationId appId) {
156 Application app = apps.get(appId);
157 if (app != null) {
158 clearActive(appId.name());
159 states.put(appId, INSTALLED);
160 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
161 }
162 }
163
164 @Override
165 public Set<Permission> getPermissions(ApplicationId appId) {
166 return permissions.get(appId);
167 }
168
169 @Override
170 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
171 Application app = getApplication(appId);
172 if (app != null) {
173 this.permissions.put(appId, permissions);
174 delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app));
175 }
176 }
177}