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 | */ |
| 16 | package org.onosproject.rest; |
| 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; |
| 25 | |
| 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.Path; |
| 29 | import javax.ws.rs.PathParam; |
| 30 | import javax.ws.rs.core.Response; |
| 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
| 33 | import java.util.HashSet; |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
| 37 | * REST resource for interacting with the ONOS cluster subsystem. |
| 38 | */ |
| 39 | @Path("cluster") |
| 40 | public 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 | } |