Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 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.ui.impl; |
| 17 | |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 21 | import org.onosproject.app.ApplicationAdminService; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationService; |
| 23 | import org.onosproject.app.ApplicationState; |
| 24 | import org.onosproject.core.Application; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.TableRequestHandler; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 30 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 32 | |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 33 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 34 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 35 | |
| 36 | /** |
| 37 | * Message handler for application view related messages. |
| 38 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 39 | public class ApplicationViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 40 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 41 | private static final String APP_DATA_REQ = "appDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 42 | private static final String APP_DATA_RESP = "appDataResponse"; |
| 43 | private static final String APPS = "apps"; |
| 44 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 45 | private static final String APP_MGMT_REQ = "appManagementRequest"; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 46 | |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 47 | 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 Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 51 | private static final String STATE = "state"; |
| 52 | private static final String STATE_IID = "_iconid_state"; |
| 53 | private static final String ID = "id"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 54 | private static final String ICON = "icon"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 55 | private static final String VERSION = "version"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 56 | private static final String CATEGORY = "category"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 57 | private static final String ORIGIN = "origin"; |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 58 | private static final String TITLE = "title"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 59 | private static final String DESC = "desc"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 60 | private static final String URL = "url"; |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 61 | private static final String README = "readme"; |
| 62 | private static final String ROLE = "role"; |
Simon Hunt | 3164293 | 2016-01-22 20:34:00 -0800 | [diff] [blame] | 63 | private static final String REQUIRED_APPS = "required_apps"; |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 64 | private static final String FEATURES = "features"; |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 65 | private static final String PERMISSIONS = "permissions"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 66 | |
| 67 | private static final String ICON_ID_ACTIVE = "active"; |
| 68 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 69 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 70 | private static final String[] COL_IDS = { |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 71 | STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, TITLE, DESC, |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 72 | URL, README, ROLE, REQUIRED_APPS, FEATURES, PERMISSIONS |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 75 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 76 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 77 | return ImmutableSet.of( |
| 78 | new AppDataRequest(), |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 79 | new AppMgmtRequest(), |
| 80 | new DetailRequestHandler() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 81 | ); |
| 82 | } |
| 83 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 84 | // handler for application table requests |
| 85 | private final class AppDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 86 | private static final String NO_ROWS_MESSAGE = "No applications found"; |
| 87 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 88 | private AppDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 89 | super(APP_DATA_REQ, APP_DATA_RESP, APPS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 93 | protected String[] getColumnIds() { |
| 94 | return COL_IDS; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 95 | } |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 96 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 97 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 98 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 99 | return NO_ROWS_MESSAGE; |
| 100 | } |
| 101 | |
| 102 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 103 | 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 Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 117 | .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 Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 126 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 129 | // handler for application management control button actions |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 130 | private final class AppMgmtRequest extends RequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 131 | private AppMgmtRequest() { |
| 132 | super(APP_MGMT_REQ); |
| 133 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 134 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 135 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 136 | public void process(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 137 | String action = string(payload, "action"); |
| 138 | String name = string(payload, "name"); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 139 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 140 | if (action != null && name != null) { |
| 141 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 142 | ApplicationId appId = service.getId(name); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 143 | 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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 155 | } |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 156 | chain(APP_DATA_REQ, payload); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 157 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 160 | |
| 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 Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 168 | public void process(ObjectNode payload) { |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 169 | String id = string(payload, ID); |
| 170 | ApplicationService as = get(ApplicationService.class); |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 171 | |
| 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 Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 178 | 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 Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 188 | data.put(TITLE, app.title()); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 189 | data.put(ORIGIN, app.origin()); |
| 190 | data.put(README, app.readme()); |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 191 | data.put(DESC, app.description()); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 192 | data.put(URL, app.url()); |
| 193 | |
| 194 | // process required applications |
| 195 | ArrayNode requiredApps = arrayNode(); |
Simon Hunt | 3164293 | 2016-01-22 20:34:00 -0800 | [diff] [blame] | 196 | app.requiredApps().forEach(requiredApps::add); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 197 | |
| 198 | data.set(REQUIRED_APPS, requiredApps); |
| 199 | |
| 200 | // process features |
| 201 | ArrayNode features = arrayNode(); |
Simon Hunt | 3164293 | 2016-01-22 20:34:00 -0800 | [diff] [blame] | 202 | app.features().forEach(features::add); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 203 | |
| 204 | data.set(FEATURES, features); |
| 205 | |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 206 | // process permissions |
| 207 | ArrayNode permissions = arrayNode(); |
| 208 | app.permissions().forEach(p -> permissions.add(p.getName())); |
| 209 | |
| 210 | data.set(PERMISSIONS, permissions); |
| 211 | |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 212 | ObjectNode rootNode = objectNode(); |
| 213 | rootNode.set(DETAILS, data); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 214 | sendMessage(APP_DETAILS_RESP, rootNode); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 215 | } |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 216 | |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 217 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 218 | } |