blob: 31ff7b0ef247aeb197f4922fdeef3dc2384e5622 [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;
Simon Huntcc035c52017-02-22 21:12:51 -080039import java.util.Collection;
40
41/**
Thomas Vachuska04059f92017-03-07 15:16:23 -080042 * ONOS UI YANG Models message handler.
Simon Huntcc035c52017-02-22 21:12:51 -080043 */
44public class YangModelMessageHandler extends UiMessageHandler {
45
46 private static final String TABLE_REQ = "yangModelDataRequest";
47 private static final String TABLE_RESP = "yangModelDataResponse";
Simon Hunt7d5e9842017-02-23 11:37:02 -080048 private static final String MODELS = "yangModels";
Simon Huntcc035c52017-02-22 21:12:51 -080049
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 Vachuskae0792f12017-03-31 00:15:06 -070056 private static final String REVISION = "revision";
Thomas Vachuska401a1d32017-03-31 09:58:11 -070057 private static final String MODEL_ID = "modelId";
Simon Huntcc035c52017-02-22 21:12:51 -080058 // TODO: fill out table columns as needed
59
Thomas Vachuskae0792f12017-03-31 00:15:06 -070060 private static final String SOURCE = "source";
61
Simon Huntcc035c52017-02-22 21:12:51 -080062 private static final String[] COL_IDS = {
Thomas Vachuska401a1d32017-03-31 09:58:11 -070063 ID, REVISION, MODEL_ID
Simon Huntcc035c52017-02-22 21:12:51 -080064 };
65
Thomas Vachuskae0792f12017-03-31 00:15:06 -070066 private static final String UTF8 = "UTF-8";
67
Simon Huntcc035c52017-02-22 21:12:51 -080068 private final Logger log = LoggerFactory.getLogger(getClass());
69
Thomas Vachuska04059f92017-03-07 15:16:23 -080070 private YangModelRegistry modelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080071 // 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 Vachuska04059f92017-03-07 15:16:23 -080079 modelRegistry = directory.get(YangModelRegistry.class);
Simon Huntcc035c52017-02-22 21:12:51 -080080 }
81
82 @Override
83 public void destroy() {
Simon Huntcc035c52017-02-22 21:12:51 -080084 super.destroy();
Simon Huntcc035c52017-02-22 21:12:51 -080085 }
86
87 @Override
88 protected Collection<RequestHandler> createRequestHandlers() {
89 return ImmutableSet.of(
Simon Hunt7d5e9842017-02-23 11:37:02 -080090 new TableDataHandler(),
91 new DetailRequestHandler()
Simon Huntcc035c52017-02-22 21:12:51 -080092 );
93 }
94
Simon Huntcc035c52017-02-22 21:12:51 -080095 // 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 Vachuska04059f92017-03-07 15:16:23 -0800115 for (YangModel model : modelRegistry.getModels()) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700116 for (YangModuleId id : model.getYangModulesId()) {
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700117 populateRow(tm.addRow(), modelId(model), id);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700118 }
Simon Huntcc035c52017-02-22 21:12:51 -0800119 }
120 }
121
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700122 private void populateRow(TableModel.Row row, String modelId, YangModuleId moduleId) {
123 row.cell(ID, moduleId.moduleName())
Thomas Vachuska770ef842017-03-31 00:15:49 -0700124 .cell(REVISION, moduleId.revision())
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700125 .cell(MODEL_ID, modelId);
Simon Huntcc035c52017-02-22 21:12:51 -0800126 }
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 Vachuska401a1d32017-03-31 09:58:11 -0700138 String name = string(payload, ID);
139 String modelId = string(payload, MODEL_ID);
140 YangModule module = getModule(modelId, name);
Simon Huntcc035c52017-02-22 21:12:51 -0800141
142 ObjectNode data = objectNode();
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700143 data.put(ID, name);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700144 if (module != null) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700145 data.put(REVISION, module.getYangModuleId().revision());
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700146 data.put(MODEL_ID, modelId);
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700147
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700148 ArrayNode source = arrayNode();
149 data.set(SOURCE, source);
150
151 addSource(source, module.getYangSource());
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700152 }
Simon Huntcc035c52017-02-22 21:12:51 -0800153
154 ObjectNode rootNode = objectNode();
155 rootNode.set(DETAILS, data);
Simon Huntcc035c52017-02-22 21:12:51 -0800156 sendMessage(DETAILS_RESP, rootNode);
157 }
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700158
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 Huntcc035c52017-02-22 21:12:51 -0800172 }
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700173
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700174
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700175 private YangModule getModule(String modelId, String name) {
176 int nid = Integer.parseInt(modelId.substring(2));
177 log.info("Got {}; {}", modelId, nid);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700178 YangModel model = modelRegistry.getModels().stream()
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700179 .filter(m -> modelId(m).equals(modelId))
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700180 .findFirst().orElse(null);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700181 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 Vachuskacde197c2017-03-23 17:51:08 -0700188 }
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700189
190 private String modelId(YangModel m) {
191 return "YM" + Math.abs(m.hashCode());
192 }
Simon Huntcc035c52017-02-22 21:12:51 -0800193}