Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [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 | |
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 | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 28 | import org.onosproject.yang.YangClassLoaderRegistry; |
| 29 | import org.onosproject.yang.model.DefaultYangModuleId; |
| 30 | import org.onosproject.yang.model.ModelException; |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 31 | import org.onosproject.yang.model.YangModel; |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 32 | import org.onosproject.yang.model.YangModule; |
Thomas Vachuska | 0cb73f4 | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 33 | import org.onosproject.yang.model.YangModuleId; |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 34 | import org.onosproject.yang.runtime.YangModelRegistry; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 38 | import java.io.BufferedReader; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.io.InputStreamReader; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 42 | import java.util.Collection; |
| 43 | |
| 44 | /** |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 45 | * ONOS UI YANG Models message handler. |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 46 | */ |
| 47 | public class YangModelMessageHandler extends UiMessageHandler { |
| 48 | |
| 49 | private static final String TABLE_REQ = "yangModelDataRequest"; |
| 50 | private static final String TABLE_RESP = "yangModelDataResponse"; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 51 | private static final String MODELS = "yangModels"; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 52 | |
| 53 | private static final String DETAILS_REQ = "yangModelDetailsRequest"; |
| 54 | private static final String DETAILS_RESP = "yangModelDetailsResponse"; |
| 55 | private static final String DETAILS = "details"; |
| 56 | |
| 57 | // Table Column IDs |
| 58 | private static final String ID = "id"; |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 59 | private static final String REVISION = "revision"; |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 60 | private static final String MODEL_ID = "modelId"; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 61 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 62 | private static final String SOURCE = "source"; |
| 63 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 64 | private static final String[] COL_IDS = { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 65 | ID, REVISION, MODEL_ID |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 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; |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 71 | private YangClassLoaderRegistry classLoaderRegistry; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 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); |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 80 | classLoaderRegistry = directory.get(YangClassLoaderRegistry.class); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void destroy() { |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 85 | super.destroy(); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected Collection<RequestHandler> createRequestHandlers() { |
| 90 | return ImmutableSet.of( |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 91 | new TableDataHandler(), |
| 92 | new DetailRequestHandler() |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 93 | ); |
| 94 | } |
| 95 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 96 | // Handler for table requests |
| 97 | private final class TableDataHandler extends TableRequestHandler { |
| 98 | private static final String NO_ROWS_MESSAGE = "No YANG Models found"; |
| 99 | |
| 100 | private TableDataHandler() { |
| 101 | super(TABLE_REQ, TABLE_RESP, MODELS); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected String[] getColumnIds() { |
| 106 | return COL_IDS; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected String noRowsMessage(ObjectNode payload) { |
| 111 | return NO_ROWS_MESSAGE; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void populateTable(TableModel tm, ObjectNode payload) { |
Thomas Vachuska | 04059f9 | 2017-03-07 15:16:23 -0800 | [diff] [blame] | 116 | for (YangModel model : modelRegistry.getModels()) { |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 117 | for (YangModuleId id : model.getYangModulesId()) { |
Thomas Vachuska | 8953445 | 2017-07-28 10:20:12 -0700 | [diff] [blame] | 118 | populateRow(tm.addRow(), model.getYangModelId(), id); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 119 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 123 | private void populateRow(TableModel.Row row, String modelId, |
| 124 | YangModuleId moduleId) { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 125 | row.cell(ID, moduleId.moduleName()) |
Thomas Vachuska | 770ef84 | 2017-03-31 00:15:49 -0700 | [diff] [blame] | 126 | .cell(REVISION, moduleId.revision()) |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 127 | .cell(MODEL_ID, modelId); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | // handler for selected model detail requests (selected table row) |
| 133 | private final class DetailRequestHandler extends RequestHandler { |
| 134 | private DetailRequestHandler() { |
| 135 | super(DETAILS_REQ); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void process(ObjectNode payload) { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 140 | String modelId = string(payload, MODEL_ID); |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 141 | String moduleName = string(payload, ID); |
| 142 | String revision = string(payload, REVISION); |
| 143 | YangModule module = getModule(modelId, new DefaultYangModuleId(moduleName, revision)); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 144 | |
| 145 | ObjectNode data = objectNode(); |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 146 | data.put(MODEL_ID, modelId); |
| 147 | data.put(ID, moduleName); |
| 148 | data.put(REVISION, revision); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 149 | |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 150 | if (module != null) { |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 151 | ArrayNode source = arrayNode(); |
| 152 | data.set(SOURCE, source); |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 153 | addSource(source, getSource(modelId, module)); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 154 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 155 | |
| 156 | ObjectNode rootNode = objectNode(); |
| 157 | rootNode.set(DETAILS, data); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 158 | sendMessage(DETAILS_RESP, rootNode); |
| 159 | } |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 160 | |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 161 | // FIXME: Hack to properly resolve the YANG source resource |
| 162 | private InputStream getSource(String modelId, YangModule module) { |
| 163 | try { |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 164 | return module.getYangSource(); // trigger exception |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 165 | } catch (ModelException e) { |
| 166 | // Strip the YANG source file base-name and then use it to access |
| 167 | // the corresponding resource in the correct run-time context. |
| 168 | String msg = e.getMessage(); |
| 169 | int i = msg.lastIndexOf('/'); |
| 170 | String baseName = i > 0 ? msg.substring(i) : msg; |
| 171 | ClassLoader loader = classLoaderRegistry.getClassLoader(modelId); |
| 172 | return loader == null ? null : |
| 173 | loader.getResourceAsStream("/yang/resources" + baseName); |
| 174 | } |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 177 | private void addSource(ArrayNode source, InputStream yangSource) { |
| 178 | try (InputStreamReader isr = new InputStreamReader(yangSource); |
| 179 | BufferedReader br = new BufferedReader(isr)) { |
| 180 | String line; |
| 181 | while ((line = br.readLine()) != null) { |
| 182 | source.add(line); |
| 183 | } |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 184 | } catch (IOException e) { |
| 185 | log.warn("Unable to read YANG source", e); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 188 | } |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 189 | |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 190 | private YangModule getModule(String modelId, DefaultYangModuleId moduleId) { |
| 191 | YangModel model = modelRegistry.getModel(modelId); |
| 192 | return model != null ? model.getYangModule(moduleId) : null; |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 193 | } |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 194 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 195 | } |