blob: 250fd02fce925d5fd4fb9717eaae77891ba50b60 [file] [log] [blame]
Thomas Vachuskade563cf2015-04-01 00:28:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskade563cf2015-04-01 00:28:50 -07003 *
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;
Jian Licc730a62016-05-10 16:36:16 -070031import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070033import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
36import java.util.HashSet;
37import java.util.List;
38
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070039import static org.onlab.util.Tools.nullIsNotFound;
40
Thomas Vachuskade563cf2015-04-01 00:28:50 -070041/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070042 * Manage cluster of ONOS instances.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070043 */
44@Path("cluster")
45public class ClusterWebResource extends AbstractWebResource {
46
Jian Licc730a62016-05-10 16:36:16 -070047 private static final String NODE_NOT_FOUND = "Node is not found";
Thomas Vachuskade563cf2015-04-01 00:28:50 -070048
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070049 /**
50 * Get all cluster nodes.
51 * Returns array of all cluster nodes.
52 *
Jian Licc730a62016-05-10 16:36:16 -070053 * @return 200 OK with a collection of cluster nodes
Andrea Campanella10c4adc2015-12-03 15:27:54 -080054 * @onos.rsModel Cluster
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070055 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070056 @GET
Jian Licc730a62016-05-10 16:36:16 -070057 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070058 public Response getClusterNodes() {
59 Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes();
60 return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build();
61 }
62
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 /**
64 * Get cluster node details.
65 * Returns details of the specified cluster node.
66 *
67 * @param id cluster node identifier
Jian Licc730a62016-05-10 16:36:16 -070068 * @return 200 OK with a cluster node
Andrea Campanella10c4adc2015-12-03 15:27:54 -080069 * @onos.rsModel ClusterNode
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070070 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070071 @GET
72 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070073 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070074 public Response getClusterNode(@PathParam("id") String id) {
75 ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)),
76 NODE_NOT_FOUND);
77 return ok(codec(ControllerNode.class).encode(node, this)).build();
78 }
79
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070080 /**
81 * Forms cluster of ONOS instances.
82 * Forms ONOS cluster using the uploaded JSON definition.
83 *
84 * @param config cluster definition
85 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070086 * @throws IOException to signify bad request
Andrea Campanella10c4adc2015-12-03 15:27:54 -080087 * @onos.rsModel ClusterPost
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070088 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070089 @POST
90 @Path("configuration")
Jian Licc730a62016-05-10 16:36:16 -070091 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070092 public Response formCluster(InputStream config) throws IOException {
93 JsonCodec<ControllerNode> codec = codec(ControllerNode.class);
94 ObjectNode root = (ObjectNode) mapper().readTree(config);
Thomas Vachuskade563cf2015-04-01 00:28:50 -070095
96 List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this);
Madan Jampaniec1df022015-10-13 21:23:03 -070097 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes));
Thomas Vachuskade563cf2015-04-01 00:28:50 -070098
99 return Response.ok().build();
100 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700101}