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 | |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiConnection; |
| 25 | import org.onosproject.ui.UiMessageHandler; |
| 26 | import org.onosproject.ui.table.TableModel; |
| 27 | import org.onosproject.ui.table.TableRequestHandler; |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 28 | import org.onosproject.yang.model.YangModel; |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 29 | import org.onosproject.yang.model.YangModule; |
Thomas Vachuska | 0cb73f4 | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 30 | import org.onosproject.yang.model.YangModuleId; |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 31 | import org.onosproject.yang.runtime.YangModelRegistry; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 35 | import java.io.BufferedReader; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.io.InputStreamReader; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 39 | import java.util.Collection; |
| 40 | |
| 41 | /** |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 42 | * ONOS UI YANG Models message handler. |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 43 | */ |
| 44 | public class YangModelMessageHandler extends UiMessageHandler { |
| 45 | |
| 46 | private static final String TABLE_REQ = "yangModelDataRequest"; |
| 47 | private static final String TABLE_RESP = "yangModelDataResponse"; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 48 | private static final String MODELS = "yangModels"; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 49 | |
| 50 | private static final String DETAILS_REQ = "yangModelDetailsRequest"; |
| 51 | private static final String DETAILS_RESP = "yangModelDetailsResponse"; |
| 52 | private static final String DETAILS = "details"; |
| 53 | |
| 54 | // Table Column IDs |
| 55 | private static final String ID = "id"; |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 56 | private static final String REVISION = "revision"; |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 57 | private static final String MODEL_ID = "modelId"; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 58 | // TODO: fill out table columns as needed |
| 59 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 60 | private static final String SOURCE = "source"; |
| 61 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 62 | private static final String[] COL_IDS = { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 63 | ID, REVISION, MODEL_ID |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 66 | private static final String UTF8 = "UTF-8"; |
| 67 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 68 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 69 | |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 70 | private YangModelRegistry modelRegistry; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 71 | // TODO: fill out other fields as necessary |
| 72 | |
| 73 | |
| 74 | // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-=========== |
| 75 | |
| 76 | @Override |
| 77 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 78 | super.init(connection, directory); |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 79 | modelRegistry = directory.get(YangModelRegistry.class); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void destroy() { |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 84 | super.destroy(); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | protected Collection<RequestHandler> createRequestHandlers() { |
| 89 | return ImmutableSet.of( |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 90 | new TableDataHandler(), |
| 91 | new DetailRequestHandler() |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 92 | ); |
| 93 | } |
| 94 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 95 | // Handler for table requests |
| 96 | private final class TableDataHandler extends TableRequestHandler { |
| 97 | private static final String NO_ROWS_MESSAGE = "No YANG Models found"; |
| 98 | |
| 99 | private TableDataHandler() { |
| 100 | super(TABLE_REQ, TABLE_RESP, MODELS); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected String[] getColumnIds() { |
| 105 | return COL_IDS; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected String noRowsMessage(ObjectNode payload) { |
| 110 | return NO_ROWS_MESSAGE; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | protected void populateTable(TableModel tm, ObjectNode payload) { |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 115 | for (YangModel model : modelRegistry.getModels()) { |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 116 | for (YangModuleId id : model.getYangModulesId()) { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 117 | populateRow(tm.addRow(), modelId(model), id); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 118 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 122 | private void populateRow(TableModel.Row row, String modelId, YangModuleId moduleId) { |
| 123 | row.cell(ID, moduleId.moduleName()) |
Thomas Vachuska | 770ef84 | 2017-03-31 00:15:49 -0700 | [diff] [blame] | 124 | .cell(REVISION, moduleId.revision()) |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 125 | .cell(MODEL_ID, modelId); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // handler for selected model detail requests (selected table row) |
| 131 | private final class DetailRequestHandler extends RequestHandler { |
| 132 | private DetailRequestHandler() { |
| 133 | super(DETAILS_REQ); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void process(ObjectNode payload) { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 138 | String name = string(payload, ID); |
| 139 | String modelId = string(payload, MODEL_ID); |
| 140 | YangModule module = getModule(modelId, name); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 141 | |
| 142 | ObjectNode data = objectNode(); |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 143 | data.put(ID, name); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 144 | if (module != null) { |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 145 | data.put(REVISION, module.getYangModuleId().revision()); |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 146 | data.put(MODEL_ID, modelId); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 147 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 148 | ArrayNode source = arrayNode(); |
| 149 | data.set(SOURCE, source); |
| 150 | |
| 151 | addSource(source, module.getYangSource()); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 152 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 153 | |
| 154 | ObjectNode rootNode = objectNode(); |
| 155 | rootNode.set(DETAILS, data); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 156 | sendMessage(DETAILS_RESP, rootNode); |
| 157 | } |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 158 | |
| 159 | private void addSource(ArrayNode source, InputStream yangSource) { |
| 160 | try (InputStreamReader isr = new InputStreamReader(yangSource); |
| 161 | BufferedReader br = new BufferedReader(isr)) { |
| 162 | String line; |
| 163 | while ((line = br.readLine()) != null) { |
| 164 | source.add(line); |
| 165 | } |
| 166 | |
| 167 | } catch (IOException e) { |
| 168 | log.warn("Unable to read YANG source", e); |
| 169 | return; |
| 170 | } |
| 171 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 172 | } |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 173 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 174 | |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 175 | private YangModule getModule(String modelId, String name) { |
| 176 | int nid = Integer.parseInt(modelId.substring(2)); |
| 177 | log.info("Got {}; {}", modelId, nid); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 178 | YangModel model = modelRegistry.getModels().stream() |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 179 | .filter(m -> modelId(m).equals(modelId)) |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 180 | .findFirst().orElse(null); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 181 | if (model != null) { |
| 182 | log.info("Got model"); |
| 183 | return model.getYangModules().stream() |
| 184 | .filter(m -> m.getYangModuleId().moduleName().contentEquals(name)) |
| 185 | .findFirst().orElse(null); |
| 186 | } |
| 187 | return null; |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 188 | } |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 189 | |
| 190 | private String modelId(YangModel m) { |
| 191 | return "YM" + Math.abs(m.hashCode()); |
| 192 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 193 | } |