Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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 | */ |
| 16 | |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 17 | package org.onosproject.yang.gui; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onosproject.ui.RequestHandler; |
| 23 | import org.onosproject.ui.UiConnection; |
| 24 | import org.onosproject.ui.UiMessageHandler; |
| 25 | import org.onosproject.ui.table.TableModel; |
| 26 | import org.onosproject.ui.table.TableRequestHandler; |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 27 | import org.onosproject.yang.model.YangModel; |
| 28 | import org.onosproject.yang.runtime.YangModelRegistry; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.Collection; |
| 33 | |
| 34 | /** |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 35 | * ONOS UI YANG Models message handler. |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class YangModelMessageHandler extends UiMessageHandler { |
| 38 | |
| 39 | private static final String TABLE_REQ = "yangModelDataRequest"; |
| 40 | private static final String TABLE_RESP = "yangModelDataResponse"; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 41 | private static final String MODELS = "yangModels"; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 42 | |
| 43 | private static final String DETAILS_REQ = "yangModelDetailsRequest"; |
| 44 | private static final String DETAILS_RESP = "yangModelDetailsResponse"; |
| 45 | private static final String DETAILS = "details"; |
| 46 | |
| 47 | // Table Column IDs |
| 48 | private static final String ID = "id"; |
| 49 | private static final String TYPE = "type"; |
| 50 | // TODO: fill out table columns as needed |
| 51 | |
| 52 | private static final String[] COL_IDS = { |
| 53 | ID, TYPE |
| 54 | }; |
| 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 58 | private YangModelRegistry modelRegistry; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 59 | // TODO: fill out other fields as necessary |
| 60 | |
| 61 | |
| 62 | // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-=========== |
| 63 | |
| 64 | @Override |
| 65 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 66 | super.init(connection, directory); |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 67 | modelRegistry = directory.get(YangModelRegistry.class); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 68 | // TODO: addListeners(); ??? |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void destroy() { |
| 73 | // TODO: removeListeners(); ??? |
| 74 | super.destroy(); |
| 75 | // NOTE: if no listeners are required, this method can be removed |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected Collection<RequestHandler> createRequestHandlers() { |
| 80 | return ImmutableSet.of( |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 81 | new TableDataHandler(), |
| 82 | new DetailRequestHandler() |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 83 | ); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | // Handler for table requests |
| 88 | private final class TableDataHandler extends TableRequestHandler { |
| 89 | private static final String NO_ROWS_MESSAGE = "No YANG Models found"; |
| 90 | |
| 91 | private TableDataHandler() { |
| 92 | super(TABLE_REQ, TABLE_RESP, MODELS); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected String[] getColumnIds() { |
| 97 | return COL_IDS; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected String noRowsMessage(ObjectNode payload) { |
| 102 | return NO_ROWS_MESSAGE; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected void populateTable(TableModel tm, ObjectNode payload) { |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 107 | for (YangModel model : modelRegistry.getModels()) { |
| 108 | populateRow(tm.addRow(), model.getYangModulesId().toString()); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | // TODO: obviously, this should be adapted to arrange YANG model data |
| 113 | // into the appropriate table columns |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame^] | 114 | private void populateRow(TableModel.Row row, String k) { |
| 115 | row.cell(ID, k).cell(TYPE, k); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // handler for selected model detail requests (selected table row) |
| 121 | private final class DetailRequestHandler extends RequestHandler { |
| 122 | private DetailRequestHandler() { |
| 123 | super(DETAILS_REQ); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void process(ObjectNode payload) { |
| 128 | String id = string(payload, ID); |
| 129 | |
| 130 | // TODO: retrieve the appropriate model from ymsService and create |
| 131 | // a detail record to send back to the client. |
| 132 | |
| 133 | ObjectNode data = objectNode(); |
| 134 | |
| 135 | data.put(ID, id); |
| 136 | data.put(TYPE, "some-type"); |
| 137 | data.put("todo", "fill out with appropriate date attributes"); |
| 138 | |
| 139 | ObjectNode rootNode = objectNode(); |
| 140 | rootNode.set(DETAILS, data); |
| 141 | |
| 142 | sendMessage(DETAILS_RESP, rootNode); |
| 143 | } |
| 144 | } |
| 145 | } |