blob: cedbd6b6a41fe5363108a6f55a8d1b2ac81d5cb0 [file] [log] [blame]
Thomas Vachuskade563cf2015-04-01 00:28:50 -07001/*
2 * Copyright 2015 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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070017
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.cluster.ClusterAdminService;
21import org.onosproject.cluster.ClusterService;
22import org.onosproject.cluster.ControllerNode;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.codec.JsonCodec;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070025import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070026
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.core.Response;
32import java.io.IOException;
33import java.io.InputStream;
34import java.util.HashSet;
35import java.util.List;
36
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070037import static org.onlab.util.Tools.nullIsNotFound;
38
Thomas Vachuskade563cf2015-04-01 00:28:50 -070039/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070040 * Manage cluster of ONOS instances.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070041 */
42@Path("cluster")
43public class ClusterWebResource extends AbstractWebResource {
44
45 public static final String NODE_NOT_FOUND = "Node is not found";
46
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070047 /**
48 * Get all cluster nodes.
49 * Returns array of all cluster nodes.
50 *
51 * @return 200 OK
Andrea Campanella10c4adc2015-12-03 15:27:54 -080052 * @onos.rsModel Cluster
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070053 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070054 @GET
55 public Response getClusterNodes() {
56 Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes();
57 return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build();
58 }
59
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070060 /**
61 * Get cluster node details.
62 * Returns details of the specified cluster node.
63 *
64 * @param id cluster node identifier
65 * @return 200 OK
Andrea Campanella10c4adc2015-12-03 15:27:54 -080066 * @onos.rsModel ClusterNode
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070067 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070068 @GET
69 @Path("{id}")
70 public Response getClusterNode(@PathParam("id") String id) {
71 ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)),
72 NODE_NOT_FOUND);
73 return ok(codec(ControllerNode.class).encode(node, this)).build();
74 }
75
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070076 /**
77 * Forms cluster of ONOS instances.
78 * Forms ONOS cluster using the uploaded JSON definition.
79 *
80 * @param config cluster definition
81 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070082 * @throws IOException to signify bad request
Andrea Campanella10c4adc2015-12-03 15:27:54 -080083 * @onos.rsModel ClusterPost
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070084 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070085 @POST
86 @Path("configuration")
87 public Response formCluster(InputStream config) throws IOException {
88 JsonCodec<ControllerNode> codec = codec(ControllerNode.class);
89 ObjectNode root = (ObjectNode) mapper().readTree(config);
Thomas Vachuskade563cf2015-04-01 00:28:50 -070090
91 List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this);
Madan Jampaniec1df022015-10-13 21:23:03 -070092 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes));
Thomas Vachuskade563cf2015-04-01 00:28:50 -070093
94 return Response.ok().build();
95 }
96
97}