blob: 43b871ba24c59261f511d4170f1a5ed302f39291 [file] [log] [blame]
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07003 *
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
Jian Lia54de5a2016-01-20 23:10:39 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070021import org.onosproject.app.ApplicationAdminService;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070022import org.onosproject.app.ApplicationService;
23import org.onosproject.app.ApplicationState;
24import org.onosproject.core.Application;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070025import org.onosproject.core.ApplicationId;
Simon Huntd2747a02015-04-30 22:41:16 -070026import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070027import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070028import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070029import org.onosproject.ui.table.TableRequestHandler;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070030
Simon Huntd2747a02015-04-30 22:41:16 -070031import java.util.Collection;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070032
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -080033import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070034import static org.onosproject.app.ApplicationState.ACTIVE;
35
36/**
37 * Message handler for application view related messages.
38 */
Simon Hunta0ddb022015-05-01 09:53:01 -070039public class ApplicationViewMessageHandler extends UiMessageHandler {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070040
Simon Huntd2747a02015-04-30 22:41:16 -070041 private static final String APP_DATA_REQ = "appDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070042 private static final String APP_DATA_RESP = "appDataResponse";
43 private static final String APPS = "apps";
44
Simon Huntd2747a02015-04-30 22:41:16 -070045 private static final String APP_MGMT_REQ = "appManagementRequest";
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070046
Jian Lia54de5a2016-01-20 23:10:39 -080047 private static final String APP_DETAILS_REQ = "appDetailsRequest";
48 private static final String APP_DETAILS_RESP = "appDetailsResponse";
49 private static final String DETAILS = "details";
50
Simon Huntabd16f62015-05-01 13:14:40 -070051 private static final String STATE = "state";
52 private static final String STATE_IID = "_iconid_state";
53 private static final String ID = "id";
Jian Li97d6b2d2016-01-20 10:13:43 -080054 private static final String ICON = "icon";
Simon Huntabd16f62015-05-01 13:14:40 -070055 private static final String VERSION = "version";
Jian Li97d6b2d2016-01-20 10:13:43 -080056 private static final String CATEGORY = "category";
Simon Huntabd16f62015-05-01 13:14:40 -070057 private static final String ORIGIN = "origin";
Simon Huntafae2f72016-03-04 21:18:23 -080058 private static final String TITLE = "title";
Simon Huntabd16f62015-05-01 13:14:40 -070059 private static final String DESC = "desc";
Jian Li97d6b2d2016-01-20 10:13:43 -080060 private static final String URL = "url";
Jian Lia54de5a2016-01-20 23:10:39 -080061 private static final String README = "readme";
62 private static final String ROLE = "role";
Simon Hunt31642932016-01-22 20:34:00 -080063 private static final String REQUIRED_APPS = "required_apps";
Jian Lia54de5a2016-01-20 23:10:39 -080064 private static final String FEATURES = "features";
Jian Lida253e02016-01-21 17:46:17 -080065 private static final String PERMISSIONS = "permissions";
Simon Huntabd16f62015-05-01 13:14:40 -070066
67 private static final String ICON_ID_ACTIVE = "active";
68 private static final String ICON_ID_INACTIVE = "appInactive";
69
Simon Hunt3d1b0652015-05-05 17:27:24 -070070 private static final String[] COL_IDS = {
Simon Huntafae2f72016-03-04 21:18:23 -080071 STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, TITLE, DESC,
Jian Lida253e02016-01-21 17:46:17 -080072 URL, README, ROLE, REQUIRED_APPS, FEATURES, PERMISSIONS
Simon Hunt3d1b0652015-05-05 17:27:24 -070073 };
74
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070075 @Override
Simon Huntda580882015-05-12 20:58:18 -070076 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070077 return ImmutableSet.of(
78 new AppDataRequest(),
Jian Lia54de5a2016-01-20 23:10:39 -080079 new AppMgmtRequest(),
80 new DetailRequestHandler()
Simon Huntd2747a02015-04-30 22:41:16 -070081 );
82 }
83
Simon Huntabd16f62015-05-01 13:14:40 -070084 // handler for application table requests
85 private final class AppDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080086 private static final String NO_ROWS_MESSAGE = "No applications found";
87
Simon Huntd2747a02015-04-30 22:41:16 -070088 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070089 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070090 }
91
92 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070093 protected String[] getColumnIds() {
94 return COL_IDS;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070095 }
Simon Huntabd16f62015-05-01 13:14:40 -070096
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 @Override
Jian Li8baf4472016-01-15 15:08:09 -080098 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080099 return NO_ROWS_MESSAGE;
100 }
101
102 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700103 protected void populateTable(TableModel tm, ObjectNode payload) {
104 ApplicationService as = get(ApplicationService.class);
105 for (Application app : as.getApplications()) {
106 populateRow(tm.addRow(), app, as);
107 }
108 }
109
110 private void populateRow(TableModel.Row row, Application app,
111 ApplicationService as) {
112 ApplicationId id = app.id();
113 ApplicationState state = as.getState(id);
114 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
115
116 row.cell(STATE, state)
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -0800117 .cell(STATE_IID, iconId)
118 .cell(ID, id.name())
119 .cell(ICON, id.name())
120 .cell(VERSION, app.version())
121 .cell(CATEGORY, app.category())
122 .cell(ORIGIN, app.origin())
123 .cell(TITLE, app.title())
124 .cell(DESC, app.description())
125 .cell(URL, app.url());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700126 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700127 }
128
Simon Huntabd16f62015-05-01 13:14:40 -0700129 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -0700130 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700131 private AppMgmtRequest() {
132 super(APP_MGMT_REQ);
133 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700134
Simon Huntd2747a02015-04-30 22:41:16 -0700135 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800136 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700137 String action = string(payload, "action");
138 String name = string(payload, "name");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800139
Simon Huntd2747a02015-04-30 22:41:16 -0700140 if (action != null && name != null) {
141 ApplicationAdminService service = get(ApplicationAdminService.class);
142 ApplicationId appId = service.getId(name);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800143 switch (action) {
144 case "activate":
145 service.activate(appId);
146 break;
147 case "deactivate":
148 service.deactivate(appId);
149 break;
150 case "uninstall":
151 service.uninstall(appId);
152 break;
153 default:
154 break;
Simon Huntd2747a02015-04-30 22:41:16 -0700155 }
Simon Hunt8a0429a2017-01-06 16:52:47 -0800156 chain(APP_DATA_REQ, payload);
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700157 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700158 }
159 }
Jian Lia54de5a2016-01-20 23:10:39 -0800160
161 // handler for selected application detail requests
162 private final class DetailRequestHandler extends RequestHandler {
163 private DetailRequestHandler() {
164 super(APP_DETAILS_REQ);
165 }
166
167 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800168 public void process(ObjectNode payload) {
Jian Lia54de5a2016-01-20 23:10:39 -0800169 String id = string(payload, ID);
170 ApplicationService as = get(ApplicationService.class);
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -0800171
172 // If the ID was not specified in the payload, use the name of the
173 // most recently uploaded app.
174 if (isNullOrEmpty(id)) {
175 id = ApplicationResource.lastInstalledAppName;
176 }
177
Jian Lia54de5a2016-01-20 23:10:39 -0800178 ApplicationId appId = as.getId(id);
179 ApplicationState state = as.getState(appId);
180 Application app = as.getApplication(appId);
181 ObjectNode data = objectNode();
182
183 data.put(STATE, state.toString());
184 data.put(ID, appId.name());
185 data.put(VERSION, app.version().toString());
186 data.put(ROLE, app.role().toString());
187 data.put(CATEGORY, app.category());
Simon Huntafae2f72016-03-04 21:18:23 -0800188 data.put(TITLE, app.title());
Jian Lia54de5a2016-01-20 23:10:39 -0800189 data.put(ORIGIN, app.origin());
190 data.put(README, app.readme());
Jian Lida253e02016-01-21 17:46:17 -0800191 data.put(DESC, app.description());
Jian Lia54de5a2016-01-20 23:10:39 -0800192 data.put(URL, app.url());
193
194 // process required applications
195 ArrayNode requiredApps = arrayNode();
Simon Hunt31642932016-01-22 20:34:00 -0800196 app.requiredApps().forEach(requiredApps::add);
Jian Lia54de5a2016-01-20 23:10:39 -0800197
198 data.set(REQUIRED_APPS, requiredApps);
199
200 // process features
201 ArrayNode features = arrayNode();
Simon Hunt31642932016-01-22 20:34:00 -0800202 app.features().forEach(features::add);
Jian Lia54de5a2016-01-20 23:10:39 -0800203
204 data.set(FEATURES, features);
205
Jian Lida253e02016-01-21 17:46:17 -0800206 // process permissions
207 ArrayNode permissions = arrayNode();
208 app.permissions().forEach(p -> permissions.add(p.getName()));
209
210 data.set(PERMISSIONS, permissions);
211
Jian Lia54de5a2016-01-20 23:10:39 -0800212 ObjectNode rootNode = objectNode();
213 rootNode.set(DETAILS, data);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800214 sendMessage(APP_DETAILS_RESP, rootNode);
Jian Lia54de5a2016-01-20 23:10:39 -0800215 }
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -0800216
Jian Lia54de5a2016-01-20 23:10:39 -0800217 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700218}