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