2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.store.consistent.impl;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import java.io.IOException;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.google.common.io.Files;
26 * Allows for reading and writing partitioned database definition as a JSON file.
28 public class DatabaseDefinitionStore {
30 private final File file;
33 * Creates a reader/writer of the database definition file.
35 * @param filePath location of the definition file
37 public DatabaseDefinitionStore(String filePath) {
38 file = new File(checkNotNull(filePath));
42 * Creates a reader/writer of the database definition file.
44 * @param filePath location of the definition file
46 public DatabaseDefinitionStore(File filePath) {
47 file = checkNotNull(filePath);
51 * Returns the database definition.
53 * @return database definition
54 * @throws IOException when I/O exception of some sort has occurred.
56 public DatabaseDefinition read() throws IOException {
57 ObjectMapper mapper = new ObjectMapper();
58 return mapper.readValue(file, DatabaseDefinition.class);
62 * Writes the specified database definition to file.
64 * @param definition database definition
65 * @throws IOException when I/O exception of some sort has occurred.
67 public void write(DatabaseDefinition definition) throws IOException {
68 checkNotNull(definition);
70 Files.createParentDirs(file);
71 ObjectMapper mapper = new ObjectMapper();
72 mapper.writeValue(file, definition);