blob: 51e538b612aa4ece289c0bd5e0100e34e8c2e7c0 [file] [log] [blame]
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07001/*
2 * Copyright 2015 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.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
33import static org.onosproject.app.ApplicationState.ACTIVE;
34
35/**
36 * Message handler for application view related messages.
37 */
Simon Hunta0ddb022015-05-01 09:53:01 -070038public class ApplicationViewMessageHandler extends UiMessageHandler {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070039
Simon Huntd2747a02015-04-30 22:41:16 -070040 private static final String APP_DATA_REQ = "appDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070041 private static final String APP_DATA_RESP = "appDataResponse";
42 private static final String APPS = "apps";
43
Simon Huntd2747a02015-04-30 22:41:16 -070044 private static final String APP_MGMT_REQ = "appManagementRequest";
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070045
Jian Lia54de5a2016-01-20 23:10:39 -080046 private static final String APP_DETAILS_REQ = "appDetailsRequest";
47 private static final String APP_DETAILS_RESP = "appDetailsResponse";
48 private static final String DETAILS = "details";
49
Simon Huntabd16f62015-05-01 13:14:40 -070050 private static final String STATE = "state";
51 private static final String STATE_IID = "_iconid_state";
52 private static final String ID = "id";
Jian Li97d6b2d2016-01-20 10:13:43 -080053 private static final String ICON = "icon";
Simon Huntabd16f62015-05-01 13:14:40 -070054 private static final String VERSION = "version";
Jian Li97d6b2d2016-01-20 10:13:43 -080055 private static final String CATEGORY = "category";
Simon Huntabd16f62015-05-01 13:14:40 -070056 private static final String ORIGIN = "origin";
Simon Huntafae2f72016-03-04 21:18:23 -080057 private static final String TITLE = "title";
Simon Huntabd16f62015-05-01 13:14:40 -070058 private static final String DESC = "desc";
Jian Li97d6b2d2016-01-20 10:13:43 -080059 private static final String URL = "url";
Jian Lia54de5a2016-01-20 23:10:39 -080060 private static final String README = "readme";
61 private static final String ROLE = "role";
Simon Hunt31642932016-01-22 20:34:00 -080062 private static final String REQUIRED_APPS = "required_apps";
Jian Lia54de5a2016-01-20 23:10:39 -080063 private static final String FEATURES = "features";
Jian Lida253e02016-01-21 17:46:17 -080064 private static final String PERMISSIONS = "permissions";
Simon Huntabd16f62015-05-01 13:14:40 -070065
66 private static final String ICON_ID_ACTIVE = "active";
67 private static final String ICON_ID_INACTIVE = "appInactive";
68
Simon Hunt3d1b0652015-05-05 17:27:24 -070069 private static final String[] COL_IDS = {
Simon Huntafae2f72016-03-04 21:18:23 -080070 STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, TITLE, DESC,
Jian Lida253e02016-01-21 17:46:17 -080071 URL, README, ROLE, REQUIRED_APPS, FEATURES, PERMISSIONS
Simon Hunt3d1b0652015-05-05 17:27:24 -070072 };
73
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070074 @Override
Simon Huntda580882015-05-12 20:58:18 -070075 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070076 return ImmutableSet.of(
77 new AppDataRequest(),
Jian Lia54de5a2016-01-20 23:10:39 -080078 new AppMgmtRequest(),
79 new DetailRequestHandler()
Simon Huntd2747a02015-04-30 22:41:16 -070080 );
81 }
82
Simon Huntabd16f62015-05-01 13:14:40 -070083 // handler for application table requests
84 private final class AppDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080085 private static final String NO_ROWS_MESSAGE = "No applications found";
86
Simon Huntd2747a02015-04-30 22:41:16 -070087 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070088 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070089 }
90
91 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070092 protected String[] getColumnIds() {
93 return COL_IDS;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070094 }
Simon Huntabd16f62015-05-01 13:14:40 -070095
Simon Hunt3d1b0652015-05-05 17:27:24 -070096 @Override
Jian Li8baf4472016-01-15 15:08:09 -080097 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080098 return NO_ROWS_MESSAGE;
99 }
100
101 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700102 protected void populateTable(TableModel tm, ObjectNode payload) {
103 ApplicationService as = get(ApplicationService.class);
104 for (Application app : as.getApplications()) {
105 populateRow(tm.addRow(), app, as);
106 }
107 }
108
109 private void populateRow(TableModel.Row row, Application app,
110 ApplicationService as) {
111 ApplicationId id = app.id();
112 ApplicationState state = as.getState(id);
113 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
114
115 row.cell(STATE, state)
116 .cell(STATE_IID, iconId)
117 .cell(ID, id.name())
Jian Li97d6b2d2016-01-20 10:13:43 -0800118 .cell(ICON, id.name())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700119 .cell(VERSION, app.version())
Jian Li97d6b2d2016-01-20 10:13:43 -0800120 .cell(CATEGORY, app.category())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700121 .cell(ORIGIN, app.origin())
Simon Huntafae2f72016-03-04 21:18:23 -0800122 .cell(TITLE, app.title())
Jian Li97d6b2d2016-01-20 10:13:43 -0800123 .cell(DESC, app.description())
124 .cell(URL, app.url());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700126 }
127
Simon Huntabd16f62015-05-01 13:14:40 -0700128 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -0700129 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700130 private AppMgmtRequest() {
131 super(APP_MGMT_REQ);
132 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700133
Simon Huntd2747a02015-04-30 22:41:16 -0700134 @Override
135 public void process(long sid, ObjectNode payload) {
136 String action = string(payload, "action");
137 String name = string(payload, "name");
138 if (action != null && name != null) {
139 ApplicationAdminService service = get(ApplicationAdminService.class);
140 ApplicationId appId = service.getId(name);
141 if (action.equals("activate")) {
142 service.activate(appId);
143 } else if (action.equals("deactivate")) {
144 service.deactivate(appId);
145 } else if (action.equals("uninstall")) {
146 service.uninstall(appId);
147 }
148 chain(APP_DATA_REQ, sid, payload);
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700149 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700150 }
151 }
Jian Lia54de5a2016-01-20 23:10:39 -0800152
153 // handler for selected application detail requests
154 private final class DetailRequestHandler extends RequestHandler {
155 private DetailRequestHandler() {
156 super(APP_DETAILS_REQ);
157 }
158
159 @Override
160 public void process(long sid, ObjectNode payload) {
161 String id = string(payload, ID);
162 ApplicationService as = get(ApplicationService.class);
163 ApplicationId appId = as.getId(id);
164 ApplicationState state = as.getState(appId);
165 Application app = as.getApplication(appId);
166 ObjectNode data = objectNode();
167
168 data.put(STATE, state.toString());
169 data.put(ID, appId.name());
170 data.put(VERSION, app.version().toString());
171 data.put(ROLE, app.role().toString());
172 data.put(CATEGORY, app.category());
Simon Huntafae2f72016-03-04 21:18:23 -0800173 data.put(TITLE, app.title());
Jian Lia54de5a2016-01-20 23:10:39 -0800174 data.put(ORIGIN, app.origin());
175 data.put(README, app.readme());
Jian Lida253e02016-01-21 17:46:17 -0800176 data.put(DESC, app.description());
Jian Lia54de5a2016-01-20 23:10:39 -0800177 data.put(URL, app.url());
178
179 // process required applications
180 ArrayNode requiredApps = arrayNode();
Simon Hunt31642932016-01-22 20:34:00 -0800181 app.requiredApps().forEach(requiredApps::add);
Jian Lia54de5a2016-01-20 23:10:39 -0800182
183 data.set(REQUIRED_APPS, requiredApps);
184
185 // process features
186 ArrayNode features = arrayNode();
Simon Hunt31642932016-01-22 20:34:00 -0800187 app.features().forEach(features::add);
Jian Lia54de5a2016-01-20 23:10:39 -0800188
189 data.set(FEATURES, features);
190
Jian Lida253e02016-01-21 17:46:17 -0800191 // process permissions
192 ArrayNode permissions = arrayNode();
193 app.permissions().forEach(p -> permissions.add(p.getName()));
194
195 data.set(PERMISSIONS, permissions);
196
Jian Lia54de5a2016-01-20 23:10:39 -0800197 ObjectNode rootNode = objectNode();
198 rootNode.set(DETAILS, data);
199 sendMessage(APP_DETAILS_RESP, 0, rootNode);
200 }
201 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700202}