blob: d19c9875e1aeef1fc4d6364b18a09c650bb6f1be [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
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import 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;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070026
27import java.util.Arrays;
28import java.util.List;
29import java.util.stream.Collectors;
30
31import static org.onosproject.app.ApplicationState.ACTIVE;
32
33/**
34 * Message handler for application view related messages.
35 */
36public class ApplicationViewMessageHandler extends AbstractTabularViewMessageHandler {
37
38 /**
39 * Creates a new message handler for the application messages.
40 */
41 protected ApplicationViewMessageHandler() {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070042 super(ImmutableSet.of("appDataRequest", "appManagementRequest"));
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070043 }
44
45 @Override
46 public void process(ObjectNode message) {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070047 String type = string(message, "event", "unknown");
48 if (type.equals("appDataRequest")) {
49 sendAppList(message);
50 } else if (type.equals("appManagementRequest")) {
51 processManagementCommand(message);
52 }
53 }
54
55 private void sendAppList(ObjectNode message) {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070056 ObjectNode payload = payload(message);
57 String sortCol = string(payload, "sortCol", "id");
58 String sortDir = string(payload, "sortDir", "asc");
59
60 ApplicationService service = get(ApplicationService.class);
61 TableRow[] rows = generateTableRows(service);
62 RowComparator rc =
63 new RowComparator(sortCol, RowComparator.direction(sortDir));
64 Arrays.sort(rows, rc);
65 ArrayNode applications = generateArrayNode(rows);
66 ObjectNode rootNode = mapper.createObjectNode();
Thomas Vachuska619c5382015-04-02 13:41:47 -070067 rootNode.set("apps", applications);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070068
69 connection().sendMessage("appDataResponse", 0, rootNode);
70 }
71
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070072 private void processManagementCommand(ObjectNode message) {
73 ObjectNode payload = payload(message);
74 String action = string(payload, "action");
75 String name = string(payload, "name");
76 if (action != null && name != null) {
77 ApplicationAdminService service = get(ApplicationAdminService.class);
78 ApplicationId appId = service.getId(name);
79 if (action.equals("activate")) {
80 service.activate(appId);
81 } else if (action.equals("deactivate")) {
82 service.deactivate(appId);
83 } else if (action.equals("uninstall")) {
84 service.uninstall(appId);
85 }
86 sendAppList(message);
87 }
88 }
89
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070090 private TableRow[] generateTableRows(ApplicationService service) {
91 List<TableRow> list = service.getApplications().stream()
92 .map(application -> new ApplicationTableRow(service, application))
93 .collect(Collectors.toList());
94 return list.toArray(new TableRow[list.size()]);
95 }
96
97 /**
98 * TableRow implementation for {@link org.onosproject.core.Application applications}.
99 */
100 private static class ApplicationTableRow extends AbstractTableRow {
101
102 private static final String STATE = "state";
103 private static final String STATE_IID = "_iconid_state";
104 private static final String ID = "id";
105 private static final String VERSION = "version";
106 private static final String ORIGIN = "origin";
107 private static final String DESC = "desc";
108
109 private static final String[] COL_IDS = {
110 STATE, STATE_IID, ID, VERSION, ORIGIN, DESC
111 };
112
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700113 private static final String ICON_ID_ACTIVE = "active";
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700114 private static final String ICON_ID_INACTIVE = "appInactive";
115
116
117 public ApplicationTableRow(ApplicationService service, Application app) {
118 ApplicationState state = service.getState(app.id());
119 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
120
121 add(STATE, state.toString());
122 add(STATE_IID, iconId);
123 add(ID, app.id().name());
124 add(VERSION, app.version().toString());
125 add(ORIGIN, app.origin());
126 add(DESC, app.description());
127 }
128
129 @Override
130 protected String[] columnIds() {
131 return COL_IDS;
132 }
133 }
134
135}