Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 20 | import java.io.File; |
| 21 | import java.io.IOException; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Allows for reading and writing partitioned database definition as a JSON file. |
| 26 | */ |
| 27 | public class DatabaseDefinitionStore { |
| 28 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 29 | private final File definitionfile; |
| 30 | |
| 31 | /** |
| 32 | * Creates a reader/writer of the database definition file. |
| 33 | * |
| 34 | * @param filePath location of the definition file |
| 35 | */ |
| 36 | public DatabaseDefinitionStore(String filePath) { |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 37 | definitionfile = new File(checkNotNull(filePath)); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Creates a reader/writer of the database definition file. |
| 42 | * |
| 43 | * @param filePath location of the definition file |
| 44 | */ |
| 45 | public DatabaseDefinitionStore(File filePath) { |
| 46 | definitionfile = checkNotNull(filePath); |
| 47 | } |
| 48 | |
| 49 | /** |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 50 | * Returns the database definition. |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 51 | * |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 52 | * @return database definition |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 53 | * @throws IOException when I/O exception of some sort has occurred. |
| 54 | */ |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 55 | public DatabaseDefinition read() throws IOException { |
| 56 | ObjectMapper mapper = new ObjectMapper(); |
| 57 | DatabaseDefinition definition = mapper.readValue(definitionfile, DatabaseDefinition.class); |
| 58 | return definition; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 62 | * Writes the specified database definition to file. |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 63 | * |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 64 | * @param definition database definition |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 65 | * @throws IOException when I/O exception of some sort has occurred. |
| 66 | */ |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 67 | public void write(DatabaseDefinition definition) throws IOException { |
| 68 | checkNotNull(definition); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 69 | // write back to file |
| 70 | final ObjectMapper mapper = new ObjectMapper(); |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame^] | 71 | mapper.writeValue(definitionfile, definition); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 72 | } |
| 73 | } |