blob: 632f721550ffefd22905db96fd656d233ce14f8f [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";
Simon Hunt50e80ea2016-06-23 01:40:30 -070040 private static final String PROP = "prop";
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070041 private static final String ID = "id";
42 private static final String TYPE = "type";
43 private static final String VALUE = "value";
44 private static final String DEFAULT = "defValue";
45 private static final String DESC = "desc";
46
Simon Huntd721f292016-06-22 21:42:23 -070047 private static final char DOT = '.';
48
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070049 private static final String[] COL_IDS = {
Simon Hunt50e80ea2016-06-23 01:40:30 -070050 COMPONENT, FQ_COMPONENT, PROP, ID, TYPE, VALUE, DEFAULT, DESC
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070051 };
52
53 @Override
54 protected Collection<RequestHandler> createRequestHandlers() {
55 return ImmutableSet.of(new SettingsRequest());
56 }
57
58 // handler for host table requests
59 private final class SettingsRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080060 private static final String NO_ROWS_MESSAGE = "No settings found";
61
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070062 private SettingsRequest() {
63 super(DATA_REQUEST, DATA_RESPONSE, SETTINGS);
64 }
65
66 @Override
67 protected String[] getColumnIds() {
68 return COL_IDS;
69 }
70
71 @Override
Jian Li8baf4472016-01-15 15:08:09 -080072 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080073 return NO_ROWS_MESSAGE;
74 }
75
76 @Override
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070077 protected String defaultColumnId() {
78 return COMPONENT;
79 }
80
81 @Override
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070082 protected void populateTable(TableModel tm, ObjectNode payload) {
83 ComponentConfigService ccs = get(ComponentConfigService.class);
84 for (String component : ccs.getComponentNames()) {
85 for (ConfigProperty prop : ccs.getProperties(component)) {
86 populateRow(tm.addRow(), component, prop);
87 }
88 }
89 }
90
Simon Huntd721f292016-06-22 21:42:23 -070091 private void populateRow(TableModel.Row row, String fqComp,
92 ConfigProperty prop) {
Simon Hunt50e80ea2016-06-23 01:40:30 -070093 String simple = simpleName(fqComp);
94
95 row.cell(ID, simple + DELIM + prop.name())
Simon Huntd721f292016-06-22 21:42:23 -070096 .cell(FQ_COMPONENT, fqComp)
Simon Hunt50e80ea2016-06-23 01:40:30 -070097 .cell(COMPONENT, simple)
98 .cell(PROP, prop.name())
Simon Huntd721f292016-06-22 21:42:23 -070099 .cell(TYPE, typeName(prop))
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700100 .cell(VALUE, prop.value())
101 .cell(DEFAULT, prop.defaultValue())
102 .cell(DESC, prop.description());
103 }
Simon Huntd721f292016-06-22 21:42:23 -0700104
105 // return just simple class name: "a.b.c.MyClass" -> "MyClass"
106 private String simpleName(String component) {
107 int lastDot = component.lastIndexOf(DOT);
108 return lastDot < 0 ? component : component.substring(lastDot + 1);
109 }
110
111 private String typeName(ConfigProperty prop) {
112 return prop.type().toString().toLowerCase();
113 }
Simon Hunt50e80ea2016-06-23 01:40:30 -0700114
115 private static final String DELIM = "::";
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -0700116 }
117}