blob: 48c6c897a78a89d62aaed7ebf343352359bd08c2 [file] [log] [blame]
Sean Condon87b78502018-09-17 20:53:24 +01001/*
2 * Copyright 2018-present Open Networking Foundation
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.ui.impl.gui2;
17
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.module.SimpleModule;
21import org.onlab.rest.BaseResource;
22import org.onosproject.ui.UiExtension;
23import org.onosproject.ui.UiExtensionService;
24import org.onosproject.ui.UiView;
25
26import javax.ws.rs.GET;
27import javax.ws.rs.Path;
28import javax.ws.rs.Produces;
29import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.Response;
31
32/**
33 * Resource for serving the list of UIExtensions.
34 */
35@Path("nav")
36public class NavResource extends BaseResource {
37
38 @GET
39 @Path("uiextensions")
40 @Produces(MediaType.APPLICATION_JSON)
41 public Response getNavigation() throws JsonProcessingException {
42 UiExtensionService service = get(UiExtensionService.class);
43 UiViewSerializer serializer = new UiViewSerializer(UiView.class);
44 ObjectMapper mapper = new ObjectMapper();
45
46 SimpleModule module =
47 new SimpleModule("UiViewSerializer");
48 module.addSerializer(serializer);
49 mapper.registerModule(module);
50 StringBuilder sb = new StringBuilder("[");
51 boolean first = true;
52 for (UiExtension uiExt : service.getExtensions()) {
53 for (UiView view : uiExt.views()) {
54 if (first) {
55 first = false;
56 } else {
57 sb.append(",");
58 }
59 sb.append(mapper.writeValueAsString(view));
60 }
61 }
62 sb.append("]");
63
64 return ok(sb.toString()).build();
65 }
66
67}