Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.impl; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 21 | import com.google.common.io.Files; |
| 22 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 23 | import java.io.File; |
| 24 | import java.io.IOException; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 25 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 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. |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 35 | * @param filePath location of the definition file |
| 36 | */ |
| 37 | public ClusterDefinitionStore(String filePath) { |
| 38 | file = new File(filePath); |
| 39 | } |
| 40 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Returns the cluster definition. |
| 43 | * @return cluster definition |
| 44 | * @throws IOException when I/O exception of some sort has occurred |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 45 | */ |
Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [diff] [blame] | 46 | public ClusterDefinition read() throws IOException { |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 47 | ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 48 | return mapper.readValue(file, ClusterDefinition.class); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 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 |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 55 | */ |
Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [diff] [blame] | 56 | public void write(ClusterDefinition definition) throws IOException { |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 57 | checkNotNull(definition); |
| 58 | // write back to file |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 59 | Files.createParentDirs(file); |
| 60 | ObjectMapper mapper = new ObjectMapper(); |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 61 | mapper.writeValue(file, definition); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 62 | } |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 63 | } |