Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.cfg.ComponentConfigService; |
| 21 | import org.onosproject.cfg.ConfigProperty; |
| 22 | import org.onosproject.ui.RequestHandler; |
| 23 | import org.onosproject.ui.UiMessageHandler; |
| 24 | import org.onosproject.ui.table.TableModel; |
| 25 | import org.onosproject.ui.table.TableRequestHandler; |
| 26 | |
| 27 | import java.util.Collection; |
| 28 | |
| 29 | /** |
| 30 | * Message handler for component configuration view related messages. |
| 31 | */ |
| 32 | public 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 Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 39 | private static final String FQ_COMPONENT = "fqComponent"; |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 40 | private static final String PROP = "prop"; |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 41 | 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 Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 47 | private static final char DOT = '.'; |
| 48 | |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 49 | private static final String[] COL_IDS = { |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 50 | COMPONENT, FQ_COMPONENT, PROP, ID, TYPE, VALUE, DEFAULT, DESC |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 51 | }; |
| 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 Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 60 | private static final String NO_ROWS_MESSAGE = "No settings found"; |
| 61 | |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 62 | 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 Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 72 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 73 | return NO_ROWS_MESSAGE; |
| 74 | } |
| 75 | |
| 76 | @Override |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 77 | protected String defaultColumnId() { |
| 78 | return COMPONENT; |
| 79 | } |
| 80 | |
| 81 | @Override |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 82 | 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 Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 91 | private void populateRow(TableModel.Row row, String fqComp, |
| 92 | ConfigProperty prop) { |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 93 | String simple = simpleName(fqComp); |
| 94 | |
| 95 | row.cell(ID, simple + DELIM + prop.name()) |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 96 | .cell(FQ_COMPONENT, fqComp) |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 97 | .cell(COMPONENT, simple) |
| 98 | .cell(PROP, prop.name()) |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 99 | .cell(TYPE, typeName(prop)) |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 100 | .cell(VALUE, prop.value()) |
| 101 | .cell(DEFAULT, prop.defaultValue()) |
| 102 | .cell(DESC, prop.description()); |
| 103 | } |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 104 | |
| 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 Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 114 | |
| 115 | private static final String DELIM = "::"; |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 116 | } |
| 117 | } |