blob: 2e1e7a0983ce0b56d0fd0167d8cb7d54de43ee55 [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";
48 private static final String VERSION = "version";
49 private static final String ORIGIN = "origin";
50 private static final String DESC = "desc";
51
52 private static final String ICON_ID_ACTIVE = "active";
53 private static final String ICON_ID_INACTIVE = "appInactive";
54
Simon Hunt3d1b0652015-05-05 17:27:24 -070055 private static final String[] COL_IDS = {
56 STATE, STATE_IID, ID, VERSION, ORIGIN, DESC
57 };
58
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070059 @Override
Simon Huntda580882015-05-12 20:58:18 -070060 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070061 return ImmutableSet.of(
62 new AppDataRequest(),
63 new AppMgmtRequest()
64 );
65 }
66
Simon Huntabd16f62015-05-01 13:14:40 -070067 // handler for application table requests
68 private final class AppDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080069 private static final String NO_ROWS_MESSAGE = "No applications found";
70
Simon Huntd2747a02015-04-30 22:41:16 -070071 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070072 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070073 }
74
75 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070076 protected String[] getColumnIds() {
77 return COL_IDS;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070078 }
Simon Huntabd16f62015-05-01 13:14:40 -070079
Simon Hunt3d1b0652015-05-05 17:27:24 -070080 @Override
Jian Li69f66632016-01-15 12:27:42 -080081 protected String noRowsMessage() {
82 return NO_ROWS_MESSAGE;
83 }
84
85 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070086 protected void populateTable(TableModel tm, ObjectNode payload) {
87 ApplicationService as = get(ApplicationService.class);
88 for (Application app : as.getApplications()) {
89 populateRow(tm.addRow(), app, as);
90 }
91 }
92
93 private void populateRow(TableModel.Row row, Application app,
94 ApplicationService as) {
95 ApplicationId id = app.id();
96 ApplicationState state = as.getState(id);
97 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
98
99 row.cell(STATE, state)
100 .cell(STATE_IID, iconId)
101 .cell(ID, id.name())
102 .cell(VERSION, app.version())
103 .cell(ORIGIN, app.origin())
104 .cell(DESC, app.description());
105 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700106 }
107
Simon Huntabd16f62015-05-01 13:14:40 -0700108 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -0700109 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700110 private AppMgmtRequest() {
111 super(APP_MGMT_REQ);
112 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700113
Simon Huntd2747a02015-04-30 22:41:16 -0700114 @Override
115 public void process(long sid, ObjectNode payload) {
116 String action = string(payload, "action");
117 String name = string(payload, "name");
118 if (action != null && name != null) {
119 ApplicationAdminService service = get(ApplicationAdminService.class);
120 ApplicationId appId = service.getId(name);
121 if (action.equals("activate")) {
122 service.activate(appId);
123 } else if (action.equals("deactivate")) {
124 service.deactivate(appId);
125 } else if (action.equals("uninstall")) {
126 service.uninstall(appId);
127 }
128 chain(APP_DATA_REQ, sid, payload);
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700129 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700130 }
131 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700132}