Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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 | |
| 33 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 34 | |
| 35 | /** |
| 36 | * Message handler for application view related messages. |
| 37 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 38 | public class ApplicationViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 39 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 40 | private static final String APP_DATA_REQ = "appDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 41 | private static final String APP_DATA_RESP = "appDataResponse"; |
| 42 | private static final String APPS = "apps"; |
| 43 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 44 | private static final String APP_MGMT_REQ = "appManagementRequest"; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 45 | |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 46 | 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 Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 50 | private static final String STATE = "state"; |
| 51 | private static final String STATE_IID = "_iconid_state"; |
| 52 | private static final String ID = "id"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 53 | private static final String ICON = "icon"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 54 | private static final String VERSION = "version"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 55 | private static final String CATEGORY = "category"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 56 | private static final String ORIGIN = "origin"; |
| 57 | private static final String DESC = "desc"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 58 | private static final String URL = "url"; |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 59 | private static final String README = "readme"; |
| 60 | private static final String ROLE = "role"; |
Simon Hunt | 3164293 | 2016-01-22 20:34:00 -0800 | [diff] [blame] | 61 | private static final String REQUIRED_APPS = "required_apps"; |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 62 | private static final String FEATURES = "features"; |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 63 | private static final String PERMISSIONS = "permissions"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 64 | |
| 65 | private static final String ICON_ID_ACTIVE = "active"; |
| 66 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 67 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 68 | private static final String[] COL_IDS = { |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 69 | STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, DESC, |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 70 | URL, README, ROLE, REQUIRED_APPS, FEATURES, PERMISSIONS |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 73 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 74 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 75 | return ImmutableSet.of( |
| 76 | new AppDataRequest(), |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 77 | new AppMgmtRequest(), |
| 78 | new DetailRequestHandler() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 79 | ); |
| 80 | } |
| 81 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 82 | // handler for application table requests |
| 83 | private final class AppDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 84 | private static final String NO_ROWS_MESSAGE = "No applications found"; |
| 85 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 86 | private AppDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 87 | super(APP_DATA_REQ, APP_DATA_RESP, APPS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 91 | protected String[] getColumnIds() { |
| 92 | return COL_IDS; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 93 | } |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 94 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 95 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 96 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 97 | return NO_ROWS_MESSAGE; |
| 98 | } |
| 99 | |
| 100 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 101 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 102 | ApplicationService as = get(ApplicationService.class); |
| 103 | for (Application app : as.getApplications()) { |
| 104 | populateRow(tm.addRow(), app, as); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private void populateRow(TableModel.Row row, Application app, |
| 109 | ApplicationService as) { |
| 110 | ApplicationId id = app.id(); |
| 111 | ApplicationState state = as.getState(id); |
| 112 | String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE; |
| 113 | |
| 114 | row.cell(STATE, state) |
| 115 | .cell(STATE_IID, iconId) |
| 116 | .cell(ID, id.name()) |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 117 | .cell(ICON, id.name()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 118 | .cell(VERSION, app.version()) |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 119 | .cell(CATEGORY, app.category()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 120 | .cell(ORIGIN, app.origin()) |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 121 | .cell(DESC, app.description()) |
| 122 | .cell(URL, app.url()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 123 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 126 | // handler for application management control button actions |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 127 | private final class AppMgmtRequest extends RequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 128 | private AppMgmtRequest() { |
| 129 | super(APP_MGMT_REQ); |
| 130 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 131 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public void process(long sid, ObjectNode payload) { |
| 134 | String action = string(payload, "action"); |
| 135 | String name = string(payload, "name"); |
| 136 | if (action != null && name != null) { |
| 137 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 138 | ApplicationId appId = service.getId(name); |
| 139 | if (action.equals("activate")) { |
| 140 | service.activate(appId); |
| 141 | } else if (action.equals("deactivate")) { |
| 142 | service.deactivate(appId); |
| 143 | } else if (action.equals("uninstall")) { |
| 144 | service.uninstall(appId); |
| 145 | } |
| 146 | chain(APP_DATA_REQ, sid, payload); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 147 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 150 | |
| 151 | // handler for selected application detail requests |
| 152 | private final class DetailRequestHandler extends RequestHandler { |
| 153 | private DetailRequestHandler() { |
| 154 | super(APP_DETAILS_REQ); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void process(long sid, ObjectNode payload) { |
| 159 | String id = string(payload, ID); |
| 160 | ApplicationService as = get(ApplicationService.class); |
| 161 | ApplicationId appId = as.getId(id); |
| 162 | ApplicationState state = as.getState(appId); |
| 163 | Application app = as.getApplication(appId); |
| 164 | ObjectNode data = objectNode(); |
| 165 | |
| 166 | data.put(STATE, state.toString()); |
| 167 | data.put(ID, appId.name()); |
| 168 | data.put(VERSION, app.version().toString()); |
| 169 | data.put(ROLE, app.role().toString()); |
| 170 | data.put(CATEGORY, app.category()); |
| 171 | data.put(ORIGIN, app.origin()); |
| 172 | data.put(README, app.readme()); |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 173 | data.put(DESC, app.description()); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 174 | data.put(URL, app.url()); |
| 175 | |
| 176 | // process required applications |
| 177 | ArrayNode requiredApps = arrayNode(); |
Simon Hunt | 3164293 | 2016-01-22 20:34:00 -0800 | [diff] [blame] | 178 | app.requiredApps().forEach(requiredApps::add); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 179 | |
| 180 | data.set(REQUIRED_APPS, requiredApps); |
| 181 | |
| 182 | // process features |
| 183 | ArrayNode features = arrayNode(); |
Simon Hunt | 3164293 | 2016-01-22 20:34:00 -0800 | [diff] [blame] | 184 | app.features().forEach(features::add); |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 185 | |
| 186 | data.set(FEATURES, features); |
| 187 | |
Jian Li | da253e0 | 2016-01-21 17:46:17 -0800 | [diff] [blame] | 188 | // process permissions |
| 189 | ArrayNode permissions = arrayNode(); |
| 190 | app.permissions().forEach(p -> permissions.add(p.getName())); |
| 191 | |
| 192 | data.set(PERMISSIONS, permissions); |
| 193 | |
Jian Li | a54de5a | 2016-01-20 23:10:39 -0800 | [diff] [blame] | 194 | ObjectNode rootNode = objectNode(); |
| 195 | rootNode.set(DETAILS, data); |
| 196 | sendMessage(APP_DETAILS_RESP, 0, rootNode); |
| 197 | } |
| 198 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 199 | } |