Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 1 | /* |
| 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 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 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.cluster.ClusterAdminService; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.ControllerNode; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.codec.JsonCodec; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 25 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 26 | |
| 27 | import javax.ws.rs.GET; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.core.Response; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.util.HashSet; |
| 35 | import java.util.List; |
| 36 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 37 | import static org.onlab.util.Tools.nullIsNotFound; |
| 38 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 39 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 40 | * Manage cluster of ONOS instances. |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 41 | */ |
| 42 | @Path("cluster") |
| 43 | public class ClusterWebResource extends AbstractWebResource { |
| 44 | |
| 45 | public static final String NODE_NOT_FOUND = "Node is not found"; |
| 46 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Get all cluster nodes. |
| 49 | * Returns array of all cluster nodes. |
| 50 | * |
| 51 | * @return 200 OK |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 52 | * @onos.rsModel Cluster |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 53 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 54 | @GET |
| 55 | public Response getClusterNodes() { |
| 56 | Iterable<ControllerNode> nodes = get(ClusterService.class).getNodes(); |
| 57 | return ok(encodeArray(ControllerNode.class, "nodes", nodes)).build(); |
| 58 | } |
| 59 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Get cluster node details. |
| 62 | * Returns details of the specified cluster node. |
| 63 | * |
| 64 | * @param id cluster node identifier |
| 65 | * @return 200 OK |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 66 | * @onos.rsModel ClusterNode |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 67 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 68 | @GET |
| 69 | @Path("{id}") |
| 70 | public Response getClusterNode(@PathParam("id") String id) { |
| 71 | ControllerNode node = nullIsNotFound(get(ClusterService.class).getNode(new NodeId(id)), |
| 72 | NODE_NOT_FOUND); |
| 73 | return ok(codec(ControllerNode.class).encode(node, this)).build(); |
| 74 | } |
| 75 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Forms cluster of ONOS instances. |
| 78 | * Forms ONOS cluster using the uploaded JSON definition. |
| 79 | * |
| 80 | * @param config cluster definition |
| 81 | * @return 200 OK |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 82 | * @throws IOException to signify bad request |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 83 | * @onos.rsModel ClusterPost |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 84 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 85 | @POST |
| 86 | @Path("configuration") |
| 87 | public Response formCluster(InputStream config) throws IOException { |
| 88 | JsonCodec<ControllerNode> codec = codec(ControllerNode.class); |
| 89 | ObjectNode root = (ObjectNode) mapper().readTree(config); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 90 | |
| 91 | List<ControllerNode> nodes = codec.decode((ArrayNode) root.path("nodes"), this); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 92 | get(ClusterAdminService.class).formCluster(new HashSet<>(nodes)); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 93 | |
| 94 | return Response.ok().build(); |
| 95 | } |
| 96 | |
| 97 | } |