blob: 19b64854099eeafc4900ce0a04095b18f9174546 [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 */
tom73094832014-09-29 13:47:08 -070016package org.onlab.onos.store.cluster.impl;
17
18import com.fasterxml.jackson.core.JsonEncoding;
19import com.fasterxml.jackson.core.JsonFactory;
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.onlab.onos.cluster.DefaultControllerNode;
25import org.onlab.onos.cluster.NodeId;
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070026import org.onlab.packet.IpAddress;
tom73094832014-09-29 13:47:08 -070027
28import java.io.File;
29import java.io.IOException;
30import java.util.HashSet;
31import java.util.Iterator;
32import java.util.Set;
33
Yuta HIGUCHI41f2ec02014-10-27 09:54:43 -070034//Not used right now
tom73094832014-09-29 13:47:08 -070035/**
36 * Allows for reading and writing cluster definition as a JSON file.
37 */
38public 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 HIGUCHI5c947272014-11-03 21:39:21 -080051 /*
tom73094832014-09-29 13:47:08 -070052 * 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 Radoslavov444b5192014-10-28 10:45:19 -070064 IpAddress.valueOf(nodeDef.get("ip").asText()),
tom73094832014-09-29 13:47:08 -070065 nodeDef.get("tcpPort").asInt(9876)));
66 }
67 return nodes;
68 }
69
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080070 /*
tom73094832014-09-29 13:47:08 -070071 * 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}