blob: a522a85fb7637f488dea472ac5fcd8e5c39f4f19 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
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;
tom73094832014-09-29 13:47:08 -070021import java.io.File;
22import java.io.IOException;
tom73094832014-09-29 13:47:08 -070023
tom73094832014-09-29 13:47:08 -070024/**
25 * Allows for reading and writing cluster definition as a JSON file.
26 */
27public class ClusterDefinitionStore {
28
29 private final File file;
30
31 /**
32 * Creates a reader/writer of the cluster definition file.
tom73094832014-09-29 13:47:08 -070033 * @param filePath location of the definition file
34 */
35 public ClusterDefinitionStore(String filePath) {
36 file = new File(filePath);
37 }
38
Madan Jampani0cb00672015-02-27 00:27:22 -080039 /**
40 * Returns the cluster definition.
41 * @return cluster definition
42 * @throws IOException when I/O exception of some sort has occurred
tom73094832014-09-29 13:47:08 -070043 */
Madan Jampania14047d2015-02-25 12:23:02 -080044 public ClusterDefinition read() throws IOException {
tom73094832014-09-29 13:47:08 -070045 ObjectMapper mapper = new ObjectMapper();
Madan Jampani0cb00672015-02-27 00:27:22 -080046 ClusterDefinition definition = mapper.readValue(file, ClusterDefinition.class);
47 return definition;
tom73094832014-09-29 13:47:08 -070048 }
49
Madan Jampani0cb00672015-02-27 00:27:22 -080050 /**
51 * Writes the specified cluster definition to file.
52 * @param definition cluster definition
53 * @throws IOException when I/O exception of some sort has occurred
tom73094832014-09-29 13:47:08 -070054 */
Madan Jampania14047d2015-02-25 12:23:02 -080055 public void write(ClusterDefinition definition) throws IOException {
Madan Jampani0cb00672015-02-27 00:27:22 -080056 checkNotNull(definition);
57 // write back to file
58 final ObjectMapper mapper = new ObjectMapper();
59 mapper.writeValue(file, definition);
tom73094832014-09-29 13:47:08 -070060 }
Madan Jampani0cb00672015-02-27 00:27:22 -080061}