Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'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; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 21 | import java.io.File; |
| 22 | import java.io.IOException; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 23 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Allows for reading and writing cluster definition as a JSON file. |
| 26 | */ |
| 27 | public class ClusterDefinitionStore { |
| 28 | |
| 29 | private final File file; |
| 30 | |
| 31 | /** |
| 32 | * Creates a reader/writer of the cluster definition file. |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 33 | * @param filePath location of the definition file |
| 34 | */ |
| 35 | public ClusterDefinitionStore(String filePath) { |
| 36 | file = new File(filePath); |
| 37 | } |
| 38 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Returns the cluster definition. |
| 41 | * @return cluster definition |
| 42 | * @throws IOException when I/O exception of some sort has occurred |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 43 | */ |
Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [diff] [blame] | 44 | public ClusterDefinition read() throws IOException { |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 45 | ObjectMapper mapper = new ObjectMapper(); |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 46 | ClusterDefinition definition = mapper.readValue(file, ClusterDefinition.class); |
| 47 | return definition; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 50 | /** |
| 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 |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 54 | */ |
Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [diff] [blame] | 55 | public void write(ClusterDefinition definition) throws IOException { |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 56 | checkNotNull(definition); |
| 57 | // write back to file |
| 58 | final ObjectMapper mapper = new ObjectMapper(); |
| 59 | mapper.writeValue(file, definition); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 60 | } |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 61 | } |