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 | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.UiMessageHandler; |
| 26 | import org.onosproject.ui.table.AbstractTableRow; |
| 27 | import org.onosproject.ui.table.RowComparator; |
| 28 | import org.onosproject.ui.table.TableRow; |
| 29 | import org.onosproject.ui.table.TableUtils; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.Arrays; |
| 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 | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 40 | public class ApplicationViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Creates a new message handler for the application messages. |
| 44 | */ |
| 45 | protected ApplicationViewMessageHandler() { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 46 | super(ImmutableSet.of("appDataRequest", "appManagementRequest")); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void process(ObjectNode message) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 51 | String type = eventType(message); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 52 | if (type.equals("appDataRequest")) { |
| 53 | sendAppList(message); |
| 54 | } else if (type.equals("appManagementRequest")) { |
| 55 | processManagementCommand(message); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void sendAppList(ObjectNode message) { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 60 | ObjectNode payload = payload(message); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 61 | RowComparator rc = TableUtils.createRowComparator(payload); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 62 | |
| 63 | ApplicationService service = get(ApplicationService.class); |
| 64 | TableRow[] rows = generateTableRows(service); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 65 | Arrays.sort(rows, rc); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 66 | ObjectNode rootNode = mapper.createObjectNode(); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 67 | rootNode.set("apps", TableUtils.generateArrayNode(rows)); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 68 | |
| 69 | connection().sendMessage("appDataResponse", 0, rootNode); |
| 70 | } |
| 71 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 72 | private void processManagementCommand(ObjectNode message) { |
| 73 | ObjectNode payload = payload(message); |
| 74 | String action = string(payload, "action"); |
| 75 | String name = string(payload, "name"); |
| 76 | if (action != null && name != null) { |
| 77 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 78 | ApplicationId appId = service.getId(name); |
| 79 | if (action.equals("activate")) { |
| 80 | service.activate(appId); |
| 81 | } else if (action.equals("deactivate")) { |
| 82 | service.deactivate(appId); |
| 83 | } else if (action.equals("uninstall")) { |
| 84 | service.uninstall(appId); |
| 85 | } |
| 86 | sendAppList(message); |
| 87 | } |
| 88 | } |
| 89 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 90 | private TableRow[] generateTableRows(ApplicationService service) { |
| 91 | List<TableRow> list = service.getApplications().stream() |
| 92 | .map(application -> new ApplicationTableRow(service, application)) |
| 93 | .collect(Collectors.toList()); |
| 94 | return list.toArray(new TableRow[list.size()]); |
| 95 | } |
| 96 | |
| 97 | /** |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 98 | * TableRow implementation for |
| 99 | * {@link org.onosproject.core.Application applications}. |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 100 | */ |
| 101 | private static class ApplicationTableRow extends AbstractTableRow { |
| 102 | |
| 103 | private static final String STATE = "state"; |
| 104 | private static final String STATE_IID = "_iconid_state"; |
| 105 | private static final String ID = "id"; |
| 106 | private static final String VERSION = "version"; |
| 107 | private static final String ORIGIN = "origin"; |
| 108 | private static final String DESC = "desc"; |
| 109 | |
| 110 | private static final String[] COL_IDS = { |
| 111 | STATE, STATE_IID, ID, VERSION, ORIGIN, DESC |
| 112 | }; |
| 113 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 114 | private static final String ICON_ID_ACTIVE = "active"; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 115 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 116 | |
| 117 | |
| 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 | } |