blob: 361859d334f682eaa64334ac9957225520efd479 [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
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 */
16package org.onosproject.rest;
17
18import javax.ws.rs.GET;
19import javax.ws.rs.Path;
20import javax.ws.rs.PathParam;
21import javax.ws.rs.Produces;
22import javax.ws.rs.core.MediaType;
23import javax.ws.rs.core.Response;
24
25import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentId;
27import org.onosproject.net.intent.IntentService;
28
29import com.fasterxml.jackson.databind.node.ObjectNode;
30
31/**
32 * REST resource for interacting with the inventory of intents.
33 */
34
35@Path("intents")
36public class IntentsWebResource extends AbstractWebResource {
37 public static final String INTENT_NOT_FOUND = "Intent is not found";
38
39 /**
40 * Gets an array containing all the intents in the system.
41 *
42 * @return array of all the intents in the system
43 */
44 @GET
45 @Produces(MediaType.APPLICATION_JSON)
46 public Response getIntents() {
47 final Iterable<Intent> intents = get(IntentService.class).getIntents();
48 final ObjectNode root = encodeArray(Intent.class, "intents", intents);
49 return ok(root.toString()).build();
50 }
51
52 /**
53 * Gets a single intent by Id.
54 *
55 * @param id Id to look up
56 * @return intent data
57 */
58 @GET
59 @Produces(MediaType.APPLICATION_JSON)
60 @Path("{id}")
61 public Response getHostById(@PathParam("id") long id) {
62 final Intent intent = nullIsNotFound(get(IntentService.class)
63 .getIntent(IntentId.valueOf(id)),
64 INTENT_NOT_FOUND);
65 final ObjectNode root = codec(Intent.class).encode(intent, this);
66 return ok(root.toString()).build();
67 }
68}