blob: 80c1e757a0eba9ce2512523a0add364cda934686 [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 Hunt3d1b0652015-05-05 17:27:24 -070027import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070028import org.onosproject.ui.table.TableRequestHandler;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070029
Simon Huntd2747a02015-04-30 22:41:16 -070030import java.util.Collection;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070031
32import static org.onosproject.app.ApplicationState.ACTIVE;
33
34/**
35 * Message handler for application view related messages.
36 */
Simon Hunta0ddb022015-05-01 09:53:01 -070037public class ApplicationViewMessageHandler extends UiMessageHandler {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070038
Simon Huntd2747a02015-04-30 22:41:16 -070039 private static final String APP_DATA_REQ = "appDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070040 private static final String APP_DATA_RESP = "appDataResponse";
41 private static final String APPS = "apps";
42
Simon Huntd2747a02015-04-30 22:41:16 -070043 private static final String APP_MGMT_REQ = "appManagementRequest";
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070044
Simon Huntabd16f62015-05-01 13:14:40 -070045 private static final String STATE = "state";
46 private static final String STATE_IID = "_iconid_state";
47 private static final String ID = "id";
Jian Li97d6b2d2016-01-20 10:13:43 -080048 private static final String ICON = "icon";
Simon Huntabd16f62015-05-01 13:14:40 -070049 private static final String VERSION = "version";
Jian Li97d6b2d2016-01-20 10:13:43 -080050 private static final String CATEGORY = "category";
Simon Huntabd16f62015-05-01 13:14:40 -070051 private static final String ORIGIN = "origin";
52 private static final String DESC = "desc";
Jian Li97d6b2d2016-01-20 10:13:43 -080053 private static final String URL = "url";
Simon Huntabd16f62015-05-01 13:14:40 -070054
55 private static final String ICON_ID_ACTIVE = "active";
56 private static final String ICON_ID_INACTIVE = "appInactive";
57
Simon Hunt3d1b0652015-05-05 17:27:24 -070058 private static final String[] COL_IDS = {
Jian Li97d6b2d2016-01-20 10:13:43 -080059 STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, DESC, URL
Simon Hunt3d1b0652015-05-05 17:27:24 -070060 };
61
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070062 @Override
Simon Huntda580882015-05-12 20:58:18 -070063 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070064 return ImmutableSet.of(
65 new AppDataRequest(),
66 new AppMgmtRequest()
67 );
68 }
69
Simon Huntabd16f62015-05-01 13:14:40 -070070 // handler for application table requests
71 private final class AppDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080072 private static final String NO_ROWS_MESSAGE = "No applications found";
73
Simon Huntd2747a02015-04-30 22:41:16 -070074 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070075 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070076 }
77
78 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070079 protected String[] getColumnIds() {
80 return COL_IDS;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070081 }
Simon Huntabd16f62015-05-01 13:14:40 -070082
Simon Hunt3d1b0652015-05-05 17:27:24 -070083 @Override
Jian Li8baf4472016-01-15 15:08:09 -080084 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080085 return NO_ROWS_MESSAGE;
86 }
87
88 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 protected void populateTable(TableModel tm, ObjectNode payload) {
90 ApplicationService as = get(ApplicationService.class);
91 for (Application app : as.getApplications()) {
92 populateRow(tm.addRow(), app, as);
93 }
94 }
95
96 private void populateRow(TableModel.Row row, Application app,
97 ApplicationService as) {
98 ApplicationId id = app.id();
99 ApplicationState state = as.getState(id);
100 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
101
102 row.cell(STATE, state)
103 .cell(STATE_IID, iconId)
104 .cell(ID, id.name())
Jian Li97d6b2d2016-01-20 10:13:43 -0800105 .cell(ICON, id.name())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700106 .cell(VERSION, app.version())
Jian Li97d6b2d2016-01-20 10:13:43 -0800107 .cell(CATEGORY, app.category())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700108 .cell(ORIGIN, app.origin())
Jian Li97d6b2d2016-01-20 10:13:43 -0800109 .cell(DESC, app.description())
110 .cell(URL, app.url());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700111 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700112 }
113
Simon Huntabd16f62015-05-01 13:14:40 -0700114 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -0700115 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700116 private AppMgmtRequest() {
117 super(APP_MGMT_REQ);
118 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700119
Simon Huntd2747a02015-04-30 22:41:16 -0700120 @Override
121 public void process(long sid, ObjectNode payload) {
122 String action = string(payload, "action");
123 String name = string(payload, "name");
124 if (action != null && name != null) {
125 ApplicationAdminService service = get(ApplicationAdminService.class);
126 ApplicationId appId = service.getId(name);
127 if (action.equals("activate")) {
128 service.activate(appId);
129 } else if (action.equals("deactivate")) {
130 service.deactivate(appId);
131 } else if (action.equals("uninstall")) {
132 service.uninstall(appId);
133 }
134 chain(APP_DATA_REQ, sid, payload);
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700135 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700136 }
137 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700138}