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; |
| 41 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 42 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 43 | * Manage cluster of ONOS instances. |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 44 | */ |
| 45 | @Path("cluster") |
| 46 | public class ClusterWebResource extends AbstractWebResource { |
| 47 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 48 | private static final String NODE_NOT_FOUND = "Node is not found"; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 49 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Get all cluster nodes. |
| 52 | * Returns array of all cluster nodes. |
| 53 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 54 | * @return 200 OK with a collection of cluster nodes |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 55 | * @onos.rsModel Cluster |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 56 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 57 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 58 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 59 | public Response getClusterNodes() { |
| 60 | Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes(); |
| 61 | return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build(); |
| 62 | } |
| 63 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Get cluster node details. |
| 66 | * Returns details of the specified cluster node. |
| 67 | * |
| 68 | * @param id cluster node identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 69 | * @return 200 OK with a cluster node |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 70 | * @onos.rsModel ClusterNode |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 71 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 72 | @GET |
| 73 | @Path("{id}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 74 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 75 | public Response getClusterNode(@PathParam("id") String id) { |
| 76 | ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)), |
| 77 | NODE_NOT_FOUND); |
| 78 | return ok(codec(ControllerNode.class).encode(node, this)).build(); |
| 79 | } |
| 80 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Forms cluster of ONOS instances. |
| 83 | * Forms ONOS cluster using the uploaded JSON definition. |
| 84 | * |
| 85 | * @param config cluster definition |
| 86 | * @return 200 OK |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 87 | * @throws IOException to signify bad request |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 88 | * @onos.rsModel ClusterPost |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 89 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 90 | @POST |
| 91 | @Path("configuration") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 92 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 93 | public Response formCluster(InputStream config) throws IOException { |
| 94 | JsonCodec<ControllerNode> codec = codec(ControllerNode.class); |
| 95 | ObjectNode root = (ObjectNode) mapper().readTree(config); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 96 | |
| 97 | List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this); |
Thiago Santos | 7a174cf | 2016-09-01 14:56:54 -0300 | [diff] [blame] | 98 | JsonNode partitionSizeNode = root.get("partitionSize"); |
| 99 | if (partitionSizeNode != null) { |
| 100 | int partitionSize = partitionSizeNode.asInt(); |
| 101 | if (partitionSize == 0) { |
| 102 | return Response.notAcceptable(null).build(); |
| 103 | } |
| 104 | get(ClusterAdminService.class).formCluster(new HashSet<>(nodes), partitionSize); |
| 105 | } else { |
| 106 | get(ClusterAdminService.class).formCluster(new HashSet<>(nodes)); |
| 107 | } |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 108 | |
| 109 | return Response.ok().build(); |
| 110 | } |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 111 | } |