2 * Copyright 2014-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.
16 package org.onosproject.store.cluster.impl;
18 import static com.google.common.base.Preconditions.checkNotNull;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.google.common.io.Files;
24 import java.io.IOException;
27 * Allows for reading and writing cluster definition as a JSON file.
29 public class ClusterDefinitionStore {
31 private final File file;
34 * Creates a reader/writer of the cluster definition file.
35 * @param filePath location of the definition file
37 public ClusterDefinitionStore(String filePath) {
38 file = new File(filePath);
42 * Returns the cluster definition.
43 * @return cluster definition
44 * @throws IOException when I/O exception of some sort has occurred
46 public ClusterDefinition read() throws IOException {
47 ObjectMapper mapper = new ObjectMapper();
48 return mapper.readValue(file, ClusterDefinition.class);
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
56 public void write(ClusterDefinition definition) throws IOException {
57 checkNotNull(definition);
59 Files.createParentDirs(file);
60 ObjectMapper mapper = new ObjectMapper();
61 mapper.writeValue(file, definition);