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 | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.table.AbstractTableRow; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.TableRow; |
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 | import java.util.List; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 36 | |
| 37 | /** |
| 38 | * Message handler for application view related messages. |
| 39 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 40 | public class ApplicationViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 41 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 42 | private static final String APP_DATA_REQ = "appDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 43 | private static final String APP_DATA_RESP = "appDataResponse"; |
| 44 | private static final String APPS = "apps"; |
| 45 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 46 | private static final String APP_MGMT_REQ = "appManagementRequest"; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 47 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 48 | private static final String STATE = "state"; |
| 49 | private static final String STATE_IID = "_iconid_state"; |
| 50 | private static final String ID = "id"; |
| 51 | private static final String VERSION = "version"; |
| 52 | private static final String ORIGIN = "origin"; |
| 53 | private static final String DESC = "desc"; |
| 54 | |
| 55 | private static final String ICON_ID_ACTIVE = "active"; |
| 56 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 57 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 58 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 59 | protected Collection<RequestHandler> getHandlers() { |
| 60 | return ImmutableSet.of( |
| 61 | new AppDataRequest(), |
| 62 | new AppMgmtRequest() |
| 63 | ); |
| 64 | } |
| 65 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 66 | // handler for application table requests |
| 67 | private final class AppDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 68 | private AppDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 69 | super(APP_DATA_REQ, APP_DATA_RESP, APPS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 73 | protected TableRow[] generateTableRows(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | ApplicationService service = get(ApplicationService.class); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 75 | List<TableRow> list = service.getApplications().stream() |
| 76 | .map(application -> new ApplicationTableRow(service, application)) |
| 77 | .collect(Collectors.toList()); |
| 78 | return list.toArray(new TableRow[list.size()]); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 79 | } |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 80 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 83 | // handler for application management control button actions |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 84 | private final class AppMgmtRequest extends RequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 85 | private AppMgmtRequest() { |
| 86 | super(APP_MGMT_REQ); |
| 87 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 88 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void process(long sid, ObjectNode payload) { |
| 91 | String action = string(payload, "action"); |
| 92 | String name = string(payload, "name"); |
| 93 | if (action != null && name != null) { |
| 94 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 95 | ApplicationId appId = service.getId(name); |
| 96 | if (action.equals("activate")) { |
| 97 | service.activate(appId); |
| 98 | } else if (action.equals("deactivate")) { |
| 99 | service.deactivate(appId); |
| 100 | } else if (action.equals("uninstall")) { |
| 101 | service.uninstall(appId); |
| 102 | } |
| 103 | chain(APP_DATA_REQ, sid, payload); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 104 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 109 | * TableRow implementation for |
| 110 | * {@link org.onosproject.core.Application applications}. |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 111 | */ |
| 112 | private static class ApplicationTableRow extends AbstractTableRow { |
| 113 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 114 | private static final String[] COL_IDS = { |
| 115 | STATE, STATE_IID, ID, VERSION, ORIGIN, DESC |
| 116 | }; |
| 117 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 118 | public ApplicationTableRow(ApplicationService service, Application app) { |
| 119 | ApplicationState state = service.getState(app.id()); |
| 120 | String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE; |
| 121 | |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 122 | add(STATE, state); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 123 | add(STATE_IID, iconId); |
| 124 | add(ID, app.id().name()); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 125 | add(VERSION, app.version()); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 126 | add(ORIGIN, app.origin()); |
| 127 | add(DESC, app.description()); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | protected String[] columnIds() { |
| 132 | return COL_IDS; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } |