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 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.Intent; |
| 27 | import org.onosproject.net.intent.IntentId; |
| 28 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 30 | |
| 31 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 32 | |
| 33 | /** |
| 34 | * REST resource for interacting with the inventory of intents. |
| 35 | */ |
| 36 | |
| 37 | @Path("intents") |
| 38 | public class IntentsWebResource extends AbstractWebResource { |
| 39 | public static final String INTENT_NOT_FOUND = "Intent is not found"; |
| 40 | |
| 41 | /** |
| 42 | * Gets an array containing all the intents in the system. |
| 43 | * |
| 44 | * @return array of all the intents in the system |
| 45 | */ |
| 46 | @GET |
| 47 | @Produces(MediaType.APPLICATION_JSON) |
| 48 | public Response getIntents() { |
| 49 | final Iterable<Intent> intents = get(IntentService.class).getIntents(); |
| 50 | final ObjectNode root = encodeArray(Intent.class, "intents", intents); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 51 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Gets a single intent by Id. |
| 56 | * |
| 57 | * @param id Id to look up |
| 58 | * @return intent data |
| 59 | */ |
| 60 | @GET |
| 61 | @Produces(MediaType.APPLICATION_JSON) |
| 62 | @Path("{id}") |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 63 | public Response getIntentById(@PathParam("id") long id) { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 64 | final Intent intent = nullIsNotFound(get(IntentService.class) |
| 65 | .getIntent(IntentId.valueOf(id)), |
| 66 | INTENT_NOT_FOUND); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 67 | final ObjectNode root; |
| 68 | if (intent instanceof HostToHostIntent) { |
| 69 | root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this); |
| 70 | } else if (intent instanceof PointToPointIntent) { |
| 71 | root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this); |
| 72 | } else { |
| 73 | root = codec(Intent.class).encode(intent, this); |
| 74 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 75 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 76 | } |
| 77 | } |