blob: 3be5dd226e96517c8861ec38bda59bac31774083 [file] [log] [blame]
Simon Huntcc035c52017-02-22 21:12:51 -08001/*
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 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 Vachuska04059f92017-03-07 15:16:23 -080028import org.onosproject.yang.model.YangModel;
Thomas Vachuskae0792f12017-03-31 00:15:06 -070029import org.onosproject.yang.model.YangModule;
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070030import org.onosproject.yang.model.YangModuleId;
Thomas Vachuska04059f92017-03-07 15:16:23 -080031import org.onosproject.yang.runtime.YangModelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Thomas Vachuskae0792f12017-03-31 00:15:06 -070035import java.io.BufferedReader;
36import java.io.IOException;
37import java.io.InputStream;
38import java.io.InputStreamReader;
39import java.net.URLEncoder;
Simon Huntcc035c52017-02-22 21:12:51 -080040import java.util.Collection;
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070041import java.util.Set;
Simon Huntcc035c52017-02-22 21:12:51 -080042
43/**
Thomas Vachuska04059f92017-03-07 15:16:23 -080044 * ONOS UI YANG Models message handler.
Simon Huntcc035c52017-02-22 21:12:51 -080045 */
46public class YangModelMessageHandler extends UiMessageHandler {
47
48 private static final String TABLE_REQ = "yangModelDataRequest";
49 private static final String TABLE_RESP = "yangModelDataResponse";
Simon Hunt7d5e9842017-02-23 11:37:02 -080050 private static final String MODELS = "yangModels";
Simon Huntcc035c52017-02-22 21:12:51 -080051
52 private static final String DETAILS_REQ = "yangModelDetailsRequest";
53 private static final String DETAILS_RESP = "yangModelDetailsResponse";
54 private static final String DETAILS = "details";
55
56 // Table Column IDs
57 private static final String ID = "id";
Thomas Vachuskae0792f12017-03-31 00:15:06 -070058 private static final String MODULE = "module";
59 private static final String REVISION = "revision";
Simon Huntcc035c52017-02-22 21:12:51 -080060 // TODO: fill out table columns as needed
61
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 Vachuskae0792f12017-03-31 00:15:06 -070065 ID, MODULE, REVISION
Simon Huntcc035c52017-02-22 21:12:51 -080066 };
67
Thomas Vachuskae0792f12017-03-31 00:15:06 -070068 private static final String UTF8 = "UTF-8";
69
Simon Huntcc035c52017-02-22 21:12:51 -080070 private final Logger log = LoggerFactory.getLogger(getClass());
71
Thomas Vachuska04059f92017-03-07 15:16:23 -080072 private YangModelRegistry modelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080073 // TODO: fill out other fields as necessary
74
75
76 // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-===========
77
78 @Override
79 public void init(UiConnection connection, ServiceDirectory directory) {
80 super.init(connection, directory);
Thomas Vachuska04059f92017-03-07 15:16:23 -080081 modelRegistry = directory.get(YangModelRegistry.class);
Simon Huntcc035c52017-02-22 21:12:51 -080082 }
83
84 @Override
85 public void destroy() {
Simon Huntcc035c52017-02-22 21:12:51 -080086 super.destroy();
Simon Huntcc035c52017-02-22 21:12:51 -080087 }
88
89 @Override
90 protected Collection<RequestHandler> createRequestHandlers() {
91 return ImmutableSet.of(
Simon Hunt7d5e9842017-02-23 11:37:02 -080092 new TableDataHandler(),
93 new DetailRequestHandler()
Simon Huntcc035c52017-02-22 21:12:51 -080094 );
95 }
96
Simon Huntcc035c52017-02-22 21:12:51 -080097 // Handler for table requests
98 private final class TableDataHandler extends TableRequestHandler {
99 private static final String NO_ROWS_MESSAGE = "No YANG Models found";
100
101 private TableDataHandler() {
102 super(TABLE_REQ, TABLE_RESP, MODELS);
103 }
104
105 @Override
106 protected String[] getColumnIds() {
107 return COL_IDS;
108 }
109
110 @Override
111 protected String noRowsMessage(ObjectNode payload) {
112 return NO_ROWS_MESSAGE;
113 }
114
115 @Override
116 protected void populateTable(TableModel tm, ObjectNode payload) {
Thomas Vachuska04059f92017-03-07 15:16:23 -0800117 for (YangModel model : modelRegistry.getModels()) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700118 for (YangModuleId id : model.getYangModulesId()) {
119 populateRow(tm.addRow(), model.hashCode(), id);
120 }
Simon Huntcc035c52017-02-22 21:12:51 -0800121 }
122 }
123
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700124 private void populateRow(TableModel.Row row, int modelId, YangModuleId moduleId) {
125 row.cell(ID, "YM" + modelId)
126 .cell(MODULE, moduleId.moduleName())
127 .cell(REVISION, moduleId.revision());
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) {
140 String id = string(payload, ID);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700141 String name = string(payload, MODULE);
142 YangModule module = getModule(id, name);
Simon Huntcc035c52017-02-22 21:12:51 -0800143
144 ObjectNode data = objectNode();
Simon Huntcc035c52017-02-22 21:12:51 -0800145 data.put(ID, id);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700146 if (module != null) {
147 data.put(MODULE, module.getYangModuleId().moduleName());
148 data.put(REVISION, module.getYangModuleId().revision());
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700149
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700150 ArrayNode source = arrayNode();
151 data.set(SOURCE, source);
152
153 addSource(source, module.getYangSource());
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
161 private void addSource(ArrayNode source, InputStream yangSource) {
162 try (InputStreamReader isr = new InputStreamReader(yangSource);
163 BufferedReader br = new BufferedReader(isr)) {
164 String line;
165 while ((line = br.readLine()) != null) {
166 source.add(line);
167 }
168
169 } catch (IOException e) {
170 log.warn("Unable to read YANG source", e);
171 return;
172 }
173 }
Simon Huntcc035c52017-02-22 21:12:51 -0800174 }
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700175
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700176
177 private YangModule getModule(String id, String name) {
178 int nid = Integer.parseInt(id.substring(2));
179 log.info("Got {}; {}", id, nid);
180 YangModel model = modelRegistry.getModels().stream()
181 .filter(m -> m.hashCode() == nid)
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700182 .findFirst().orElse(null);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700183 if (model != null) {
184 log.info("Got model");
185 return model.getYangModules().stream()
186 .filter(m -> m.getYangModuleId().moduleName().contentEquals(name))
187 .findFirst().orElse(null);
188 }
189 return null;
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700190 }
Simon Huntcc035c52017-02-22 21:12:51 -0800191}