blob: 7b2492990ee8c8cce98ad0b14d5bd818bba75db9 [file] [log] [blame]
Ray Milkey1f95bd32014-12-10 11:11:00 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey1f95bd32014-12-10 11:11:00 -08003 *
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 Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.net.Host;
20import org.onosproject.net.host.HostService;
21import org.onosproject.rest.AbstractWebResource;
Ray Milkey1f95bd32014-12-10 11:11:00 -080022
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.PathParam;
26import javax.ws.rs.Produces;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070030import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey1f95bd32014-12-10 11:11:00 -080031import static org.onosproject.net.HostId.hostId;
32
33/**
34 * REST resource for interacting with the inventory of hosts.
35 */
36@Path("hosts")
37public class HostsWebResource extends AbstractWebResource {
38
39 public static final String HOST_NOT_FOUND = "Host is not found";
40
41 @GET
42 @Produces(MediaType.APPLICATION_JSON)
43 public Response getHosts() {
44 final Iterable<Host> hosts = get(HostService.class).getHosts();
45 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080046 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080047 }
48
49 @GET
50 @Produces(MediaType.APPLICATION_JSON)
51 @Path("{id}")
52 public Response getHostById(@PathParam("id") String id) {
53 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
54 HOST_NOT_FOUND);
55 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -080056 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080057 }
58
59 @GET
60 @Produces(MediaType.APPLICATION_JSON)
61 @Path("{mac}/{vlan}")
62 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
63 @PathParam("vlan") String vlan) {
64 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
65 HOST_NOT_FOUND);
66 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -080067 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080068 }
69}
70