Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 javax.ws.rs.GET; |
| 19 | import javax.ws.rs.Path; |
| 20 | import javax.ws.rs.PathParam; |
| 21 | import javax.ws.rs.Produces; |
| 22 | import javax.ws.rs.core.MediaType; |
| 23 | import javax.ws.rs.core.Response; |
| 24 | |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.host.HostService; |
| 27 | |
| 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 29 | |
| 30 | import static org.onosproject.net.HostId.hostId; |
| 31 | |
| 32 | /** |
| 33 | * REST resource for interacting with the inventory of hosts. |
| 34 | */ |
| 35 | @Path("hosts") |
| 36 | public class HostsWebResource extends AbstractWebResource { |
| 37 | |
| 38 | public static final String HOST_NOT_FOUND = "Host is not found"; |
| 39 | |
| 40 | @GET |
| 41 | @Produces(MediaType.APPLICATION_JSON) |
| 42 | public Response getHosts() { |
| 43 | final Iterable<Host> hosts = get(HostService.class).getHosts(); |
| 44 | final ObjectNode root = encodeArray(Host.class, "hosts", hosts); |
| 45 | return ok(root.toString()).build(); |
| 46 | } |
| 47 | |
| 48 | @GET |
| 49 | @Produces(MediaType.APPLICATION_JSON) |
| 50 | @Path("{id}") |
| 51 | public Response getHostById(@PathParam("id") String id) { |
| 52 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)), |
| 53 | HOST_NOT_FOUND); |
| 54 | final ObjectNode root = codec(Host.class).encode(host, this); |
| 55 | return ok(root.toString()).build(); |
| 56 | } |
| 57 | |
| 58 | @GET |
| 59 | @Produces(MediaType.APPLICATION_JSON) |
| 60 | @Path("{mac}/{vlan}") |
| 61 | public Response getHostByMacAndVlan(@PathParam("mac") String mac, |
| 62 | @PathParam("vlan") String vlan) { |
| 63 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)), |
| 64 | HOST_NOT_FOUND); |
| 65 | final ObjectNode root = codec(Host.class).encode(host, this); |
| 66 | return ok(root.toString()).build(); |
| 67 | } |
| 68 | } |
| 69 | |