blob: 1a24268946584d7cfd6f231662c86e1ed209083d [file] [log] [blame]
Aaron Kruglikov92511f22015-10-12 14:39:04 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Aaron Kruglikov92511f22015-10-12 14:39:04 -07003 *
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 */
16
17package org.onosproject.persistence.impl;
18
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.Service;
23import org.mapdb.DB;
24import org.mapdb.DBMaker;
25import org.onosproject.persistence.PersistenceService;
26import org.onosproject.persistence.PersistentMapBuilder;
27import org.onosproject.persistence.PersistentSetBuilder;
28import org.slf4j.Logger;
29
30import java.io.IOException;
31import java.nio.file.Files;
32import java.nio.file.Path;
33import java.nio.file.Paths;
34import java.util.Map;
35import java.util.Set;
36import java.util.Timer;
37import java.util.TimerTask;
38
Heedo Kang4a47a302016-02-29 17:40:23 +090039import static org.onosproject.security.AppGuard.checkPermission;
40import static org.onosproject.security.AppPermission.Type.PERSISTENCE_WRITE;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070041import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Service that maintains local disk backed maps and sets. This implementation automatically deletes empty structures
45 * on shutdown.
46 */
47@Component(immediate = true)
48@Service
49public class PersistenceManager implements PersistenceService {
50
Madan Jampani44d8fbb2015-12-04 12:54:39 -080051 private static final String DATABASE_PATH = "../data/localDB";
52 private static final String ENCLOSING_FOLDER = "../data";
Aaron Kruglikov92511f22015-10-12 14:39:04 -070053
54 static final String MAP_PREFIX = "map:";
55
56 static final String SET_PREFIX = "set:";
57
58 private final Logger log = getLogger(getClass());
59
60 private DB localDB = null;
61
62 private static final int FLUSH_FREQUENCY_MILLIS = 3000;
63
Aaron Kruglikovdfb325a2015-11-23 14:16:55 -080064 private Timer timer;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070065
66 private final CommitTask commitTask = new CommitTask();
67
68 @Activate
69 public void activate() {
Aaron Kruglikovdfb325a2015-11-23 14:16:55 -080070 timer = new Timer();
Aaron Kruglikov92511f22015-10-12 14:39:04 -070071 Path dbPath = Paths.get(DATABASE_PATH);
72 Path dbFolderPath = Paths.get(ENCLOSING_FOLDER);
73 //Make sure the directory exists, if it does not, make it.
74 if (!dbFolderPath.toFile().isDirectory()) {
75 log.info("The specified folder location for the database did not exist and will be created.");
76 try {
77 Files.createDirectories(dbFolderPath);
78 } catch (IOException e) {
79 log.error("Could not create the required folder for the database.");
80 throw new PersistenceException("Database folder could not be created.");
81 }
82 }
83 //Notify if the database file does not exist.
84 boolean dbFound = Files.exists(dbPath);
85 if (!dbFound) {
86 log.info("The database file could not be located, a new database will be constructed.");
87
88 } else {
89 log.info("A previous database file has been found.");
90 }
91 localDB = DBMaker.newFileDB(dbPath.toFile())
92 .asyncWriteEnable()
93 .closeOnJvmShutdown()
94 .make();
95 timer.schedule(commitTask, FLUSH_FREQUENCY_MILLIS, FLUSH_FREQUENCY_MILLIS);
96 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
Aaron Kruglikovdfb325a2015-11-23 14:16:55 -0800101 timer.cancel();
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700102 for (Map.Entry<String, Object> entry : localDB.getAll().entrySet()) {
103 String key = entry.getKey();
104 Object value = entry.getValue();
105 //This is a map implementation to be handled as such
106 if (value instanceof Map) {
107 Map asMap = (Map) value;
108 if (asMap.isEmpty()) {
109 //the map is empty and may be deleted
110 localDB.delete(key);
111 }
112 //This is a set implementation and can be handled as such
113 } else if (value instanceof Set) {
114 Set asSet = (Set) value;
115 if (asSet.isEmpty()) {
116 //the set is empty and may be deleted
117 localDB.delete(key);
118 }
119 }
120 }
121 localDB.commit();
122 localDB.close();
123 log.info("Stopped");
124 }
125
126 public <K, V> PersistentMapBuilder<K, V> persistentMapBuilder() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900127 checkPermission(PERSISTENCE_WRITE);
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700128 return new DefaultPersistentMapBuilder<>(localDB);
129 }
130
131 public <E> PersistentSetBuilder<E> persistentSetBuilder() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900132 checkPermission(PERSISTENCE_WRITE);
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700133 return new DefaultPersistentSetBuilder<>(localDB);
134 }
135
136 private class CommitTask extends TimerTask {
137
138 @Override
139 public void run() {
140 localDB.commit();
141 }
142 }
143}