blob: 75287e0290c19d1b8735d61aa2b0574476221173 [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
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080025import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080026import org.onosproject.net.intent.Intent;
27import org.onosproject.net.intent.IntentId;
28import org.onosproject.net.intent.IntentService;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080029import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080030
31import com.fasterxml.jackson.databind.node.ObjectNode;
32
33/**
34 * REST resource for interacting with the inventory of intents.
35 */
36
37@Path("intents")
38public 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 Milkey3f025692015-01-26 11:15:41 -080051 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -080052 }
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 Milkey4f5de002014-12-17 19:26:11 -080063 public Response getIntentById(@PathParam("id") long id) {
Ray Milkey2b217142014-12-15 09:24:24 -080064 final Intent intent = nullIsNotFound(get(IntentService.class)
65 .getIntent(IntentId.valueOf(id)),
66 INTENT_NOT_FOUND);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080067 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 Milkey3f025692015-01-26 11:15:41 -080075 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -080076 }
77}