blob: 5f2fde9e51a0ed08786955384a4c214af1101e02 [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
36/**
37 * REST resource for interacting with the ONOS cluster subsystem.
38 */
39@Path("cluster")
40public class ClusterWebResource extends AbstractWebResource {
41
42 public static final String NODE_NOT_FOUND = "Node is not found";
43
44 @GET
45 public Response getClusterNodes() {
46 Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes();
47 return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build();
48 }
49
50 @GET
51 @Path("{id}")
52 public Response getClusterNode(@PathParam("id") String id) {
53 ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)),
54 NODE_NOT_FOUND);
55 return ok(codec(ControllerNode.class).encode(node, this)).build();
56 }
57
58 @POST
59 @Path("configuration")
60 public Response formCluster(InputStream config) throws IOException {
61 JsonCodec<ControllerNode> codec = codec(ControllerNode.class);
62 ObjectNode root = (ObjectNode) mapper().readTree(config);
63 String ipPrefix = root.path("ipPrefix").asText();
64
65 List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this);
66 get(ClusterAdminService.class).formCluster(new HashSet<>(nodes), ipPrefix);
67
68 return Response.ok().build();
69 }
70
71}