Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -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.intent.Intent; |
| 26 | import org.onosproject.net.intent.IntentId; |
| 27 | import org.onosproject.net.intent.IntentService; |
| 28 | |
| 29 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 30 | |
| 31 | /** |
| 32 | * REST resource for interacting with the inventory of intents. |
| 33 | */ |
| 34 | |
| 35 | @Path("intents") |
| 36 | public 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}") |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame^] | 61 | public Response getIntentById(@PathParam("id") long id) { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 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 | } |