blob: 800b57d7557619e12bb2a6fe1ee720b02bb94f14 [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;
21import org.onosproject.app.ApplicationService;
22import org.onosproject.app.ApplicationState;
23import org.onosproject.core.Application;
24
25import java.util.Arrays;
26import java.util.List;
27import java.util.stream.Collectors;
28
29import static org.onosproject.app.ApplicationState.ACTIVE;
30
31/**
32 * Message handler for application view related messages.
33 */
34public class ApplicationViewMessageHandler extends AbstractTabularViewMessageHandler {
35
36 /**
37 * Creates a new message handler for the application messages.
38 */
39 protected ApplicationViewMessageHandler() {
40 super(ImmutableSet.of("appDataRequest"));
41 }
42
43 @Override
44 public void process(ObjectNode message) {
45 ObjectNode payload = payload(message);
46 String sortCol = string(payload, "sortCol", "id");
47 String sortDir = string(payload, "sortDir", "asc");
48
49 ApplicationService service = get(ApplicationService.class);
50 TableRow[] rows = generateTableRows(service);
51 RowComparator rc =
52 new RowComparator(sortCol, RowComparator.direction(sortDir));
53 Arrays.sort(rows, rc);
54 ArrayNode applications = generateArrayNode(rows);
55 ObjectNode rootNode = mapper.createObjectNode();
56 rootNode.set("applications", applications);
57
58 connection().sendMessage("appDataResponse", 0, rootNode);
59 }
60
61 private TableRow[] generateTableRows(ApplicationService service) {
62 List<TableRow> list = service.getApplications().stream()
63 .map(application -> new ApplicationTableRow(service, application))
64 .collect(Collectors.toList());
65 return list.toArray(new TableRow[list.size()]);
66 }
67
68 /**
69 * TableRow implementation for {@link org.onosproject.core.Application applications}.
70 */
71 private static class ApplicationTableRow extends AbstractTableRow {
72
73 private static final String STATE = "state";
74 private static final String STATE_IID = "_iconid_state";
75 private static final String ID = "id";
76 private static final String VERSION = "version";
77 private static final String ORIGIN = "origin";
78 private static final String DESC = "desc";
79
80 private static final String[] COL_IDS = {
81 STATE, STATE_IID, ID, VERSION, ORIGIN, DESC
82 };
83
84 private static final String ICON_ID_ACTIVE = "appActive";
85 private static final String ICON_ID_INACTIVE = "appInactive";
86
87
88 public ApplicationTableRow(ApplicationService service, Application app) {
89 ApplicationState state = service.getState(app.id());
90 String iconId = state == ACTIVE ? ICON_ID_ACTIVE : ICON_ID_INACTIVE;
91
92 add(STATE, state.toString());
93 add(STATE_IID, iconId);
94 add(ID, app.id().name());
95 add(VERSION, app.version().toString());
96 add(ORIGIN, app.origin());
97 add(DESC, app.description());
98 }
99
100 @Override
101 protected String[] columnIds() {
102 return COL_IDS;
103 }
104 }
105
106}