blob: 7f15bdc5a8a305810129f66f7e6172deab06f4c5 [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 */
16package org.onosproject.rest;
17
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;
25
26import javax.ws.rs.GET;
27import javax.ws.rs.POST;
28import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
30import javax.ws.rs.core.Response;
31import java.io.IOException;
32import java.io.InputStream;
33import java.util.HashSet;
34import java.util.List;
35
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070036import static org.onlab.util.Tools.nullIsNotFound;
37
Thomas Vachuskade563cf2015-04-01 00:28:50 -070038/**
39 * REST resource for interacting with the ONOS cluster subsystem.
40 */
41@Path("cluster")
42public class ClusterWebResource extends AbstractWebResource {
43
44 public static final String NODE_NOT_FOUND = "Node is not found";
45
46 @GET
47 public Response getClusterNodes() {
48 Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes();
49 return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build();
50 }
51
52 @GET
53 @Path("{id}")
54 public Response getClusterNode(@PathParam("id") String id) {
55 ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)),
56 NODE_NOT_FOUND);
57 return ok(codec(ControllerNode.class).encode(node, this)).build();
58 }
59
60 @POST
61 @Path("configuration")
62 public Response formCluster(InputStream config) throws IOException {
63 JsonCodec<ControllerNode> codec = codec(ControllerNode.class);
64 ObjectNode root = (ObjectNode) mapper().readTree(config);
65 String ipPrefix = root.path("ipPrefix").asText();
66
67 List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this);
68 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes), ipPrefix);
69
70 return Response.ok().build();
71 }
72
73}