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