blob: 9b90d5e6e741b9e3095f3e0dd431fb3aa8f7a25c [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
Thomas Vachuskae0792f12017-03-31 00:15:06 -070059 private static final String SOURCE = "source";
60
Simon Huntcc035c52017-02-22 21:12:51 -080061 private static final String[] COL_IDS = {
Thomas Vachuska401a1d32017-03-31 09:58:11 -070062 ID, REVISION, MODEL_ID
Simon Huntcc035c52017-02-22 21:12:51 -080063 };
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
Thomas Vachuska04059f92017-03-07 15:16:23 -080067 private YangModelRegistry modelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080068
69
70 // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-===========
71
72 @Override
73 public void init(UiConnection connection, ServiceDirectory directory) {
74 super.init(connection, directory);
Thomas Vachuska04059f92017-03-07 15:16:23 -080075 modelRegistry = directory.get(YangModelRegistry.class);
Simon Huntcc035c52017-02-22 21:12:51 -080076 }
77
78 @Override
79 public void destroy() {
Simon Huntcc035c52017-02-22 21:12:51 -080080 super.destroy();
Simon Huntcc035c52017-02-22 21:12:51 -080081 }
82
83 @Override
84 protected Collection<RequestHandler> createRequestHandlers() {
85 return ImmutableSet.of(
Simon Hunt7d5e9842017-02-23 11:37:02 -080086 new TableDataHandler(),
87 new DetailRequestHandler()
Simon Huntcc035c52017-02-22 21:12:51 -080088 );
89 }
90
Simon Huntcc035c52017-02-22 21:12:51 -080091 // Handler for table requests
92 private final class TableDataHandler extends TableRequestHandler {
93 private static final String NO_ROWS_MESSAGE = "No YANG Models found";
94
95 private TableDataHandler() {
96 super(TABLE_REQ, TABLE_RESP, MODELS);
97 }
98
99 @Override
100 protected String[] getColumnIds() {
101 return COL_IDS;
102 }
103
104 @Override
105 protected String noRowsMessage(ObjectNode payload) {
106 return NO_ROWS_MESSAGE;
107 }
108
109 @Override
110 protected void populateTable(TableModel tm, ObjectNode payload) {
Thomas Vachuska04059f92017-03-07 15:16:23 -0800111 for (YangModel model : modelRegistry.getModels()) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700112 for (YangModuleId id : model.getYangModulesId()) {
Thomas Vachuska89534452017-07-28 10:20:12 -0700113 populateRow(tm.addRow(), model.getYangModelId(), id);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700114 }
Simon Huntcc035c52017-02-22 21:12:51 -0800115 }
116 }
117
Simon Hunt3ee1f432017-03-31 17:18:53 -0700118 private void populateRow(TableModel.Row row, String modelId,
119 YangModuleId moduleId) {
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700120 row.cell(ID, moduleId.moduleName())
Thomas Vachuska770ef842017-03-31 00:15:49 -0700121 .cell(REVISION, moduleId.revision())
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700122 .cell(MODEL_ID, modelId);
Simon Huntcc035c52017-02-22 21:12:51 -0800123 }
124 }
125
126
127 // handler for selected model detail requests (selected table row)
128 private final class DetailRequestHandler extends RequestHandler {
129 private DetailRequestHandler() {
130 super(DETAILS_REQ);
131 }
132
133 @Override
134 public void process(ObjectNode payload) {
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700135 String name = string(payload, ID);
136 String modelId = string(payload, MODEL_ID);
137 YangModule module = getModule(modelId, name);
Simon Huntcc035c52017-02-22 21:12:51 -0800138
139 ObjectNode data = objectNode();
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700140 data.put(ID, name);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700141 if (module != null) {
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700142 data.put(REVISION, module.getYangModuleId().revision());
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700143 data.put(MODEL_ID, modelId);
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700144
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700145 ArrayNode source = arrayNode();
146 data.set(SOURCE, source);
147
148 addSource(source, module.getYangSource());
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700149 }
Simon Huntcc035c52017-02-22 21:12:51 -0800150
151 ObjectNode rootNode = objectNode();
152 rootNode.set(DETAILS, data);
Simon Huntcc035c52017-02-22 21:12:51 -0800153 sendMessage(DETAILS_RESP, rootNode);
154 }
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700155
156 private void addSource(ArrayNode source, InputStream yangSource) {
157 try (InputStreamReader isr = new InputStreamReader(yangSource);
158 BufferedReader br = new BufferedReader(isr)) {
159 String line;
160 while ((line = br.readLine()) != null) {
161 source.add(line);
162 }
163
164 } catch (IOException e) {
165 log.warn("Unable to read YANG source", e);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700166 }
167 }
Simon Huntcc035c52017-02-22 21:12:51 -0800168 }
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700169
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700170
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700171 private YangModule getModule(String modelId, String name) {
172 int nid = Integer.parseInt(modelId.substring(2));
173 log.info("Got {}; {}", modelId, nid);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700174 YangModel model = modelRegistry.getModels().stream()
Thomas Vachuska89534452017-07-28 10:20:12 -0700175 .filter(m -> m.getYangModelId().equals(modelId))
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700176 .findFirst().orElse(null);
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700177 if (model != null) {
178 log.info("Got model");
179 return model.getYangModules().stream()
180 .filter(m -> m.getYangModuleId().moduleName().contentEquals(name))
181 .findFirst().orElse(null);
182 }
183 return null;
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700184 }
Thomas Vachuska401a1d32017-03-31 09:58:11 -0700185
Simon Huntcc035c52017-02-22 21:12:51 -0800186}