Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 17 | |
Thiago Santos | 7a174cf | 2016-09-01 14:56:54 -0300 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.cluster.ClusterAdminService; |
| 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.ControllerNode; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.codec.JsonCodec; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 26 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 27 | |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 34 | import javax.ws.rs.core.Response; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.HashSet; |
| 38 | import java.util.List; |
| 39 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 40 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 41 | import static org.onlab.util.Tools.readTreeFromStream; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 42 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 43 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 44 | * Manage cluster of ONOS instances. |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 45 | */ |
| 46 | @Path("cluster") |
| 47 | public class ClusterWebResource extends AbstractWebResource { |
| 48 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 49 | private static final String NODE_NOT_FOUND = "Node is not found"; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 50 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Get all cluster nodes. |
| 53 | * Returns array of all cluster nodes. |
| 54 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 55 | * @return 200 OK with a collection of cluster nodes |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 56 | * @onos.rsModel Cluster |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 57 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 58 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 59 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 60 | public Response getClusterNodes() { |
| 61 | Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes(); |
| 62 | return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build(); |
| 63 | } |
| 64 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Get cluster node details. |
| 67 | * Returns details of the specified cluster node. |
| 68 | * |
| 69 | * @param id cluster node identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 70 | * @return 200 OK with a cluster node |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 71 | * @onos.rsModel ClusterNode |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 72 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 73 | @GET |
| 74 | @Path("{id}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 75 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 76 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 82 | /** |
| 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 Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 88 | * @throws IOException to signify bad request |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 89 | * @onos.rsModel ClusterPost |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 90 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 91 | @POST |
| 92 | @Path("configuration") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 93 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 94 | public Response formCluster(InputStream config) throws IOException { |
| 95 | JsonCodec<ControllerNode> codec = codec(ControllerNode.class); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 96 | ObjectNode root = readTreeFromStream(mapper(), config); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 97 | |
| 98 | List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this); |
Thiago Santos | 7a174cf | 2016-09-01 14:56:54 -0300 | [diff] [blame] | 99 | 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 Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 109 | |
| 110 | return Response.ok().build(); |
| 111 | } |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 112 | } |