blob: b77667b28e7801240cb0e91986e70c770ba7f9c1 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
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 Jampani09342702015-02-05 23:32:40 -080017package org.onosproject.store.consistent.impl;
18
Madan Jampani09342702015-02-05 23:32:40 -080019import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani09342702015-02-05 23:32:40 -080020import java.io.File;
21import java.io.IOException;
Madan Jampani09342702015-02-05 23:32:40 -080022import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070023import com.google.common.io.Files;
Madan Jampani09342702015-02-05 23:32:40 -080024
25/**
26 * Allows for reading and writing partitioned database definition as a JSON file.
27 */
28public class DatabaseDefinitionStore {
29
Thomas Vachuskade563cf2015-04-01 00:28:50 -070030 private final File file;
Madan Jampani09342702015-02-05 23:32:40 -080031
32 /**
33 * Creates a reader/writer of the database definition file.
34 *
35 * @param filePath location of the definition file
36 */
37 public DatabaseDefinitionStore(String filePath) {
Thomas Vachuskade563cf2015-04-01 00:28:50 -070038 file = new File(checkNotNull(filePath));
Madan Jampani09342702015-02-05 23:32:40 -080039 }
40
41 /**
42 * Creates a reader/writer of the database definition file.
43 *
44 * @param filePath location of the definition file
45 */
46 public DatabaseDefinitionStore(File filePath) {
Thomas Vachuskade563cf2015-04-01 00:28:50 -070047 file = checkNotNull(filePath);
Madan Jampani09342702015-02-05 23:32:40 -080048 }
49
50 /**
Madan Jampani0cb00672015-02-27 00:27:22 -080051 * Returns the database definition.
Madan Jampani09342702015-02-05 23:32:40 -080052 *
Madan Jampani0cb00672015-02-27 00:27:22 -080053 * @return database definition
Madan Jampani09342702015-02-05 23:32:40 -080054 * @throws IOException when I/O exception of some sort has occurred.
55 */
Madan Jampani0cb00672015-02-27 00:27:22 -080056 public DatabaseDefinition read() throws IOException {
57 ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskade563cf2015-04-01 00:28:50 -070058 return mapper.readValue(file, DatabaseDefinition.class);
Madan Jampani09342702015-02-05 23:32:40 -080059 }
60
61 /**
Madan Jampani0cb00672015-02-27 00:27:22 -080062 * Writes the specified database definition to file.
Madan Jampani09342702015-02-05 23:32:40 -080063 *
Madan Jampani0cb00672015-02-27 00:27:22 -080064 * @param definition database definition
Madan Jampani09342702015-02-05 23:32:40 -080065 * @throws IOException when I/O exception of some sort has occurred.
66 */
Madan Jampani0cb00672015-02-27 00:27:22 -080067 public void write(DatabaseDefinition definition) throws IOException {
68 checkNotNull(definition);
Madan Jampani09342702015-02-05 23:32:40 -080069 // write back to file
Thomas Vachuskade563cf2015-04-01 00:28:50 -070070 Files.createParentDirs(file);
71 ObjectMapper mapper = new ObjectMapper();
72 mapper.writeValue(file, definition);
Madan Jampani09342702015-02-05 23:32:40 -080073 }
74}