blob: 63a40817671250a9d3f8036fa37404b9a066f971 [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
Thiago Santos7a174cf2016-09-01 14:56:54 -030018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.cluster.ClusterAdminService;
22import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.ControllerNode;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.codec.JsonCodec;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070026import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070027
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
Jian Licc730a62016-05-10 16:36:16 -070032import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070034import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.HashSet;
38import java.util.List;
39
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070040import static org.onlab.util.Tools.nullIsNotFound;
41
Thomas Vachuskade563cf2015-04-01 00:28:50 -070042/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070043 * Manage cluster of ONOS instances.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070044 */
45@Path("cluster")
46public class ClusterWebResource extends AbstractWebResource {
47
Jian Licc730a62016-05-10 16:36:16 -070048 private static final String NODE_NOT_FOUND = "Node is not found";
Thomas Vachuskade563cf2015-04-01 00:28:50 -070049
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 /**
51 * Get all cluster nodes.
52 * Returns array of all cluster nodes.
53 *
Jian Licc730a62016-05-10 16:36:16 -070054 * @return 200 OK with a collection of cluster nodes
Andrea Campanella10c4adc2015-12-03 15:27:54 -080055 * @onos.rsModel Cluster
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070056 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070057 @GET
Jian Licc730a62016-05-10 16:36:16 -070058 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070059 public Response getClusterNodes() {
60 Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes();
61 return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build();
62 }
63
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 /**
65 * Get cluster node details.
66 * Returns details of the specified cluster node.
67 *
68 * @param id cluster node identifier
Jian Licc730a62016-05-10 16:36:16 -070069 * @return 200 OK with a cluster node
Andrea Campanella10c4adc2015-12-03 15:27:54 -080070 * @onos.rsModel ClusterNode
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070072 @GET
73 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070074 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070075 public Response getClusterNode(@PathParam("id") String id) {
76 ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)),
77 NODE_NOT_FOUND);
78 return ok(codec(ControllerNode.class).encode(node, this)).build();
79 }
80
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070081 /**
82 * Forms cluster of ONOS instances.
83 * Forms ONOS cluster using the uploaded JSON definition.
84 *
85 * @param config cluster definition
86 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070087 * @throws IOException to signify bad request
Andrea Campanella10c4adc2015-12-03 15:27:54 -080088 * @onos.rsModel ClusterPost
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070089 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070090 @POST
91 @Path("configuration")
Jian Licc730a62016-05-10 16:36:16 -070092 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070093 public Response formCluster(InputStream config) throws IOException {
94 JsonCodec<ControllerNode> codec = codec(ControllerNode.class);
95 ObjectNode root = (ObjectNode) mapper().readTree(config);
Thomas Vachuskade563cf2015-04-01 00:28:50 -070096
97 List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this);
Thiago Santos7a174cf2016-09-01 14:56:54 -030098 JsonNode partitionSizeNode = root.get("partitionSize");
99 if (partitionSizeNode != null) {
100 int partitionSize = partitionSizeNode.asInt();
101 if (partitionSize == 0) {
102 return Response.notAcceptable(null).build();
103 }
104 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes), partitionSize);
105 } else {
106 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes));
107 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700108
109 return Response.ok().build();
110 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700111}