blob: 2a2f4dc49d1c1db93c4e7614768394f83d5c3ae1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.cluster.impl;
tom73094832014-09-29 13:47:08 -070017
Madan Jampani0cb00672015-02-27 00:27:22 -080018import static com.google.common.base.Preconditions.checkNotNull;
19
tom73094832014-09-29 13:47:08 -070020import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070021import com.google.common.io.Files;
22
tom73094832014-09-29 13:47:08 -070023import java.io.File;
24import java.io.IOException;
tom73094832014-09-29 13:47:08 -070025
tom73094832014-09-29 13:47:08 -070026/**
27 * Allows for reading and writing cluster definition as a JSON file.
28 */
29public class ClusterDefinitionStore {
30
31 private final File file;
32
33 /**
34 * Creates a reader/writer of the cluster definition file.
tom73094832014-09-29 13:47:08 -070035 * @param filePath location of the definition file
36 */
37 public ClusterDefinitionStore(String filePath) {
38 file = new File(filePath);
39 }
40
Madan Jampani0cb00672015-02-27 00:27:22 -080041 /**
42 * Returns the cluster definition.
43 * @return cluster definition
44 * @throws IOException when I/O exception of some sort has occurred
tom73094832014-09-29 13:47:08 -070045 */
Madan Jampania14047d2015-02-25 12:23:02 -080046 public ClusterDefinition read() throws IOException {
tom73094832014-09-29 13:47:08 -070047 ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskade563cf2015-04-01 00:28:50 -070048 return mapper.readValue(file, ClusterDefinition.class);
tom73094832014-09-29 13:47:08 -070049 }
50
Madan Jampani0cb00672015-02-27 00:27:22 -080051 /**
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
tom73094832014-09-29 13:47:08 -070055 */
Madan Jampania14047d2015-02-25 12:23:02 -080056 public void write(ClusterDefinition definition) throws IOException {
Madan Jampani0cb00672015-02-27 00:27:22 -080057 checkNotNull(definition);
58 // write back to file
Thomas Vachuskade563cf2015-04-01 00:28:50 -070059 Files.createParentDirs(file);
60 ObjectMapper mapper = new ObjectMapper();
Madan Jampani0cb00672015-02-27 00:27:22 -080061 mapper.writeValue(file, definition);
tom73094832014-09-29 13:47:08 -070062 }
Ray Milkey34c95902015-04-15 09:47:53 -070063}