Ray Milkey

Implement REST API for Hosts objects

Change-Id: Ie1d4f7c19c47d2d357dedf81016e4d4d197e4052
...@@ -38,6 +38,10 @@ ...@@ -38,6 +38,10 @@
38 <version>${project.version}</version> 38 <version>${project.version}</version>
39 <scope>test</scope> 39 <scope>test</scope>
40 </dependency> 40 </dependency>
41 + <dependency>
42 + <groupId>org.easymock</groupId>
43 + <artifactId>easymock</artifactId>
44 + </dependency>
41 </dependencies> 45 </dependencies>
42 46
43 <properties> 47 <properties>
......
...@@ -25,6 +25,8 @@ import org.onosproject.codec.JsonCodec; ...@@ -25,6 +25,8 @@ import org.onosproject.codec.JsonCodec;
25 import org.onosproject.net.Annotations; 25 import org.onosproject.net.Annotations;
26 import org.onosproject.net.ConnectPoint; 26 import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.Device; 27 import org.onosproject.net.Device;
28 +import org.onosproject.net.Host;
29 +import org.onosproject.net.HostLocation;
28 import org.onosproject.net.Link; 30 import org.onosproject.net.Link;
29 import org.onosproject.net.Port; 31 import org.onosproject.net.Port;
30 import org.slf4j.Logger; 32 import org.slf4j.Logger;
...@@ -53,11 +55,13 @@ public class CodecManager implements CodecService { ...@@ -53,11 +55,13 @@ public class CodecManager implements CodecService {
53 registerCodec(Port.class, new PortCodec()); 55 registerCodec(Port.class, new PortCodec());
54 registerCodec(ConnectPoint.class, new ConnectPointCodec()); 56 registerCodec(ConnectPoint.class, new ConnectPointCodec());
55 registerCodec(Link.class, new LinkCodec()); 57 registerCodec(Link.class, new LinkCodec());
58 + registerCodec(Host.class, new HostCodec());
59 + registerCodec(HostLocation.class, new HostLocationCodec());
56 log.info("Started"); 60 log.info("Started");
57 } 61 }
58 62
59 @Deactivate 63 @Deactivate
60 - public void deativate() { 64 + public void deactivate() {
61 codecs.clear(); 65 codecs.clear();
62 log.info("Stopped"); 66 log.info("Stopped");
63 } 67 }
......
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.codec.impl;
17 +
18 +import org.onlab.packet.IpAddress;
19 +import org.onosproject.codec.CodecContext;
20 +import org.onosproject.codec.JsonCodec;
21 +import org.onosproject.net.Host;
22 +import org.onosproject.net.HostLocation;
23 +
24 +import com.fasterxml.jackson.databind.node.ArrayNode;
25 +import com.fasterxml.jackson.databind.node.ObjectNode;
26 +
27 +import static com.google.common.base.Preconditions.checkNotNull;
28 +
29 +/**
30 + * Host JSON codec.
31 + */
32 +public class HostCodec extends AnnotatedCodec<Host> {
33 +
34 + @Override
35 + public ObjectNode encode(Host host, CodecContext context) {
36 + checkNotNull(host, "Host cannot be null");
37 + final JsonCodec<HostLocation> locationCodec = new HostLocationCodec();
38 + final ObjectNode result = context.mapper().createObjectNode()
39 + .put("id", host.id().toString())
40 + .put("mac", host.mac().toString())
41 + .put("vlan", host.vlan().toString());
42 +
43 + final ArrayNode jsonIpAddresses = result.putArray("ipAddresses");
44 + for (final IpAddress ipAddress : host.ipAddresses()) {
45 + jsonIpAddresses.add(ipAddress.toString());
46 + }
47 + result.set("ipAddresses", jsonIpAddresses);
48 + result.set("location", locationCodec.encode(host.location(), context));
49 +
50 + return annotate(result, host, context);
51 + }
52 +
53 +}
54 +
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.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.HostLocation;
21 +
22 +import com.fasterxml.jackson.databind.node.ObjectNode;
23 +
24 +import static com.google.common.base.Preconditions.checkNotNull;
25 +
26 +/**
27 + * Host JSON codec.
28 + */
29 +public class HostLocationCodec extends JsonCodec<HostLocation> {
30 +
31 + @Override
32 + public ObjectNode encode(HostLocation hostLocation, CodecContext context) {
33 + checkNotNull(hostLocation, "Host location cannot be null");
34 + return context.mapper().createObjectNode()
35 + .put("elementId", hostLocation.elementId().toString())
36 + .put("port", hostLocation.port().toString());
37 + }
38 +
39 +}
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 +
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
37 37
38 org.onosproject.rest.DevicesWebResource, 38 org.onosproject.rest.DevicesWebResource,
39 org.onosproject.rest.LinksWebResource, 39 org.onosproject.rest.LinksWebResource,
40 + org.onosproject.rest.HostsWebResource,
40 org.onosproject.rest.ConfigResource 41 org.onosproject.rest.ConfigResource
41 </param-value> 42 </param-value>
42 </init-param> 43 </init-param>
......
...@@ -45,7 +45,7 @@ public class GreetResourceTest extends JerseyTest { ...@@ -45,7 +45,7 @@ public class GreetResourceTest extends JerseyTest {
45 public void basics() { 45 public void basics() {
46 WebResource rs = resource(); 46 WebResource rs = resource();
47 String response = rs.path("greet").get(String.class); 47 String response = rs.path("greet").get(String.class);
48 - assertTrue("incorrect response", response.contains("Whazup ")); 48 + assertTrue("incorrect response", response.contains("greeting"));
49 } 49 }
50 50
51 } 51 }
......
This diff is collapsed. Click to expand it.