Yuta HIGUCHI
Committed by Gerrit Code Review

Use karaf/OSGi bundle data path for persistence

Change-Id: If5a4eb1fb63ca3455200c29e9981c7a0f2dd044e
......@@ -12,16 +12,11 @@ target
dependency-reduced-pom.xml
.idea
*.DS_Store
localDB
localDB.*
.buckd
buck-out
.buckconfig.local
.buckjavaargs*
core/store/trivial/data/
core/store/apps
core/store/data
/bin/
web/gui/src/main/webapp/tests/node_modules
......
......@@ -46,6 +46,11 @@
<artifactId>mapdb</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
</project>
......
......@@ -25,12 +25,13 @@ import org.mapdb.DBMaker;
import org.onosproject.persistence.PersistenceService;
import org.onosproject.persistence.PersistentMapBuilder;
import org.onosproject.persistence.PersistentSetBuilder;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
......@@ -48,8 +49,7 @@ import static org.slf4j.LoggerFactory.getLogger;
@Service
public class PersistenceManager implements PersistenceService {
private static final String DATABASE_PATH = "../data/localDB";
private static final String ENCLOSING_FOLDER = "../data";
private static final String DATABASE_PATH = "localDB";
static final String MAP_PREFIX = "map:";
......@@ -66,15 +66,18 @@ public class PersistenceManager implements PersistenceService {
private final CommitTask commitTask = new CommitTask();
@Activate
public void activate() {
public void activate(ComponentContext context) {
timer = new Timer();
Path dbPath = Paths.get(DATABASE_PATH);
Path dbFolderPath = Paths.get(ENCLOSING_FOLDER);
// bundle's persistent storage area directory
File dbFolderPath = context.getBundleContext().getDataFile("");
Path dbPath = dbFolderPath.toPath().resolve(DATABASE_PATH);
log.debug("dbPath: {}", dbPath);
//Make sure the directory exists, if it does not, make it.
if (!dbFolderPath.toFile().isDirectory()) {
if (!dbFolderPath.isDirectory()) {
log.info("The specified folder location for the database did not exist and will be created.");
try {
Files.createDirectories(dbFolderPath);
Files.createDirectories(dbFolderPath.toPath());
} catch (IOException e) {
log.error("Could not create the required folder for the database.");
throw new PersistenceException("Database folder could not be created.");
......@@ -123,11 +126,13 @@ public class PersistenceManager implements PersistenceService {
log.info("Stopped");
}
@Override
public <K, V> PersistentMapBuilder<K, V> persistentMapBuilder() {
checkPermission(PERSISTENCE_WRITE);
return new DefaultPersistentMapBuilder<>(localDB);
}
@Override
public <E> PersistentSetBuilder<E> persistentSetBuilder() {
checkPermission(PERSISTENCE_WRITE);
return new DefaultPersistentSetBuilder<>(localDB);
......
......@@ -47,7 +47,7 @@ fi
# If clean option was specified, wipe-out existing installation
if [ "$CLEAN" = "true" ]; then
echo "Removing existing ONOS Karaf, apps, data and config directories..."
[ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $STAGE/apps $STAGE/data $STAGE/config
[ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $KARAF_ROOT/data $STAGE/apps $STAGE/data $STAGE/config
fi
# If IP was not given, nor configured attempt to use ONOS_NIC env. variable
......