blob: 5d1c2f0baeae4254cf63c883cff404776b563aeb [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
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080027import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080028import org.onosproject.net.intent.Intent;
Ray Milkey2b217142014-12-15 09:24:24 -080029import org.onosproject.net.intent.IntentService;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080030import org.onosproject.net.intent.Key;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080031import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080032
33import com.fasterxml.jackson.databind.node.ObjectNode;
34
35/**
36 * REST resource for interacting with the inventory of intents.
37 */
38
39@Path("intents")
40public 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 Milkey3f025692015-01-26 11:15:41 -080053 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -080054 }
55
56 /**
57 * Gets a single intent by Id.
58 *
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080059 * @param appId the Application ID
60 * @param key the Intent key value to look up
Ray Milkey2b217142014-12-15 09:24:24 -080061 * @return intent data
62 */
63 @GET
64 @Produces(MediaType.APPLICATION_JSON)
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080065 @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 HIGUCHI179f3772015-02-18 17:28:39 +090073 .getIntent(Key.of(Long.parseLong(key), app));
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080074 }
75 nullIsNotFound(intent, INTENT_NOT_FOUND);
76
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080077 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 Milkey3f025692015-01-26 11:15:41 -080085 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -080086 }
87}