ONOS-542 Defining application subsystem interfaces & public constructs.
Change-Id: Iba0d2cb69dace5beee8a68def9918059ce755b5c
diff --git a/core/store/dist/src/main/java/org/onosproject/store/core/impl/DistributedApplicationIdStore.java b/core/store/dist/src/main/java/org/onosproject/store/core/impl/DistributedApplicationIdStore.java
index f55ea54..186fb4e 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/core/impl/DistributedApplicationIdStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/core/impl/DistributedApplicationIdStore.java
@@ -103,6 +103,11 @@
return appId;
}
+ @Override
+ public ApplicationId getAppId(String name) {
+ return appIdsByName.get(name);
+ }
+
private void primeAppIds() {
for (DefaultApplicationId appId : appIdsByName.values()) {
appIds.put(appId.id(), appId);
@@ -113,7 +118,7 @@
public ApplicationId registerApplication(String name) {
DefaultApplicationId appId = appIdsByName.get(name);
if (appId == null) {
- short id = (short) lastAppId.getAndIncrement();
+ int id = (int) lastAppId.getAndIncrement();
appId = putIfAbsent(appIdsByName, name,
new DefaultApplicationId(id, name));
}
diff --git a/core/store/pom.xml b/core/store/pom.xml
index 334832f..e5078b9 100644
--- a/core/store/pom.xml
+++ b/core/store/pom.xml
@@ -44,6 +44,14 @@
</dependency>
<dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-core-common</artifactId>
+ <version>${project.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
diff --git a/core/store/trivial/pom.xml b/core/store/trivial/pom.xml
index 5e82ee8..2842ead 100644
--- a/core/store/trivial/pom.xml
+++ b/core/store/trivial/pom.xml
@@ -38,6 +38,11 @@
</dependency>
<dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-core-common</artifactId>
+ </dependency>
+
+ <dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<classifier>tests</classifier>
diff --git a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationIdStore.java b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationIdStore.java
index 34abd44..d6a9310 100644
--- a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationIdStore.java
+++ b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationIdStore.java
@@ -51,6 +51,11 @@
}
@Override
+ public ApplicationId getAppId(String name) {
+ return appIdsByName.get(name);
+ }
+
+ @Override
public ApplicationId registerApplication(String name) {
DefaultApplicationId appId = appIdsByName.get(name);
if (appId == null) {
diff --git a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationStore.java b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationStore.java
new file mode 100644
index 0000000..cecf5fd
--- /dev/null
+++ b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleApplicationStore.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.store.trivial.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.app.ApplicationDescription;
+import org.onosproject.app.ApplicationEvent;
+import org.onosproject.app.ApplicationState;
+import org.onosproject.app.ApplicationStore;
+import org.onosproject.common.app.ApplicationArchive;
+import org.onosproject.core.Application;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.ApplicationIdStore;
+import org.onosproject.core.DefaultApplication;
+import org.onosproject.core.Permission;
+import org.slf4j.Logger;
+
+import java.io.InputStream;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import static org.onosproject.app.ApplicationEvent.Type.*;
+import static org.onosproject.app.ApplicationState.ACTIVE;
+import static org.onosproject.app.ApplicationState.INSTALLED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Manages inventory of network control applications.
+ */
+@Component(immediate = true)
+@Service
+public class SimpleApplicationStore extends ApplicationArchive implements ApplicationStore {
+
+ private final Logger log = getLogger(getClass());
+
+ // App inventory & states
+ private final ConcurrentMap<ApplicationId, DefaultApplication> apps = new ConcurrentHashMap<>();
+ private final ConcurrentMap<ApplicationId, ApplicationState> states = new ConcurrentHashMap<>();
+ private final ConcurrentMap<ApplicationId, Set<Permission>> permissions = new ConcurrentHashMap<>();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ApplicationIdStore idStore;
+
+ @Activate
+ public void activate() {
+ loadFromDisk();
+ log.info("Started");
+ }
+
+ private void loadFromDisk() {
+ for (String name : getApplicationNames()) {
+ ApplicationId appId = idStore.registerApplication(name);
+ ApplicationDescription appDesc = getApplicationDescription(name);
+ DefaultApplication app =
+ new DefaultApplication(appId, appDesc.version(),
+ appDesc.description(), appDesc.origin(),
+ appDesc.permissions(),
+ appDesc.featuresRepo(), appDesc.features());
+ apps.put(appId, app);
+ states.put(appId, isActive(name) ? INSTALLED : ACTIVE);
+ // load app permissions
+ }
+ }
+
+ @Deactivate
+ public void deactivate() {
+ apps.clear();
+ states.clear();
+ permissions.clear();
+ log.info("Stopped");
+ }
+
+ @Override
+ public Set<Application> getApplications() {
+ return ImmutableSet.copyOf(apps.values());
+ }
+
+ @Override
+ public ApplicationId getId(String name) {
+ return idStore.getAppId(name);
+ }
+
+ @Override
+ public Application getApplication(ApplicationId appId) {
+ return apps.get(appId);
+ }
+
+ @Override
+ public ApplicationState getState(ApplicationId appId) {
+ return states.get(appId);
+ }
+
+ @Override
+ public Application create(InputStream appDescStream) {
+ ApplicationDescription appDesc = saveApplication(appDescStream);
+ ApplicationId appId = idStore.registerApplication(appDesc.name());
+ DefaultApplication app =
+ new DefaultApplication(appId, appDesc.version(), appDesc.description(),
+ appDesc.origin(), appDesc.permissions(),
+ appDesc.featuresRepo(), appDesc.features());
+ apps.put(appId, app);
+ states.put(appId, INSTALLED);
+ delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
+ return app;
+ }
+
+ @Override
+ public void remove(ApplicationId appId) {
+ Application app = apps.remove(appId);
+ if (app != null) {
+ states.remove(appId);
+ delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
+ purgeApplication(app.id().name());
+ }
+ }
+
+ @Override
+ public void activate(ApplicationId appId) {
+ Application app = apps.get(appId);
+ if (app != null) {
+ setActive(appId.name());
+ states.put(appId, ACTIVE);
+ delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
+ }
+ }
+
+ @Override
+ public void deactivate(ApplicationId appId) {
+ Application app = apps.get(appId);
+ if (app != null) {
+ clearActive(appId.name());
+ states.put(appId, INSTALLED);
+ delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
+ }
+ }
+
+ @Override
+ public Set<Permission> getPermissions(ApplicationId appId) {
+ return permissions.get(appId);
+ }
+
+ @Override
+ public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
+ Application app = getApplication(appId);
+ if (app != null) {
+ this.permissions.put(appId, permissions);
+ delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app));
+ }
+ }
+}
diff --git a/core/store/trivial/src/test/java/org/onosproject/store/trivial/impl/SimpleApplicationStoreTest.java b/core/store/trivial/src/test/java/org/onosproject/store/trivial/impl/SimpleApplicationStoreTest.java
new file mode 100644
index 0000000..29006c4
--- /dev/null
+++ b/core/store/trivial/src/test/java/org/onosproject/store/trivial/impl/SimpleApplicationStoreTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.store.trivial.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.app.ApplicationEvent;
+import org.onosproject.app.ApplicationStoreDelegate;
+import org.onosproject.common.app.ApplicationArchive;
+import org.onosproject.core.Application;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.ApplicationIdStoreAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.core.Permission;
+
+import static org.junit.Assert.assertEquals;
+import static org.onosproject.app.ApplicationEvent.Type.*;
+import static org.onosproject.app.ApplicationState.ACTIVE;
+import static org.onosproject.app.ApplicationState.INSTALLED;
+
+/**
+ * Test of the trivial application store implementation.
+ */
+public class SimpleApplicationStoreTest {
+
+ private SimpleApplicationStore store = new SimpleApplicationStore();
+ private TestDelegate delegate = new TestDelegate();
+
+ @Before
+ public void setUp() {
+ store.idStore = new TestIdStore();
+ store.setDelegate(delegate);
+ store.activate();
+ }
+
+ @After
+ public void tearDown() {
+ store.deactivate();
+ }
+
+ private Application createTestApp() {
+ return store.create(ApplicationArchive.class.getResourceAsStream("app.zip"));
+ }
+
+ @Test
+ public void create() {
+ Application app = createTestApp();
+ assertEquals("incorrect name", "org.foo.app", app.id().name());
+ assertEquals("incorrect app count", 1, store.getApplications().size());
+ assertEquals("incorrect app", app, store.getApplication(app.id()));
+ assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
+ assertEquals("incorrect event type", APP_INSTALLED, delegate.event.type());
+ assertEquals("incorrect event app", app, delegate.event.subject());
+ }
+
+ @Test
+ public void remove() {
+ Application app = createTestApp();
+ store.remove(app.id());
+ assertEquals("incorrect app count", 0, store.getApplications().size());
+ assertEquals("incorrect event type", APP_UNINSTALLED, delegate.event.type());
+ assertEquals("incorrect event app", app, delegate.event.subject());
+ }
+
+ @Test
+ public void activate() {
+ Application app = createTestApp();
+ store.activate(app.id());
+ assertEquals("incorrect app count", 1, store.getApplications().size());
+ assertEquals("incorrect app state", ACTIVE, store.getState(app.id()));
+ assertEquals("incorrect event type", APP_ACTIVATED, delegate.event.type());
+ assertEquals("incorrect event app", app, delegate.event.subject());
+ }
+
+ @Test
+ public void deactivate() {
+ Application app = createTestApp();
+ store.deactivate(app.id());
+ assertEquals("incorrect app count", 1, store.getApplications().size());
+ assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
+ assertEquals("incorrect event type", APP_DEACTIVATED, delegate.event.type());
+ assertEquals("incorrect event app", app, delegate.event.subject());
+ }
+
+ @Test
+ public void permissions() {
+ Application app = createTestApp();
+ ImmutableSet<Permission> permissions = ImmutableSet.of(new Permission() {
+ });
+ store.setPermissions(app.id(), permissions);
+ assertEquals("incorrect app perms", 1, store.getPermissions(app.id()).size());
+ assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
+ assertEquals("incorrect event type", APP_PERMISSIONS_CHANGED, delegate.event.type());
+ assertEquals("incorrect event app", app, delegate.event.subject());
+ }
+
+ private class TestIdStore extends ApplicationIdStoreAdapter {
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return new DefaultApplicationId(1, name);
+ }
+
+ @Override
+ public ApplicationId getAppId(String name) {
+ return new DefaultApplicationId(1, name);
+ }
+ }
+
+ private class TestDelegate implements ApplicationStoreDelegate {
+ private ApplicationEvent event;
+
+ @Override
+ public void notify(ApplicationEvent event) {
+ this.event = event;
+ }
+ }
+}
\ No newline at end of file