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 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 20 | import org.onosproject.app.ApplicationAdminService; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 21 | import org.onosproject.app.ApplicationService; |
| 22 | import org.onosproject.app.ApplicationState; |
| 23 | import org.onosproject.core.Application; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRequestHandler; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 29 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 30 | import java.util.Collection; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 31 | |
| 32 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 33 | |
| 34 | /** |
| 35 | * Message handler for application view related messages. |
| 36 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 37 | public class ApplicationViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 38 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 39 | private static final String APP_DATA_REQ = "appDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 40 | private static final String APP_DATA_RESP = "appDataResponse"; |
| 41 | private static final String APPS = "apps"; |
| 42 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 43 | private static final String APP_MGMT_REQ = "appManagementRequest"; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 44 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 45 | private static final String STATE = "state"; |
| 46 | private static final String STATE_IID = "_iconid_state"; |
| 47 | private static final String ID = "id"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 48 | private static final String ICON = "icon"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 49 | private static final String VERSION = "version"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 50 | private static final String CATEGORY = "category"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 51 | private static final String ORIGIN = "origin"; |
| 52 | private static final String DESC = "desc"; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 53 | private static final String URL = "url"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 54 | |
| 55 | private static final String ICON_ID_ACTIVE = "active"; |
| 56 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 57 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 58 | private static final String[] COL_IDS = { |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 59 | STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, DESC, URL |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 62 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 63 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 64 | return ImmutableSet.of( |
| 65 | new AppDataRequest(), |
| 66 | new AppMgmtRequest() |
| 67 | ); |
| 68 | } |
| 69 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 70 | // handler for application table requests |
| 71 | private final class AppDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 72 | private static final String NO_ROWS_MESSAGE = "No applications found"; |
| 73 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | private AppDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 75 | super(APP_DATA_REQ, APP_DATA_RESP, APPS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 79 | protected String[] getColumnIds() { |
| 80 | return COL_IDS; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 81 | } |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 82 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 83 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 84 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 85 | return NO_ROWS_MESSAGE; |
| 86 | } |
| 87 | |
| 88 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 89 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 90 | ApplicationService as = get(ApplicationService.class); |
| 91 | for (Application app : as.getApplications()) { |
| 92 | populateRow(tm.addRow(), app, as); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private void populateRow(TableModel.Row row, Application app, |
| 97 | ApplicationService as) { |
| 98 | ApplicationId id = app.id(); |
| 99 | ApplicationState state = as.getState(id); |
| 100 | String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE; |
| 101 | |
| 102 | row.cell(STATE, state) |
| 103 | .cell(STATE_IID, iconId) |
| 104 | .cell(ID, id.name()) |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 105 | .cell(ICON, id.name()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 106 | .cell(VERSION, app.version()) |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 107 | .cell(CATEGORY, app.category()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 108 | .cell(ORIGIN, app.origin()) |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 109 | .cell(DESC, app.description()) |
| 110 | .cell(URL, app.url()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 111 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 114 | // handler for application management control button actions |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 115 | private final class AppMgmtRequest extends RequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 116 | private AppMgmtRequest() { |
| 117 | super(APP_MGMT_REQ); |
| 118 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 119 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public void process(long sid, ObjectNode payload) { |
| 122 | String action = string(payload, "action"); |
| 123 | String name = string(payload, "name"); |
| 124 | if (action != null && name != null) { |
| 125 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 126 | ApplicationId appId = service.getId(name); |
| 127 | if (action.equals("activate")) { |
| 128 | service.activate(appId); |
| 129 | } else if (action.equals("deactivate")) { |
| 130 | service.deactivate(appId); |
| 131 | } else if (action.equals("uninstall")) { |
| 132 | service.uninstall(appId); |
| 133 | } |
| 134 | chain(APP_DATA_REQ, sid, payload); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 135 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 138 | } |