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"; |
| 48 | private static final String VERSION = "version"; |
| 49 | private static final String ORIGIN = "origin"; |
| 50 | private static final String DESC = "desc"; |
| 51 | |
| 52 | private static final String ICON_ID_ACTIVE = "active"; |
| 53 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 54 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 55 | private static final String[] COL_IDS = { |
| 56 | STATE, STATE_IID, ID, VERSION, ORIGIN, DESC |
| 57 | }; |
| 58 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 59 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 60 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 61 | return ImmutableSet.of( |
| 62 | new AppDataRequest(), |
| 63 | new AppMgmtRequest() |
| 64 | ); |
| 65 | } |
| 66 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 67 | // handler for application table requests |
| 68 | private final class AppDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 69 | private AppDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 70 | super(APP_DATA_REQ, APP_DATA_RESP, APPS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 74 | protected String[] getColumnIds() { |
| 75 | return COL_IDS; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 76 | } |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 77 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 78 | @Override |
| 79 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 80 | ApplicationService as = get(ApplicationService.class); |
| 81 | for (Application app : as.getApplications()) { |
| 82 | populateRow(tm.addRow(), app, as); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private void populateRow(TableModel.Row row, Application app, |
| 87 | ApplicationService as) { |
| 88 | ApplicationId id = app.id(); |
| 89 | ApplicationState state = as.getState(id); |
| 90 | String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE; |
| 91 | |
| 92 | row.cell(STATE, state) |
| 93 | .cell(STATE_IID, iconId) |
| 94 | .cell(ID, id.name()) |
| 95 | .cell(VERSION, app.version()) |
| 96 | .cell(ORIGIN, app.origin()) |
| 97 | .cell(DESC, app.description()); |
| 98 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 101 | // handler for application management control button actions |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 102 | private final class AppMgmtRequest extends RequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 103 | private AppMgmtRequest() { |
| 104 | super(APP_MGMT_REQ); |
| 105 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 106 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public void process(long sid, ObjectNode payload) { |
| 109 | String action = string(payload, "action"); |
| 110 | String name = string(payload, "name"); |
| 111 | if (action != null && name != null) { |
| 112 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 113 | ApplicationId appId = service.getId(name); |
| 114 | if (action.equals("activate")) { |
| 115 | service.activate(appId); |
| 116 | } else if (action.equals("deactivate")) { |
| 117 | service.deactivate(appId); |
| 118 | } else if (action.equals("uninstall")) { |
| 119 | service.uninstall(appId); |
| 120 | } |
| 121 | chain(APP_DATA_REQ, sid, payload); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 122 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 125 | } |