Implement GET REST API for intents
- GET /intent and GET /intent/id implemented - Basic codec for Intents - Unit tests Change-Id: Idbe59d4f7a792c88c77d6f3498d01d4cb7c4467d
Showing
5 changed files
with
122 additions
and
0 deletions
... | @@ -29,6 +29,7 @@ import org.onosproject.net.Host; | ... | @@ -29,6 +29,7 @@ import org.onosproject.net.Host; |
29 | import org.onosproject.net.HostLocation; | 29 | import org.onosproject.net.HostLocation; |
30 | import org.onosproject.net.Link; | 30 | import org.onosproject.net.Link; |
31 | import org.onosproject.net.Port; | 31 | import org.onosproject.net.Port; |
32 | +import org.onosproject.net.intent.Intent; | ||
32 | import org.slf4j.Logger; | 33 | import org.slf4j.Logger; |
33 | import org.slf4j.LoggerFactory; | 34 | import org.slf4j.LoggerFactory; |
34 | 35 | ||
... | @@ -57,6 +58,7 @@ public class CodecManager implements CodecService { | ... | @@ -57,6 +58,7 @@ public class CodecManager implements CodecService { |
57 | registerCodec(Link.class, new LinkCodec()); | 58 | registerCodec(Link.class, new LinkCodec()); |
58 | registerCodec(Host.class, new HostCodec()); | 59 | registerCodec(Host.class, new HostCodec()); |
59 | registerCodec(HostLocation.class, new HostLocationCodec()); | 60 | registerCodec(HostLocation.class, new HostLocationCodec()); |
61 | + registerCodec(Intent.class, new IntentCodec()); | ||
60 | log.info("Started"); | 62 | log.info("Started"); |
61 | } | 63 | } |
62 | 64 | ... | ... |
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.codec.impl; | ||
17 | + | ||
18 | +import org.onosproject.codec.CodecContext; | ||
19 | +import org.onosproject.codec.JsonCodec; | ||
20 | +import org.onosproject.net.NetworkResource; | ||
21 | +import org.onosproject.net.intent.Intent; | ||
22 | + | ||
23 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
24 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
25 | + | ||
26 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
27 | + | ||
28 | +/** | ||
29 | + * Intent JSON codec. | ||
30 | + */ | ||
31 | +public class IntentCodec extends JsonCodec<Intent> { | ||
32 | + | ||
33 | + @Override | ||
34 | + public ObjectNode encode(Intent intent, CodecContext context) { | ||
35 | + checkNotNull(intent, "Intent cannot be null"); | ||
36 | + final ObjectNode result = context.mapper().createObjectNode() | ||
37 | + .put("type", intent.getClass().getSimpleName()) | ||
38 | + .put("id", intent.id().toString()) | ||
39 | + .put("appId", intent.appId().toString()) | ||
40 | + .put("details", intent.toString()); | ||
41 | + | ||
42 | + final ArrayNode jsonResources = result.putArray("resources"); | ||
43 | + | ||
44 | + if (intent.resources() != null) { | ||
45 | + for (final NetworkResource resource : intent.resources()) { | ||
46 | + jsonResources.add(resource.toString()); | ||
47 | + } | ||
48 | + } | ||
49 | + return result; | ||
50 | + } | ||
51 | +} |
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}") | ||
61 | + public Response getHostById(@PathParam("id") long id) { | ||
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 | +} |
... | @@ -38,6 +38,7 @@ | ... | @@ -38,6 +38,7 @@ |
38 | org.onosproject.rest.DevicesWebResource, | 38 | org.onosproject.rest.DevicesWebResource, |
39 | org.onosproject.rest.LinksWebResource, | 39 | org.onosproject.rest.LinksWebResource, |
40 | org.onosproject.rest.HostsWebResource, | 40 | org.onosproject.rest.HostsWebResource, |
41 | + org.onosproject.rest.IntentsWebResource, | ||
41 | org.onosproject.rest.ConfigResource | 42 | org.onosproject.rest.ConfigResource |
42 | </param-value> | 43 | </param-value> |
43 | </init-param> | 44 | </init-param> | ... | ... |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment