blob: 3ca6e88892b14c6a101da01561f4da01c90e12fb [file] [log] [blame]
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -07003 *
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.ObjectNode;
19import com.google.common.collect.ImmutableSet;
20import org.onosproject.cfg.ComponentConfigService;
21import org.onosproject.cfg.ConfigProperty;
22import org.onosproject.ui.RequestHandler;
23import org.onosproject.ui.UiMessageHandler;
24import org.onosproject.ui.table.TableModel;
25import org.onosproject.ui.table.TableRequestHandler;
26
27import java.util.Collection;
28
29/**
30 * Message handler for component configuration view related messages.
31 */
32public class SettingsViewMessageHandler extends UiMessageHandler {
33
34 private static final String DATA_REQUEST = "settingDataRequest";
35 private static final String DATA_RESPONSE = "settingDataResponse";
36 private static final String SETTINGS = "settings";
37
38 private static final String COMPONENT = "component";
Simon Huntd721f292016-06-22 21:42:23 -070039 private static final String FQ_COMPONENT = "fqComponent";
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070040 private static final String ID = "id";
41 private static final String TYPE = "type";
42 private static final String VALUE = "value";
43 private static final String DEFAULT = "defValue";
44 private static final String DESC = "desc";
45
Simon Huntd721f292016-06-22 21:42:23 -070046 private static final char DOT = '.';
47
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070048 private static final String[] COL_IDS = {
Simon Huntd721f292016-06-22 21:42:23 -070049 COMPONENT, FQ_COMPONENT, ID, TYPE, VALUE, DEFAULT, DESC
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070050 };
51
52 @Override
53 protected Collection<RequestHandler> createRequestHandlers() {
54 return ImmutableSet.of(new SettingsRequest());
55 }
56
57 // handler for host table requests
58 private final class SettingsRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080059 private static final String NO_ROWS_MESSAGE = "No settings found";
60
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070061 private SettingsRequest() {
62 super(DATA_REQUEST, DATA_RESPONSE, SETTINGS);
63 }
64
65 @Override
66 protected String[] getColumnIds() {
67 return COL_IDS;
68 }
69
70 @Override
Jian Li8baf4472016-01-15 15:08:09 -080071 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080072 return NO_ROWS_MESSAGE;
73 }
74
75 @Override
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070076 protected String defaultColumnId() {
77 return COMPONENT;
78 }
79
80 @Override
81 protected TableModel createTableModel() {
82 TableModel tm = super.createTableModel();
83 return tm;
84 }
85
86 @Override
87 protected void populateTable(TableModel tm, ObjectNode payload) {
88 ComponentConfigService ccs = get(ComponentConfigService.class);
89 for (String component : ccs.getComponentNames()) {
90 for (ConfigProperty prop : ccs.getProperties(component)) {
91 populateRow(tm.addRow(), component, prop);
92 }
93 }
94 }
95
Simon Huntd721f292016-06-22 21:42:23 -070096 private void populateRow(TableModel.Row row, String fqComp,
97 ConfigProperty prop) {
98 row.cell(COMPONENT, simpleName(fqComp))
99 .cell(FQ_COMPONENT, fqComp)
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700100 .cell(ID, prop.name())
Simon Huntd721f292016-06-22 21:42:23 -0700101 .cell(TYPE, typeName(prop))
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700102 .cell(VALUE, prop.value())
103 .cell(DEFAULT, prop.defaultValue())
104 .cell(DESC, prop.description());
105 }
Simon Huntd721f292016-06-22 21:42:23 -0700106
107 // return just simple class name: "a.b.c.MyClass" -> "MyClass"
108 private String simpleName(String component) {
109 int lastDot = component.lastIndexOf(DOT);
110 return lastDot < 0 ? component : component.substring(lastDot + 1);
111 }
112
113 private String typeName(ConfigProperty prop) {
114 return prop.type().toString().toLowerCase();
115 }
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700116 }
117}