blob: 77a58972357ff89621a9acc5d7ccce96bc35cde8 [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 {
Simon Huntd2747a02015-04-30 22:41:16 -070069 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070070 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070071 }
72
73 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070074 protected String[] getColumnIds() {
75 return COL_IDS;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070076 }
Simon Huntabd16f62015-05-01 13:14:40 -070077
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 @Override
79 protected void populateTable(TableModel tm, ObjectNode payload) {
80 ApplicationService as = get(ApplicationService.class);
81 for (Application app : as.getApplications()) {
82 populateRow(tm.addRow(), app, as);
83 }
84 }
85
86 private void populateRow(TableModel.Row row, Application app,
87 ApplicationService as) {
88 ApplicationId id = app.id();
89 ApplicationState state = as.getState(id);
90 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
91
92 row.cell(STATE, state)
93 .cell(STATE_IID, iconId)
94 .cell(ID, id.name())
95 .cell(VERSION, app.version())
96 .cell(ORIGIN, app.origin())
97 .cell(DESC, app.description());
98 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070099 }
100
Simon Huntabd16f62015-05-01 13:14:40 -0700101 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -0700102 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700103 private AppMgmtRequest() {
104 super(APP_MGMT_REQ);
105 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700106
Simon Huntd2747a02015-04-30 22:41:16 -0700107 @Override
108 public void process(long sid, ObjectNode payload) {
109 String action = string(payload, "action");
110 String name = string(payload, "name");
111 if (action != null && name != null) {
112 ApplicationAdminService service = get(ApplicationAdminService.class);
113 ApplicationId appId = service.getId(name);
114 if (action.equals("activate")) {
115 service.activate(appId);
116 } else if (action.equals("deactivate")) {
117 service.deactivate(appId);
118 } else if (action.equals("uninstall")) {
119 service.uninstall(appId);
120 }
121 chain(APP_DATA_REQ, sid, payload);
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700122 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700123 }
124 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700125}