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 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 21 | import org.onosproject.app.ApplicationAdminService; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationService; |
| 23 | import org.onosproject.app.ApplicationState; |
| 24 | import org.onosproject.core.Application; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 25 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Arrays; |
| 28 | import java.util.List; |
| 29 | import java.util.stream.Collectors; |
| 30 | |
| 31 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 32 | |
| 33 | /** |
| 34 | * Message handler for application view related messages. |
| 35 | */ |
| 36 | public class ApplicationViewMessageHandler extends AbstractTabularViewMessageHandler { |
| 37 | |
| 38 | /** |
| 39 | * Creates a new message handler for the application messages. |
| 40 | */ |
| 41 | protected ApplicationViewMessageHandler() { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 42 | super(ImmutableSet.of("appDataRequest", "appManagementRequest")); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void process(ObjectNode message) { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 47 | String type = string(message, "event", "unknown"); |
| 48 | if (type.equals("appDataRequest")) { |
| 49 | sendAppList(message); |
| 50 | } else if (type.equals("appManagementRequest")) { |
| 51 | processManagementCommand(message); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private void sendAppList(ObjectNode message) { |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 56 | ObjectNode payload = payload(message); |
| 57 | String sortCol = string(payload, "sortCol", "id"); |
| 58 | String sortDir = string(payload, "sortDir", "asc"); |
| 59 | |
| 60 | ApplicationService service = get(ApplicationService.class); |
| 61 | TableRow[] rows = generateTableRows(service); |
| 62 | RowComparator rc = |
| 63 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
| 64 | Arrays.sort(rows, rc); |
| 65 | ArrayNode applications = generateArrayNode(rows); |
| 66 | ObjectNode rootNode = mapper.createObjectNode(); |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 67 | rootNode.set("apps", applications); |
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 | /** |
| 98 | * TableRow implementation for {@link org.onosproject.core.Application applications}. |
| 99 | */ |
| 100 | private static class ApplicationTableRow extends AbstractTableRow { |
| 101 | |
| 102 | private static final String STATE = "state"; |
| 103 | private static final String STATE_IID = "_iconid_state"; |
| 104 | private static final String ID = "id"; |
| 105 | private static final String VERSION = "version"; |
| 106 | private static final String ORIGIN = "origin"; |
| 107 | private static final String DESC = "desc"; |
| 108 | |
| 109 | private static final String[] COL_IDS = { |
| 110 | STATE, STATE_IID, ID, VERSION, ORIGIN, DESC |
| 111 | }; |
| 112 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 113 | private static final String ICON_ID_ACTIVE = "active"; |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 114 | private static final String ICON_ID_INACTIVE = "appInactive"; |
| 115 | |
| 116 | |
| 117 | public ApplicationTableRow(ApplicationService service, Application app) { |
| 118 | ApplicationState state = service.getState(app.id()); |
| 119 | String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE; |
| 120 | |
| 121 | add(STATE, state.toString()); |
| 122 | add(STATE_IID, iconId); |
| 123 | add(ID, app.id().name()); |
| 124 | add(VERSION, app.version().toString()); |
| 125 | add(ORIGIN, app.origin()); |
| 126 | add(DESC, app.description()); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected String[] columnIds() { |
| 131 | return COL_IDS; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } |