blob: cbc173f21ae1e13d7ac9f650e313ef47ad0711c9 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08003 *
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;
17
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070018import org.onosproject.rest.AbstractInjectionResource;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080019import org.onosproject.ui.UiExtension;
20import org.onosproject.ui.UiExtensionService;
21import org.onosproject.ui.UiView;
Simon Hunt23f9c7b2017-07-10 20:00:30 -070022import org.onosproject.ui.lion.LionBundle;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080023
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.Produces;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29import java.io.ByteArrayInputStream;
30import java.io.IOException;
31import java.io.InputStream;
32import java.io.SequenceInputStream;
Simon Hunt38c2b6a2015-04-24 13:02:05 -070033import java.util.ArrayList;
34import java.util.List;
35import java.util.stream.Collectors;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080036
37import static com.google.common.collect.ImmutableList.of;
38import static com.google.common.io.ByteStreams.toByteArray;
39
40/**
Thomas Vachuskae95da772015-02-23 15:50:11 -080041 * Resource for serving the dynamically composed nav.html.
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080042 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080043@Path("/")
Thomas Vachuskae95da772015-02-23 15:50:11 -080044public class MainNavResource extends AbstractInjectionResource {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080045
Thomas Vachuskae95da772015-02-23 15:50:11 -080046 private static final String NAV_HTML = "nav.html";
Thomas Vachuskaa0509892015-02-21 22:18:41 -080047
Simon Hunt38c2b6a2015-04-24 13:02:05 -070048 private static final String INJECT_VIEW_ITEMS_START =
49 "<!-- {INJECTED-VIEW-NAV-START} -->";
50 private static final String INJECT_VIEW_ITEMS_END =
51 "<!-- {INJECTED-VIEW-NAV-END} -->";
Thomas Vachuskaa0509892015-02-21 22:18:41 -080052
Simon Hunt38c2b6a2015-04-24 13:02:05 -070053 private static final String HDR_FORMAT =
Phil Huang111bc592016-01-15 01:55:00 +080054 "<div class=\"nav-hdr\">%s</div>%n";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080055 private static final String NAV_FORMAT =
Phil Huang111bc592016-01-15 01:55:00 +080056 "<a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s %s</a>%n";
Simon Huntfb35b832016-05-26 11:43:00 -070057 private static final String ICON_FORMAT =
58 "<div icon icon-id=\"%s\"></div>";
Simon Hunt20e16792015-04-24 14:29:39 -070059
60 private static final String BLANK_GLYPH = "unknown";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080061
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080062 @GET
63 @Produces(MediaType.TEXT_HTML)
64 public Response getNavigation() throws IOException {
65 UiExtensionService service = get(UiExtensionService.class);
Simon Hunt38c2b6a2015-04-24 13:02:05 -070066 InputStream navTemplate =
67 getClass().getClassLoader().getResourceAsStream(NAV_HTML);
68 String html = new String(toByteArray(navTemplate));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080069
Simon Hunt38c2b6a2015-04-24 13:02:05 -070070 int p1s = split(html, 0, INJECT_VIEW_ITEMS_START);
71 int p1e = split(html, 0, INJECT_VIEW_ITEMS_END);
72 int p2s = split(html, p1e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080073
74 StreamEnumeration streams =
Simon Hunt38c2b6a2015-04-24 13:02:05 -070075 new StreamEnumeration(of(stream(html, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080076 includeNavItems(service),
Simon Hunt38c2b6a2015-04-24 13:02:05 -070077 stream(html, p1e, p2s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080078
79 return Response.ok(new SequenceInputStream(streams)).build();
80 }
81
Simon Hunt38c2b6a2015-04-24 13:02:05 -070082 // Produces an input stream of nav item injections from all extensions.
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080083 private InputStream includeNavItems(UiExtensionService service) {
Simon Hunt38c2b6a2015-04-24 13:02:05 -070084 List<UiExtension> extensions = service.getExtensions();
Simon Hunt23f9c7b2017-07-10 20:00:30 -070085 LionBundle navLion = service.getNavLionBundle();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080086 StringBuilder sb = new StringBuilder("\n");
Simon Hunt38c2b6a2015-04-24 13:02:05 -070087
88 for (UiView.Category cat : UiView.Category.values()) {
89 if (cat == UiView.Category.HIDDEN) {
90 continue;
91 }
92
93 List<UiView> catViews = getViewsForCat(extensions, cat);
94 if (!catViews.isEmpty()) {
Simon Hunt23f9c7b2017-07-10 20:00:30 -070095 addCatHeader(sb, cat, navLion);
Simon Hunt38c2b6a2015-04-24 13:02:05 -070096 addCatItems(sb, catViews);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080097 }
98 }
Simon Hunt38c2b6a2015-04-24 13:02:05 -070099
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800100 return new ByteArrayInputStream(sb.toString().getBytes());
101 }
Simon Hunt38c2b6a2015-04-24 13:02:05 -0700102
103 private List<UiView> getViewsForCat(List<UiExtension> extensions,
104 UiView.Category cat) {
105 List<UiView> views = new ArrayList<>();
106 for (UiExtension extension : extensions) {
107 views.addAll(extension.views().stream().filter(
108 view -> cat.equals(view.category())
109 ).collect(Collectors.toList()));
110 }
111 return views;
112 }
113
Simon Hunt23f9c7b2017-07-10 20:00:30 -0700114 private void addCatHeader(StringBuilder sb, UiView.Category cat,
115 LionBundle navLion) {
116 String key = "cat_" + cat.name().toLowerCase();
117 sb.append(String.format(HDR_FORMAT, navLion.getValue(key)));
Simon Hunt38c2b6a2015-04-24 13:02:05 -0700118 }
119
120 private void addCatItems(StringBuilder sb, List<UiView> catViews) {
121 for (UiView view : catViews) {
Simon Hunt20e16792015-04-24 14:29:39 -0700122 sb.append(String.format(NAV_FORMAT, view.id(), icon(view), view.label()));
Simon Hunt38c2b6a2015-04-24 13:02:05 -0700123 }
124 }
Simon Hunt20e16792015-04-24 14:29:39 -0700125
126 private String icon(UiView view) {
127 String gid = view.iconId() == null ? BLANK_GLYPH : view.iconId();
Simon Huntfb35b832016-05-26 11:43:00 -0700128 return String.format(ICON_FORMAT, gid);
Simon Hunt20e16792015-04-24 14:29:39 -0700129 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800130}