blob: 8bf6876452aecba0381ef776785c6800a2c68c0a [file] [log] [blame]
Simon Huntcc035c52017-02-22 21:12:51 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Huntcc035c52017-02-22 21:12:51 -08003 *
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 Vachuska04059f92017-03-07 15:16:23 -080017package org.onosproject.yang.gui;
Simon Huntcc035c52017-02-22 21:12:51 -080018
Thomas Vachuskacde197c2017-03-23 17:51:08 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
Simon Huntcc035c52017-02-22 21:12:51 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.osgi.ServiceDirectory;
23import org.onosproject.ui.RequestHandler;
24import org.onosproject.ui.UiConnection;
25import org.onosproject.ui.UiMessageHandler;
26import org.onosproject.ui.table.TableModel;
27import org.onosproject.ui.table.TableRequestHandler;
Thomas Vachuskac307d212017-07-31 13:57:20 -070028import org.onosproject.yang.YangClassLoaderRegistry;
29import org.onosproject.yang.model.DefaultYangModuleId;
30import org.onosproject.yang.model.ModelException;
Thomas Vachuska04059f92017-03-07 15:16:23 -080031import org.onosproject.yang.model.YangModel;
Thomas Vachuskae0792f12017-03-31 00:15:06 -070032import org.onosproject.yang.model.YangModule;
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070033import org.onosproject.yang.model.YangModuleId;
Thomas Vachuska04059f92017-03-07 15:16:23 -080034import org.onosproject.yang.runtime.YangModelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Thomas Vachuskae0792f12017-03-31 00:15:06 -070038import java.io.BufferedReader;
39import java.io.IOException;
40import java.io.InputStream;
41import java.io.InputStreamReader;
Simon Huntcc035c52017-02-22 21:12:51 -080042import java.util.Collection;
43
44/**
Thomas Vachuska04059f92017-03-07 15:16:23 -080045 * ONOS UI YANG Models message handler.
Simon Huntcc035c52017-02-22 21:12:51 -080046 */
47public class YangModelMessageHandler extends UiMessageHandler {
48
49 private static final String TABLE_REQ = "yangModelDataRequest";
50 private static final String TABLE_RESP = "yangModelDataResponse";
Simon Hunt7d5e9842017-02-23 11:37:02 -080051 private static final String MODELS = "yangModels";
Simon Huntcc035c52017-02-22 21:12:51 -080052
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 Vachuskae0792f12017-03-31 00:15:06 -070059 private static final String REVISION = "revision";
Thomas Vachuska401a1d32017-03-31 09:58:11 -070060 private static final String MODEL_ID = "modelId";
Simon Huntcc035c52017-02-22 21:12:51 -080061
Thomas Vachuskae0792f12017-03-31 00:15:06 -070062 private static final String SOURCE = "source";
63
Simon Huntcc035c52017-02-22 21:12:51 -080064 private static final String[] COL_IDS = {
Thomas Vachuska401a1d32017-03-31 09:58:11 -070065 ID, REVISION, MODEL_ID
Simon Huntcc035c52017-02-22 21:12:51 -080066 };
67
68 private final Logger log = LoggerFactory.getLogger(getClass());
69
Thomas Vachuska04059f92017-03-07 15:16:23 -080070 private YangModelRegistry modelRegistry;
Thomas Vachuskac307d212017-07-31 13:57:20 -070071 private YangClassLoaderRegistry classLoaderRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080072
73
74 // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-===========
75
76 @Override
77 public void init(UiConnection connection, ServiceDirectory directory) {
78 super.init(connection, directory);
Thomas Vachuska04059f92017-03-07 15:16:23 -080079 modelRegistry = directory.get(YangModelRegistry.class);
Thomas Vachuskac307d212017-07-31 13:57:20 -070080 classLoaderRegistry = directory.get(YangClassLoaderRegistry.class);
Simon Huntcc035c52017-02-22 21:12:51 -080081 }
82
83 @Override
84 public void destroy() {
Simon Huntcc035c52017-02-22 21:12:51 -080085 super.destroy();
Simon Huntcc035c52017-02-22 21:12:51 -080086 }
87
88 @Override
89 protected Collection<RequestHandler> createRequestHandlers() {
90 return ImmutableSet.of(
Simon Hunt7d5e9842017-02-23 11:37:02 -080091 new TableDataHandler(),
92 new DetailRequestHandler()
Simon Huntcc035c52017-02-22 21:12:51 -080093 );
94 }
95
Simon Huntcc035c52017-02-22 21:12:51 -080096 // 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 Vachuska04059f92017-03-07 15:16:23 -0800116 for (YangModel model : modelRegistry.getModels()) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700117 for (YangModuleId id : model.getYangModulesId()) {
Thomas Vachuska89534452017-07-28 10:20:12 -0700118 populateRow(tm.addRow(), model.getYangModelId(), id);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700119 }
Simon Huntcc035c52017-02-22 21:12:51 -0800120 }
121 }
122
Simon Hunt3ee1f432017-03-31 17:18:53 -0700123 private void populateRow(TableModel.Row row, String modelId,
124 YangModuleId moduleId) {
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700125 row.cell(ID, moduleId.moduleName())
Thomas Vachuska770ef842017-03-31 00:15:49 -0700126 .cell(REVISION, moduleId.revision())
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700127 .cell(MODEL_ID, modelId);
Simon Huntcc035c52017-02-22 21:12:51 -0800128 }
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 Vachuska401a1d32017-03-31 09:58:11 -0700140 String modelId = string(payload, MODEL_ID);
Thomas Vachuskac307d212017-07-31 13:57:20 -0700141 String moduleName = string(payload, ID);
142 String revision = string(payload, REVISION);
143 YangModule module = getModule(modelId, new DefaultYangModuleId(moduleName, revision));
Simon Huntcc035c52017-02-22 21:12:51 -0800144
145 ObjectNode data = objectNode();
Thomas Vachuskac307d212017-07-31 13:57:20 -0700146 data.put(MODEL_ID, modelId);
147 data.put(ID, moduleName);
148 data.put(REVISION, revision);
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700149
Thomas Vachuskac307d212017-07-31 13:57:20 -0700150 if (module != null) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700151 ArrayNode source = arrayNode();
152 data.set(SOURCE, source);
Thomas Vachuskac307d212017-07-31 13:57:20 -0700153 addSource(source, getSource(modelId, module));
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700154 }
Simon Huntcc035c52017-02-22 21:12:51 -0800155
156 ObjectNode rootNode = objectNode();
157 rootNode.set(DETAILS, data);
Simon Huntcc035c52017-02-22 21:12:51 -0800158 sendMessage(DETAILS_RESP, rootNode);
159 }
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700160
Thomas Vachuskac307d212017-07-31 13:57:20 -0700161 // FIXME: Hack to properly resolve the YANG source resource
162 private InputStream getSource(String modelId, YangModule module) {
163 try {
164 module.getYangSource(); // trigger exception
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 }
175 return null;
176 }
177
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700178 private void addSource(ArrayNode source, InputStream yangSource) {
179 try (InputStreamReader isr = new InputStreamReader(yangSource);
180 BufferedReader br = new BufferedReader(isr)) {
181 String line;
182 while ((line = br.readLine()) != null) {
183 source.add(line);
184 }
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700185 } catch (IOException e) {
186 log.warn("Unable to read YANG source", e);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700187 }
188 }
Simon Huntcc035c52017-02-22 21:12:51 -0800189 }
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700190
Thomas Vachuskac307d212017-07-31 13:57:20 -0700191 private YangModule getModule(String modelId, DefaultYangModuleId moduleId) {
192 YangModel model = modelRegistry.getModel(modelId);
193 return model != null ? model.getYangModule(moduleId) : null;
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700194 }
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700195
Simon Huntcc035c52017-02-22 21:12:51 -0800196}