2a2f4dc49d1c1db93c4e7614768394f83d5c3ae1
[onosfw.git] /
1 /*
2  * Copyright 2014-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 package org.onosproject.store.cluster.impl;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.google.common.io.Files;
22
23 import java.io.File;
24 import java.io.IOException;
25
26 /**
27  * Allows for reading and writing cluster definition as a JSON file.
28  */
29 public class ClusterDefinitionStore {
30
31     private final File file;
32
33     /**
34      * Creates a reader/writer of the cluster definition file.
35      * @param filePath location of the definition file
36      */
37     public ClusterDefinitionStore(String filePath) {
38         file = new File(filePath);
39     }
40
41     /**
42      * Returns the cluster definition.
43      * @return cluster definition
44      * @throws IOException when I/O exception of some sort has occurred
45      */
46     public ClusterDefinition read() throws IOException {
47         ObjectMapper mapper = new ObjectMapper();
48         return mapper.readValue(file, ClusterDefinition.class);
49     }
50
51     /**
52      * Writes the specified cluster definition to file.
53      * @param definition cluster definition
54      * @throws IOException when I/O exception of some sort has occurred
55      */
56     public void write(ClusterDefinition definition) throws IOException {
57         checkNotNull(definition);
58         // write back to file
59         Files.createParentDirs(file);
60         ObjectMapper mapper = new ObjectMapper();
61         mapper.writeValue(file, definition);
62     }
63 }