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 | |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Key; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 32 | |
| 33 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 34 | |
| 35 | /** |
| 36 | * REST resource for interacting with the inventory of intents. |
| 37 | */ |
| 38 | |
| 39 | @Path("intents") |
| 40 | public class IntentsWebResource extends AbstractWebResource { |
| 41 | public static final String INTENT_NOT_FOUND = "Intent is not found"; |
| 42 | |
| 43 | /** |
| 44 | * Gets an array containing all the intents in the system. |
| 45 | * |
| 46 | * @return array of all the intents in the system |
| 47 | */ |
| 48 | @GET |
| 49 | @Produces(MediaType.APPLICATION_JSON) |
| 50 | public Response getIntents() { |
| 51 | final Iterable<Intent> intents = get(IntentService.class).getIntents(); |
| 52 | final ObjectNode root = encodeArray(Intent.class, "intents", intents); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 53 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Gets a single intent by Id. |
| 58 | * |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 59 | * @param appId the Application ID |
| 60 | * @param key the Intent key value to look up |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 61 | * @return intent data |
| 62 | */ |
| 63 | @GET |
| 64 | @Produces(MediaType.APPLICATION_JSON) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 65 | @Path("{appId}/{key}") |
| 66 | public Response getIntentById(@PathParam("appId") Short appId, |
| 67 | @PathParam("key") String key) { |
| 68 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
| 69 | |
| 70 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 71 | if (intent == null) { |
| 72 | intent = get(IntentService.class) |
Yuta HIGUCHI | 179f377 | 2015-02-18 17:28:39 +0900 | [diff] [blame] | 73 | .getIntent(Key.of(Long.parseLong(key), app)); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 74 | } |
| 75 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 76 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 77 | final ObjectNode root; |
| 78 | if (intent instanceof HostToHostIntent) { |
| 79 | root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this); |
| 80 | } else if (intent instanceof PointToPointIntent) { |
| 81 | root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this); |
| 82 | } else { |
| 83 | root = codec(Intent.class).encode(intent, this); |
| 84 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 85 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 86 | } |
| 87 | } |