blob: dca882eb05321ca8697e5528c8c911822c672112 [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
Jian Lia54de5a2016-01-20 23:10:39 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070021import org.onosproject.app.ApplicationAdminService;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070022import org.onosproject.app.ApplicationService;
23import org.onosproject.app.ApplicationState;
24import org.onosproject.core.Application;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070025import org.onosproject.core.ApplicationId;
Simon Huntd2747a02015-04-30 22:41:16 -070026import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070027import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070028import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070029import org.onosproject.ui.table.TableRequestHandler;
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 -070032
33import static org.onosproject.app.ApplicationState.ACTIVE;
34
35/**
36 * Message handler for application view related messages.
37 */
Simon Hunta0ddb022015-05-01 09:53:01 -070038public class ApplicationViewMessageHandler extends UiMessageHandler {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070039
Simon Huntd2747a02015-04-30 22:41:16 -070040 private static final String APP_DATA_REQ = "appDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070041 private static final String APP_DATA_RESP = "appDataResponse";
42 private static final String APPS = "apps";
43
Simon Huntd2747a02015-04-30 22:41:16 -070044 private static final String APP_MGMT_REQ = "appManagementRequest";
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070045
Jian Lia54de5a2016-01-20 23:10:39 -080046 private static final String APP_DETAILS_REQ = "appDetailsRequest";
47 private static final String APP_DETAILS_RESP = "appDetailsResponse";
48 private static final String DETAILS = "details";
49
Simon Huntabd16f62015-05-01 13:14:40 -070050 private static final String STATE = "state";
51 private static final String STATE_IID = "_iconid_state";
52 private static final String ID = "id";
Jian Li97d6b2d2016-01-20 10:13:43 -080053 private static final String ICON = "icon";
Simon Huntabd16f62015-05-01 13:14:40 -070054 private static final String VERSION = "version";
Jian Li97d6b2d2016-01-20 10:13:43 -080055 private static final String CATEGORY = "category";
Simon Huntabd16f62015-05-01 13:14:40 -070056 private static final String ORIGIN = "origin";
57 private static final String DESC = "desc";
Jian Li97d6b2d2016-01-20 10:13:43 -080058 private static final String URL = "url";
Jian Lia54de5a2016-01-20 23:10:39 -080059 private static final String README = "readme";
60 private static final String ROLE = "role";
Simon Hunt31642932016-01-22 20:34:00 -080061 private static final String REQUIRED_APPS = "required_apps";
Jian Lia54de5a2016-01-20 23:10:39 -080062 private static final String FEATURES = "features";
Jian Lida253e02016-01-21 17:46:17 -080063 private static final String PERMISSIONS = "permissions";
Simon Huntabd16f62015-05-01 13:14:40 -070064
65 private static final String ICON_ID_ACTIVE = "active";
66 private static final String ICON_ID_INACTIVE = "appInactive";
67
Simon Hunt3d1b0652015-05-05 17:27:24 -070068 private static final String[] COL_IDS = {
Jian Lia54de5a2016-01-20 23:10:39 -080069 STATE, STATE_IID, ID, ICON, VERSION, CATEGORY, ORIGIN, DESC,
Jian Lida253e02016-01-21 17:46:17 -080070 URL, README, ROLE, REQUIRED_APPS, FEATURES, PERMISSIONS
Simon Hunt3d1b0652015-05-05 17:27:24 -070071 };
72
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070073 @Override
Simon Huntda580882015-05-12 20:58:18 -070074 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070075 return ImmutableSet.of(
76 new AppDataRequest(),
Jian Lia54de5a2016-01-20 23:10:39 -080077 new AppMgmtRequest(),
78 new DetailRequestHandler()
Simon Huntd2747a02015-04-30 22:41:16 -070079 );
80 }
81
Simon Huntabd16f62015-05-01 13:14:40 -070082 // handler for application table requests
83 private final class AppDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080084 private static final String NO_ROWS_MESSAGE = "No applications found";
85
Simon Huntd2747a02015-04-30 22:41:16 -070086 private AppDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070087 super(APP_DATA_REQ, APP_DATA_RESP, APPS);
Simon Huntd2747a02015-04-30 22:41:16 -070088 }
89
90 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070091 protected String[] getColumnIds() {
92 return COL_IDS;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070093 }
Simon Huntabd16f62015-05-01 13:14:40 -070094
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 @Override
Jian Li8baf4472016-01-15 15:08:09 -080096 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080097 return NO_ROWS_MESSAGE;
98 }
99
100 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700101 protected void populateTable(TableModel tm, ObjectNode payload) {
102 ApplicationService as = get(ApplicationService.class);
103 for (Application app : as.getApplications()) {
104 populateRow(tm.addRow(), app, as);
105 }
106 }
107
108 private void populateRow(TableModel.Row row, Application app,
109 ApplicationService as) {
110 ApplicationId id = app.id();
111 ApplicationState state = as.getState(id);
112 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
113
114 row.cell(STATE, state)
115 .cell(STATE_IID, iconId)
116 .cell(ID, id.name())
Jian Li97d6b2d2016-01-20 10:13:43 -0800117 .cell(ICON, id.name())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700118 .cell(VERSION, app.version())
Jian Li97d6b2d2016-01-20 10:13:43 -0800119 .cell(CATEGORY, app.category())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700120 .cell(ORIGIN, app.origin())
Jian Li97d6b2d2016-01-20 10:13:43 -0800121 .cell(DESC, app.description())
122 .cell(URL, app.url());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700123 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700124 }
125
Simon Huntabd16f62015-05-01 13:14:40 -0700126 // handler for application management control button actions
Simon Huntd2747a02015-04-30 22:41:16 -0700127 private final class AppMgmtRequest extends RequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700128 private AppMgmtRequest() {
129 super(APP_MGMT_REQ);
130 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700131
Simon Huntd2747a02015-04-30 22:41:16 -0700132 @Override
133 public void process(long sid, ObjectNode payload) {
134 String action = string(payload, "action");
135 String name = string(payload, "name");
136 if (action != null && name != null) {
137 ApplicationAdminService service = get(ApplicationAdminService.class);
138 ApplicationId appId = service.getId(name);
139 if (action.equals("activate")) {
140 service.activate(appId);
141 } else if (action.equals("deactivate")) {
142 service.deactivate(appId);
143 } else if (action.equals("uninstall")) {
144 service.uninstall(appId);
145 }
146 chain(APP_DATA_REQ, sid, payload);
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700147 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700148 }
149 }
Jian Lia54de5a2016-01-20 23:10:39 -0800150
151 // handler for selected application detail requests
152 private final class DetailRequestHandler extends RequestHandler {
153 private DetailRequestHandler() {
154 super(APP_DETAILS_REQ);
155 }
156
157 @Override
158 public void process(long sid, ObjectNode payload) {
159 String id = string(payload, ID);
160 ApplicationService as = get(ApplicationService.class);
161 ApplicationId appId = as.getId(id);
162 ApplicationState state = as.getState(appId);
163 Application app = as.getApplication(appId);
164 ObjectNode data = objectNode();
165
166 data.put(STATE, state.toString());
167 data.put(ID, appId.name());
168 data.put(VERSION, app.version().toString());
169 data.put(ROLE, app.role().toString());
170 data.put(CATEGORY, app.category());
171 data.put(ORIGIN, app.origin());
172 data.put(README, app.readme());
Jian Lida253e02016-01-21 17:46:17 -0800173 data.put(DESC, app.description());
Jian Lia54de5a2016-01-20 23:10:39 -0800174 data.put(URL, app.url());
175
176 // process required applications
177 ArrayNode requiredApps = arrayNode();
Simon Hunt31642932016-01-22 20:34:00 -0800178 app.requiredApps().forEach(requiredApps::add);
Jian Lia54de5a2016-01-20 23:10:39 -0800179
180 data.set(REQUIRED_APPS, requiredApps);
181
182 // process features
183 ArrayNode features = arrayNode();
Simon Hunt31642932016-01-22 20:34:00 -0800184 app.features().forEach(features::add);
Jian Lia54de5a2016-01-20 23:10:39 -0800185
186 data.set(FEATURES, features);
187
Jian Lida253e02016-01-21 17:46:17 -0800188 // process permissions
189 ArrayNode permissions = arrayNode();
190 app.permissions().forEach(p -> permissions.add(p.getName()));
191
192 data.set(PERMISSIONS, permissions);
193
Jian Lia54de5a2016-01-20 23:10:39 -0800194 ObjectNode rootNode = objectNode();
195 rootNode.set(DETAILS, data);
196 sendMessage(APP_DETAILS_RESP, 0, rootNode);
197 }
198 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700199}