Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.persistence.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
| 23 | import org.mapdb.DB; |
| 24 | import org.mapdb.DBMaker; |
| 25 | import org.onosproject.persistence.PersistenceService; |
| 26 | import org.onosproject.persistence.PersistentMapBuilder; |
| 27 | import org.onosproject.persistence.PersistentSetBuilder; |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 28 | import org.osgi.service.component.ComponentContext; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 31 | import java.io.File; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 32 | import java.io.IOException; |
| 33 | import java.nio.file.Files; |
| 34 | import java.nio.file.Path; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 35 | import java.util.Map; |
| 36 | import java.util.Set; |
| 37 | import java.util.Timer; |
| 38 | import java.util.TimerTask; |
| 39 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 40 | import static org.onosproject.security.AppGuard.checkPermission; |
| 41 | import static org.onosproject.security.AppPermission.Type.PERSISTENCE_WRITE; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Service that maintains local disk backed maps and sets. This implementation automatically deletes empty structures |
| 46 | * on shutdown. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
| 50 | public class PersistenceManager implements PersistenceService { |
| 51 | |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 52 | private static final String DATABASE_PATH = "localDB"; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 53 | |
| 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 Kruglikov | dfb325a | 2015-11-23 14:16:55 -0800 | [diff] [blame] | 64 | private Timer timer; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 65 | |
| 66 | private final CommitTask commitTask = new CommitTask(); |
| 67 | |
| 68 | @Activate |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 69 | public void activate(ComponentContext context) { |
Aaron Kruglikov | dfb325a | 2015-11-23 14:16:55 -0800 | [diff] [blame] | 70 | timer = new Timer(); |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 71 | // bundle's persistent storage area directory |
| 72 | File dbFolderPath = context.getBundleContext().getDataFile(""); |
| 73 | Path dbPath = dbFolderPath.toPath().resolve(DATABASE_PATH); |
| 74 | log.debug("dbPath: {}", dbPath); |
| 75 | |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 76 | //Make sure the directory exists, if it does not, make it. |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 77 | if (!dbFolderPath.isDirectory()) { |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 78 | log.info("The specified folder location for the database did not exist and will be created."); |
| 79 | try { |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 80 | Files.createDirectories(dbFolderPath.toPath()); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 81 | } catch (IOException e) { |
| 82 | log.error("Could not create the required folder for the database."); |
| 83 | throw new PersistenceException("Database folder could not be created."); |
| 84 | } |
| 85 | } |
| 86 | //Notify if the database file does not exist. |
| 87 | boolean dbFound = Files.exists(dbPath); |
| 88 | if (!dbFound) { |
| 89 | log.info("The database file could not be located, a new database will be constructed."); |
| 90 | |
| 91 | } else { |
| 92 | log.info("A previous database file has been found."); |
| 93 | } |
| 94 | localDB = DBMaker.newFileDB(dbPath.toFile()) |
| 95 | .asyncWriteEnable() |
| 96 | .closeOnJvmShutdown() |
| 97 | .make(); |
| 98 | timer.schedule(commitTask, FLUSH_FREQUENCY_MILLIS, FLUSH_FREQUENCY_MILLIS); |
| 99 | log.info("Started"); |
| 100 | } |
| 101 | |
| 102 | @Deactivate |
| 103 | public void deactivate() { |
Aaron Kruglikov | dfb325a | 2015-11-23 14:16:55 -0800 | [diff] [blame] | 104 | timer.cancel(); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 105 | for (Map.Entry<String, Object> entry : localDB.getAll().entrySet()) { |
| 106 | String key = entry.getKey(); |
| 107 | Object value = entry.getValue(); |
| 108 | //This is a map implementation to be handled as such |
| 109 | if (value instanceof Map) { |
| 110 | Map asMap = (Map) value; |
| 111 | if (asMap.isEmpty()) { |
| 112 | //the map is empty and may be deleted |
| 113 | localDB.delete(key); |
| 114 | } |
| 115 | //This is a set implementation and can be handled as such |
| 116 | } else if (value instanceof Set) { |
| 117 | Set asSet = (Set) value; |
| 118 | if (asSet.isEmpty()) { |
| 119 | //the set is empty and may be deleted |
| 120 | localDB.delete(key); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | localDB.commit(); |
| 125 | localDB.close(); |
| 126 | log.info("Stopped"); |
| 127 | } |
| 128 | |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 129 | @Override |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 130 | public <K, V> PersistentMapBuilder<K, V> persistentMapBuilder() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 131 | checkPermission(PERSISTENCE_WRITE); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 132 | return new DefaultPersistentMapBuilder<>(localDB); |
| 133 | } |
| 134 | |
Yuta HIGUCHI | 7f2c6f9 | 2016-07-04 19:52:52 -0700 | [diff] [blame] | 135 | @Override |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 136 | public <E> PersistentSetBuilder<E> persistentSetBuilder() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 137 | checkPermission(PERSISTENCE_WRITE); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 138 | return new DefaultPersistentSetBuilder<>(localDB); |
| 139 | } |
| 140 | |
| 141 | private class CommitTask extends TimerTask { |
| 142 | |
| 143 | @Override |
| 144 | public void run() { |
| 145 | localDB.commit(); |
| 146 | } |
| 147 | } |
| 148 | } |