blob: 1bb3c4d5c34557d3938e02e9b614d3e2764f4361 [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;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import org.onosproject.app.ApplicationDescription;
20import org.onosproject.app.ApplicationEvent;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.onosproject.app.ApplicationIdStore;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080022import org.onosproject.app.ApplicationState;
23import org.onosproject.app.ApplicationStore;
24import org.onosproject.common.app.ApplicationArchive;
25import org.onosproject.core.Application;
26import org.onosproject.core.ApplicationId;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080027import org.onosproject.core.DefaultApplication;
Changhoon Yoonb856b812015-08-10 03:47:19 +090028import org.onosproject.security.Permission;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import org.slf4j.Logger;
35
36import java.io.InputStream;
37import java.util.Set;
38import java.util.concurrent.ConcurrentHashMap;
39import java.util.concurrent.ConcurrentMap;
40
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED;
42import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
43import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
44import static org.onosproject.app.ApplicationEvent.Type.APP_PERMISSIONS_CHANGED;
45import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046import static org.onosproject.app.ApplicationState.ACTIVE;
47import static org.onosproject.app.ApplicationState.INSTALLED;
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Manages inventory of network control applications.
52 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053@Component(immediate = true, service = ApplicationStore.class)
Simon Huntafae2f72016-03-04 21:18:23 -080054public class SimpleApplicationStore extends ApplicationArchive
55 implements ApplicationStore {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080056
57 private final Logger log = getLogger(getClass());
58
59 // App inventory & states
Simon Huntafae2f72016-03-04 21:18:23 -080060 private final ConcurrentMap<ApplicationId, DefaultApplication> apps =
61 new ConcurrentHashMap<>();
62 private final ConcurrentMap<ApplicationId, ApplicationState> states =
63 new ConcurrentHashMap<>();
64 private final ConcurrentMap<ApplicationId, Set<Permission>> permissions =
65 new ConcurrentHashMap<>();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 protected ApplicationIdStore idStore;
69
70 @Activate
71 public void activate() {
72 loadFromDisk();
73 log.info("Started");
74 }
75
76 private void loadFromDisk() {
77 for (String name : getApplicationNames()) {
78 ApplicationId appId = idStore.registerApplication(name);
79 ApplicationDescription appDesc = getApplicationDescription(name);
80 DefaultApplication app =
Ray Milkey47c95412017-09-15 10:40:48 -070081 DefaultApplication
82 .builder(appDesc)
83 .withAppId(appId)
84 .build();
85
Thomas Vachuska02aeb032015-01-06 22:36:30 -080086 apps.put(appId, app);
87 states.put(appId, isActive(name) ? INSTALLED : ACTIVE);
88 // load app permissions
89 }
90 }
91
92 @Deactivate
93 public void deactivate() {
94 apps.clear();
95 states.clear();
96 permissions.clear();
97 log.info("Stopped");
98 }
99
100 @Override
101 public Set<Application> getApplications() {
102 return ImmutableSet.copyOf(apps.values());
103 }
104
105 @Override
106 public ApplicationId getId(String name) {
107 return idStore.getAppId(name);
108 }
109
110 @Override
111 public Application getApplication(ApplicationId appId) {
112 return apps.get(appId);
113 }
114
115 @Override
116 public ApplicationState getState(ApplicationId appId) {
117 return states.get(appId);
118 }
119
120 @Override
121 public Application create(InputStream appDescStream) {
122 ApplicationDescription appDesc = saveApplication(appDescStream);
123 ApplicationId appId = idStore.registerApplication(appDesc.name());
124 DefaultApplication app =
Ray Milkey47c95412017-09-15 10:40:48 -0700125 DefaultApplication
126 .builder(appDesc)
127 .withAppId(appId)
128 .build();
129
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800130 apps.put(appId, app);
131 states.put(appId, INSTALLED);
132 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
133 return app;
134 }
135
136 @Override
137 public void remove(ApplicationId appId) {
138 Application app = apps.remove(appId);
139 if (app != null) {
140 states.remove(appId);
141 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
142 purgeApplication(app.id().name());
143 }
144 }
145
146 @Override
147 public void activate(ApplicationId appId) {
148 Application app = apps.get(appId);
149 if (app != null) {
150 setActive(appId.name());
151 states.put(appId, ACTIVE);
152 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
153 }
154 }
155
156 @Override
157 public void deactivate(ApplicationId appId) {
158 Application app = apps.get(appId);
159 if (app != null) {
160 clearActive(appId.name());
161 states.put(appId, INSTALLED);
162 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
163 }
164 }
165
166 @Override
167 public Set<Permission> getPermissions(ApplicationId appId) {
168 return permissions.get(appId);
169 }
170
171 @Override
172 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
173 Application app = getApplication(appId);
174 if (app != null) {
175 this.permissions.put(appId, permissions);
176 delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app));
177 }
178 }
179}