blob: 664f0ebb89e6bc6596fc75293b542dec1170d5b5 [file] [log] [blame]
Thomas Vachuskade563cf2015-04-01 00:28:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyb784adb2018-04-02 15:33:07 -070041import static org.onlab.util.Tools.readTreeFromStream;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070042
Thomas Vachuskade563cf2015-04-01 00:28:50 -070043/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 * Manage cluster of ONOS instances.
Thomas Vachuskade563cf2015-04-01 00:28:50 -070045 */
46@Path("cluster")
47public class ClusterWebResource extends AbstractWebResource {
48
Jian Licc730a62016-05-10 16:36:16 -070049 private static final String NODE_NOT_FOUND = "Node is not found";
Thomas Vachuskade563cf2015-04-01 00:28:50 -070050
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070051 /**
52 * Get all cluster nodes.
53 * Returns array of all cluster nodes.
54 *
Jian Licc730a62016-05-10 16:36:16 -070055 * @return 200 OK with a collection of cluster nodes
Andrea Campanella10c4adc2015-12-03 15:27:54 -080056 * @onos.rsModel Cluster
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070057 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070058 @GET
Jian Licc730a62016-05-10 16:36:16 -070059 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070060 public Response getClusterNodes() {
61 Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes();
62 return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build();
63 }
64
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 /**
66 * Get cluster node details.
67 * Returns details of the specified cluster node.
68 *
69 * @param id cluster node identifier
Jian Licc730a62016-05-10 16:36:16 -070070 * @return 200 OK with a cluster node
Andrea Campanella10c4adc2015-12-03 15:27:54 -080071 * @onos.rsModel ClusterNode
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070072 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070073 @GET
74 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070075 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070076 public Response getClusterNode(@PathParam("id") String id) {
77 ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)),
78 NODE_NOT_FOUND);
79 return ok(codec(ControllerNode.class).encode(node, this)).build();
80 }
81
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070082 /**
83 * Forms cluster of ONOS instances.
84 * Forms ONOS cluster using the uploaded JSON definition.
85 *
86 * @param config cluster definition
87 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070088 * @throws IOException to signify bad request
Andrea Campanella10c4adc2015-12-03 15:27:54 -080089 * @onos.rsModel ClusterPost
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070090 */
Thomas Vachuskade563cf2015-04-01 00:28:50 -070091 @POST
92 @Path("configuration")
Jian Licc730a62016-05-10 16:36:16 -070093 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskade563cf2015-04-01 00:28:50 -070094 public Response formCluster(InputStream config) throws IOException {
95 JsonCodec<ControllerNode> codec = codec(ControllerNode.class);
Ray Milkeyb784adb2018-04-02 15:33:07 -070096 ObjectNode root = readTreeFromStream(mapper(), config);
Thomas Vachuskade563cf2015-04-01 00:28:50 -070097
98 List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this);
Thiago Santos7a174cf2016-09-01 14:56:54 -030099 JsonNode partitionSizeNode = root.get("partitionSize");
100 if (partitionSizeNode != null) {
101 int partitionSize = partitionSizeNode.asInt();
102 if (partitionSize == 0) {
103 return Response.notAcceptable(null).build();
104 }
105 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes), partitionSize);
106 } else {
107 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes));
108 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700109
110 return Response.ok().build();
111 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700112}