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 | |
| 18 | import com.fasterxml.jackson.core.JsonEncoding; |
| 19 | import com.fasterxml.jackson.core.JsonFactory; |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.DefaultControllerNode; |
| 25 | import org.onosproject.cluster.NodeId; |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 27 | |
| 28 | import java.io.File; |
| 29 | import java.io.IOException; |
| 30 | import java.util.HashSet; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.Set; |
| 33 | |
Yuta HIGUCHI | 41f2ec0 | 2014-10-27 09:54:43 -0700 | [diff] [blame] | 34 | //Not used right now |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Allows for reading and writing cluster definition as a JSON file. |
| 37 | */ |
| 38 | public class ClusterDefinitionStore { |
| 39 | |
| 40 | private final File file; |
| 41 | |
| 42 | /** |
| 43 | * Creates a reader/writer of the cluster definition file. |
| 44 | * |
| 45 | * @param filePath location of the definition file |
| 46 | */ |
| 47 | public ClusterDefinitionStore(String filePath) { |
| 48 | file = new File(filePath); |
| 49 | } |
| 50 | |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 51 | /* |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 52 | * Returns set of the controller nodes, including self. |
| 53 | * |
| 54 | * @return set of controller nodes |
| 55 | */ |
| 56 | public Set<DefaultControllerNode> read() throws IOException { |
| 57 | Set<DefaultControllerNode> nodes = new HashSet<>(); |
| 58 | ObjectMapper mapper = new ObjectMapper(); |
| 59 | ObjectNode clusterNodeDef = (ObjectNode) mapper.readTree(file); |
| 60 | Iterator<JsonNode> it = ((ArrayNode) clusterNodeDef.get("nodes")).elements(); |
| 61 | while (it.hasNext()) { |
| 62 | ObjectNode nodeDef = (ObjectNode) it.next(); |
| 63 | nodes.add(new DefaultControllerNode(new NodeId(nodeDef.get("id").asText()), |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 64 | IpAddress.valueOf(nodeDef.get("ip").asText()), |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 65 | nodeDef.get("tcpPort").asInt(9876))); |
| 66 | } |
| 67 | return nodes; |
| 68 | } |
| 69 | |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 70 | /* |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 71 | * Writes the given set of the controller nodes. |
| 72 | * |
| 73 | * @param nodes set of controller nodes |
| 74 | */ |
| 75 | public void write(Set<DefaultControllerNode> nodes) throws IOException { |
| 76 | ObjectMapper mapper = new ObjectMapper(); |
| 77 | ObjectNode clusterNodeDef = mapper.createObjectNode(); |
| 78 | ArrayNode nodeDefs = mapper.createArrayNode(); |
| 79 | clusterNodeDef.set("nodes", nodeDefs); |
| 80 | for (DefaultControllerNode node : nodes) { |
| 81 | ObjectNode nodeDef = mapper.createObjectNode(); |
| 82 | nodeDef.put("id", node.id().toString()) |
| 83 | .put("ip", node.ip().toString()) |
| 84 | .put("tcpPort", node.tcpPort()); |
| 85 | nodeDefs.add(nodeDef); |
| 86 | } |
| 87 | mapper.writeTree(new JsonFactory().createGenerator(file, JsonEncoding.UTF8), |
| 88 | clusterNodeDef); |
| 89 | } |
| 90 | |
| 91 | } |