blob: 498f5d0aed53b336c6654a4e9e9ef854b7b5d6d9 [file] [log] [blame]
Aaron Kruglikov92511f22015-10-12 14:39:04 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Yuta HIGUCHI7f2c6f92016-07-04 19:52:52 -070030import java.io.File;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070031import java.io.IOException;
32import java.nio.file.Files;
33import java.nio.file.Path;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070034import 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/**
Thomas Vachuska58bf4912017-10-31 12:00:32 -070044 * Service that maintains local disk backed maps and sets.
45 * This implementation automatically deletes empty structures on shutdown.
Aaron Kruglikov92511f22015-10-12 14:39:04 -070046 */
47@Component(immediate = true)
48@Service
49public class PersistenceManager implements PersistenceService {
50
Thomas Vachuska58bf4912017-10-31 12:00:32 -070051 private static final String DATABASE_ROOT =
52 System.getProperty("karaf.data") + "/db/local/";
53
54 private static final String DATABASE_PATH = "cache";
Aaron Kruglikov92511f22015-10-12 14:39:04 -070055
56 static final String MAP_PREFIX = "map:";
Aaron Kruglikov92511f22015-10-12 14:39:04 -070057 static final String SET_PREFIX = "set:";
58
59 private final Logger log = getLogger(getClass());
60
61 private DB localDB = null;
62
63 private static final int FLUSH_FREQUENCY_MILLIS = 3000;
64
Aaron Kruglikovdfb325a2015-11-23 14:16:55 -080065 private Timer timer;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070066
67 private final CommitTask commitTask = new CommitTask();
68
69 @Activate
Thomas Vachuska58bf4912017-10-31 12:00:32 -070070 public void activate() {
Aaron Kruglikovdfb325a2015-11-23 14:16:55 -080071 timer = new Timer();
Thomas Vachuska58bf4912017-10-31 12:00:32 -070072
73 File dbFolderPath = new File(DATABASE_ROOT);
Yuta HIGUCHI7f2c6f92016-07-04 19:52:52 -070074 Path dbPath = dbFolderPath.toPath().resolve(DATABASE_PATH);
75 log.debug("dbPath: {}", dbPath);
76
Aaron Kruglikov92511f22015-10-12 14:39:04 -070077 //Make sure the directory exists, if it does not, make it.
Yuta HIGUCHI7f2c6f92016-07-04 19:52:52 -070078 if (!dbFolderPath.isDirectory()) {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070079 log.info("The specified folder location for the database did not exist and will be created.");
80 try {
Yuta HIGUCHI7f2c6f92016-07-04 19:52:52 -070081 Files.createDirectories(dbFolderPath.toPath());
Aaron Kruglikov92511f22015-10-12 14:39:04 -070082 } catch (IOException e) {
83 log.error("Could not create the required folder for the database.");
84 throw new PersistenceException("Database folder could not be created.");
85 }
86 }
87 //Notify if the database file does not exist.
88 boolean dbFound = Files.exists(dbPath);
89 if (!dbFound) {
90 log.info("The database file could not be located, a new database will be constructed.");
91
92 } else {
93 log.info("A previous database file has been found.");
94 }
95 localDB = DBMaker.newFileDB(dbPath.toFile())
96 .asyncWriteEnable()
97 .closeOnJvmShutdown()
98 .make();
99 timer.schedule(commitTask, FLUSH_FREQUENCY_MILLIS, FLUSH_FREQUENCY_MILLIS);
100 log.info("Started");
101 }
102
103 @Deactivate
104 public void deactivate() {
Aaron Kruglikovdfb325a2015-11-23 14:16:55 -0800105 timer.cancel();
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700106 for (Map.Entry<String, Object> entry : localDB.getAll().entrySet()) {
107 String key = entry.getKey();
108 Object value = entry.getValue();
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700109 if (value instanceof Map) {
Thomas Vachuska58bf4912017-10-31 12:00:32 -0700110 // This is a map implementation to be handled as such
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700111 Map asMap = (Map) value;
112 if (asMap.isEmpty()) {
113 //the map is empty and may be deleted
114 localDB.delete(key);
115 }
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700116 } else if (value instanceof Set) {
Thomas Vachuska58bf4912017-10-31 12:00:32 -0700117 // This is a set implementation and can be handled as such
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700118 Set asSet = (Set) value;
119 if (asSet.isEmpty()) {
120 //the set is empty and may be deleted
121 localDB.delete(key);
122 }
123 }
124 }
125 localDB.commit();
126 localDB.close();
127 log.info("Stopped");
128 }
129
Yuta HIGUCHI7f2c6f92016-07-04 19:52:52 -0700130 @Override
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700131 public <K, V> PersistentMapBuilder<K, V> persistentMapBuilder() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900132 checkPermission(PERSISTENCE_WRITE);
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700133 return new DefaultPersistentMapBuilder<>(localDB);
134 }
135
Yuta HIGUCHI7f2c6f92016-07-04 19:52:52 -0700136 @Override
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700137 public <E> PersistentSetBuilder<E> persistentSetBuilder() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900138 checkPermission(PERSISTENCE_WRITE);
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700139 return new DefaultPersistentSetBuilder<>(localDB);
140 }
141
142 private class CommitTask extends TimerTask {
143
144 @Override
145 public void run() {
146 localDB.commit();
147 }
148 }
149}