blob: 6a00f131f222b5c49f24c8a322c506c97d3a7f17 [file] [log] [blame]
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07001/*
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 */
16package org.onosproject.ui.impl;
17
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableSet;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070020import org.onosproject.app.ApplicationAdminService;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070021import org.onosproject.app.ApplicationService;
22import org.onosproject.app.ApplicationState;
23import org.onosproject.core.Application;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070024import org.onosproject.core.ApplicationId;
Simon Huntd2747a02015-04-30 22:41:16 -070025import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070026import org.onosproject.ui.UiMessageHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070027import org.onosproject.ui.table.AbstractTableRow;
Simon Huntabd16f62015-05-01 13:14:40 -070028import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070029import org.onosproject.ui.table.TableRow;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070030
Simon Huntd2747a02015-04-30 22:41:16 -070031import java.util.Collection;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070032import java.util.List;
33import java.util.stream.Collectors;
34
35import static org.onosproject.app.ApplicationState.ACTIVE;
36
37/**
38 * Message handler for application view related messages.
39 */
Simon Hunta0ddb022015-05-01 09:53:01 -070040public class ApplicationViewMessageHandler extends UiMessageHandler {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070041
Simon Huntd2747a02015-04-30 22:41:16 -070042 private static final String APP_DATA_REQ = "appDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070043 private static final String APP_DATA_RESP = "appDataResponse";
44 private static final String APPS = "apps";
45
Simon Huntd2747a02015-04-30 22:41:16 -070046 private static final String APP_MGMT_REQ = "appManagementRequest";
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070047
Simon Huntabd16f62015-05-01 13:14:40 -070048 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 Vachuska0fa583c2015-03-30 23:07:41 -070058 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070059 protected Collection<RequestHandler> getHandlers() {
60 return ImmutableSet.of(
61 new AppDataRequest(),
62 new AppMgmtRequest()
63 );
64 }
65
Simon Huntabd16f62015-05-01 13:14:40 -070066 // handler for application table requests
67 private final class AppDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070068 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070069 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070070 }
71
72 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070073 protected TableRow[] generateTableRows(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -070074 ApplicationService service = get(ApplicationService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070075 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 Vachuskaa7a0f562015-04-14 23:27:44 -070079 }
Simon Huntabd16f62015-05-01 13:14:40 -070080
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070081 }
82
Simon Huntabd16f62015-05-01 13:14:40 -070083 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -070084 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070085 private AppMgmtRequest() {
86 super(APP_MGMT_REQ);
87 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070088
Simon Huntd2747a02015-04-30 22:41:16 -070089 @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 Vachuskaa7a0f562015-04-14 23:27:44 -0700104 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700105 }
106 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700107
108 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -0700109 * TableRow implementation for
110 * {@link org.onosproject.core.Application applications}.
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700111 */
112 private static class ApplicationTableRow extends AbstractTableRow {
113
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700114 private static final String[] COL_IDS = {
115 STATE, STATE_IID, ID, VERSION, ORIGIN, DESC
116 };
117
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700118 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 Hunt44aa2f82015-04-30 15:01:35 -0700122 add(STATE, state);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700123 add(STATE_IID, iconId);
124 add(ID, app.id().name());
Simon Hunt44aa2f82015-04-30 15:01:35 -0700125 add(VERSION, app.version());
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700126 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}