rubyang 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+
3
+ require_relative 'restapi/httpd.rb'
4
+
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+
3
+ require 'sinatra'
4
+ require 'sinatra/contrib'
5
+ require 'sinatra/reloader'
6
+ require 'json'
7
+
8
+ require_relative '../../rubyang'
9
+ db = Rubyang::Database.new
10
+ yang_str = <<-EOB
11
+ module module1 {
12
+ namespace "http://module1/";
13
+ prefix module1;
14
+ container container1 {
15
+ container container2 {
16
+ leaf leaf1 { type string; }
17
+ }
18
+ }
19
+ }
20
+ EOB
21
+ db.load_model Rubyang::Model::Parser.parse( yang_str )
22
+ config = db.configure
23
+ config.edit( 'container1' ).edit( 'container2' ).edit( 'leaf1' ).set( 'aaa' )
24
+
25
+
26
+ get '/' do
27
+ respond_to do |f|
28
+ f.on( 'application/xml' ){
29
+ config = db.configure
30
+ config.to_xml( pretty: true )
31
+ }
32
+ end
33
+ end
34
+
35
+ get %r{/api(?:/(.+)[/]?)} do
36
+ respond_to do |f|
37
+ f.on( 'application/xml' ){
38
+ #{ params: ' '+params[:captures][0] }.to_json
39
+ config = db.configure
40
+ paths = params[:captures][0].split( '/' )
41
+ element = paths.inject( config ){ |parent, child| parent.edit( child ) }
42
+ element.to_xml( pretty: true )
43
+ }
44
+ end
45
+ end
46
+
47
+ post %r{/api(?:/(.+)[/]?)} do
48
+ body = request.body.read
49
+ respond_to do |f|
50
+ f.on( 'application/xml' ){
51
+ config = db.configure
52
+ paths = params[:captures][0].split( '/' )
53
+ element = paths.inject( config ){ |parent, child| parent.edit( child ) }
54
+ }
55
+ end
56
+ end
57
+
58
+ get %r{/api[/]?} do
59
+ respond_to do |f|
60
+ f.on( 'application/xml' ){ db.configure.to_xml( pretty: true ) }
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Rubyang
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+
3
+ require_relative 'webui/app'
4
+
5
+ module Rubyang
6
+ module WebUI
7
+ end
8
+ end
9
+
10
+ Rubyang::WebUI::App.run!
11
+
@@ -0,0 +1,74 @@
1
+ # coding: utf-8
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'sinatra/base'
6
+ require 'sinatra/reloader'
7
+ require 'sinatra/content_for'
8
+
9
+ require_relative '../../rubyang'
10
+ require_relative 'make_json_schema'
11
+
12
+ module Rubyang
13
+ module WebUI
14
+ class App < Sinatra::Base
15
+ set :environment, :development
16
+ set :bind, '0.0.0.0'
17
+
18
+ helpers Sinatra::ContentFor
19
+
20
+ configure :development do
21
+ register Sinatra::Reloader
22
+ end
23
+
24
+
25
+ target_yang = File.expand_path( File.dirname( __FILE__ ) ) + '/target.yang'
26
+ model = Rubyang::Model::Parser.parse( File.open( target_yang, 'r' ).read )
27
+ db = Rubyang::Database.new
28
+ db.load_model model
29
+
30
+
31
+ get '/' do
32
+ locals = Hash.new
33
+ erb :index, locals: locals
34
+ end
35
+
36
+ get '/api/reload_model' do
37
+ target_yang = File.expand_path( File.dirname( __FILE__ ) ) + '/target.yang'
38
+ model = Rubyang::Model::Parser.parse( File.open( target_yang, 'r' ).read )
39
+ db = Rubyang::Database.new
40
+ db.load_model @model
41
+ '{}'
42
+ end
43
+
44
+ get '/api/schema' do
45
+ schema_tree = db.configure.schema
46
+ data = make_json_schema( schema_tree )
47
+ puts data
48
+ sleep 0
49
+ data
50
+ end
51
+
52
+ get '/api/data' do
53
+ config = db.configure
54
+ data = config.to_json( pretty: true )
55
+ puts data
56
+ sleep 0
57
+ data
58
+ end
59
+
60
+ post '/api/data' do
61
+ request.body.rewind
62
+ data = request.body.read
63
+ puts data
64
+ db.configure.load_override_json( data )
65
+ sleep 1
66
+ '{}'
67
+ end
68
+
69
+ run! if app_file == $0
70
+ end
71
+ end
72
+ end
73
+
74
+
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+
3
+ def make_json_schema schema_tree
4
+ json_schema_tree = Array.new
5
+ make_json_schema_recursive json_schema_tree, schema_tree
6
+ return JSON.pretty_generate(json_schema_tree)
7
+ end
8
+
9
+ def make_json_schema_recursive json_schema_tree, schema_tree
10
+ case schema_tree.model
11
+ when nil
12
+ json_schema_tree.push Hash.new
13
+ json_schema_tree.last[:root] = Hash.new
14
+ json_schema_tree.last[:root][:name] = "root"
15
+ json_schema_tree.last[:root][:description] = "root"
16
+ json_schema_tree.last[:root][:children] = Array.new
17
+ schema_tree.children.each{ |child_schema_tree|
18
+ make_json_schema_recursive json_schema_tree.last[:root][:children], child_schema_tree
19
+ }
20
+ when Rubyang::Model::Container
21
+ json_schema_tree.push Hash.new
22
+ json_schema_tree.last[:container] = Hash.new
23
+ json_schema_tree.last[:container][:name] = schema_tree.model.arg
24
+ json_schema_tree.last[:container][:description] = schema_tree.model.substmt('description').first.arg
25
+ json_schema_tree.last[:container][:children] = Array.new
26
+ schema_tree.children.each{ |child_schema_tree|
27
+ make_json_schema_recursive json_schema_tree.last[:container][:children], child_schema_tree
28
+ }
29
+ when Rubyang::Model::List
30
+ json_schema_tree.push Hash.new
31
+ json_schema_tree.last[:list] = Hash.new
32
+ json_schema_tree.last[:list][:key] = schema_tree.model.substmt('key')[0].arg
33
+ json_schema_tree.last[:list][:name] = schema_tree.model.arg
34
+ json_schema_tree.last[:list][:description] = schema_tree.model.substmt('description').first.arg
35
+ json_schema_tree.last[:list][:children] = Array.new
36
+ schema_tree.children.each{ |child_schema_tree|
37
+ make_json_schema_recursive json_schema_tree.last[:list][:children], child_schema_tree
38
+ }
39
+ when Rubyang::Model::Leaf
40
+ json_schema_tree.push Hash.new
41
+ json_schema_tree.last[:leaf] = Hash.new
42
+ json_schema_tree.last[:leaf][:name] = schema_tree.model.arg
43
+ json_schema_tree.last[:leaf][:description] = schema_tree.model.substmt('description').first.arg
44
+ json_schema_tree.last[:leaf][:type] = schema_tree.type.arg
45
+ end
46
+ end
47
+
48
+
49
+ if __FILE__ == $0
50
+ require 'yaml'
51
+ require_relative '../../rubyang'
52
+
53
+ target_yang = File.expand_path( File.dirname( __FILE__ ) ) + '/target.yang'
54
+
55
+ model = Rubyang::Model::Parser.parse( File.open( target_yang, 'r' ).read )
56
+ db = Rubyang::Database.new
57
+ db.load_model model
58
+
59
+ schema_tree = db.configure.schema
60
+
61
+ json = make_json_schema schema_tree
62
+ puts json
63
+ end
@@ -0,0 +1,326 @@
1
+ // Global Variables
2
+ id_index = -1;
3
+ list_schema_tree = {};
4
+
5
+ function next_id(){
6
+ id_index = id_index + 1;
7
+ var next_id = 'node' + id_index;
8
+ return next_id;
9
+ }
10
+
11
+ function make_form(trees, parent_id){
12
+ for(var i=0; i<trees.length; i++){
13
+ var stmt = Object.keys(trees[i])[0];
14
+ var tree = trees[i][stmt];
15
+ if(stmt == "root"){
16
+ var children = tree["children"];
17
+ make_form(children, parent_id);
18
+ }else if(stmt == "container"){
19
+ var child_schema_path = parent_id + '/' + tree["name"];
20
+ var child_id = next_id();
21
+ addcontainer(parent_id, child_id, child_schema_path, tree["name"], tree["description"], '');
22
+ var children = tree["children"];
23
+ make_form(children, child_id);
24
+ }else if(stmt == "list"){
25
+ var child_schema_path = parent_id + '/' + tree["name"];
26
+ var child_id = next_id();
27
+ addlist(parent_id, child_id, child_schema_path, tree["name"], tree["description"], '');
28
+ var children = tree["children"];
29
+ list_schema_tree[child_id] = children;
30
+ }else if(stmt == "leaf"){
31
+ var child_schema_path = parent_id + '/' + tree["name"];
32
+ var child_id = next_id();
33
+ addleaf(parent_id, child_id, child_schema_path, tree["name"], tree["description"], 'string', '');
34
+ }
35
+ }
36
+ }
37
+
38
+ function addcontainer(parent_id, id, schemapath, schemaname, description, options){
39
+ var panel = $('<div>')
40
+ var panelheading = $('<div>')
41
+ var panelbody = $('<div>')
42
+ var container = $('<div>');
43
+
44
+ panel.addClass('panel');
45
+ panel.addClass('panel-default');
46
+
47
+ panelheading.addClass('panel-heading');
48
+ panelheading.html(description);
49
+
50
+ panelbody.addClass('panel-body');
51
+
52
+ container.attr('id',id);
53
+ container.addClass('schemanode');
54
+ container.attr('data-schemanodetype','container');
55
+ container.attr('data-schemapath',schemapath);
56
+ container.attr('data-schemaname',schemaname);
57
+
58
+ $('#'+parent_id).append(panel);
59
+ panel.append(panelheading);
60
+ panel.append(panelbody);
61
+ panelbody.append(container);
62
+ }
63
+
64
+ function addlist(parent_id, id, schemapath, schemaname, description, options){
65
+ var panel = $('<div>')
66
+ var panelheading = $('<div>')
67
+ var panelbody = $('<div>')
68
+ var list = $('<div>');
69
+ var button = $('<button>');
70
+ var panelfooter = $('<div>')
71
+
72
+ panel.addClass('panel');
73
+ panel.addClass('panel-default');
74
+
75
+ panelheading.addClass('panel-heading');
76
+ panelheading.html(description);
77
+
78
+ panelbody.addClass('panel-body');
79
+
80
+ list.attr('id',id);
81
+ list.addClass('schemanode');
82
+ list.attr('data-schemanodetype','list');
83
+ list.attr('data-schemapath',schemapath);
84
+ list.attr('data-schemaname',schemaname);
85
+
86
+ panelfooter.addClass('panel-footer');
87
+
88
+ button.addClass('btn');
89
+ button.addClass('btn-success');
90
+ button.addClass('button-ok');
91
+ button.attr('type','button');
92
+ button.on('click',function(){
93
+ var child_id = next_id();
94
+ addlistelement(id,child_id,'');
95
+ make_form(list_schema_tree[id],child_id);
96
+ });
97
+ button.html('Add');
98
+
99
+ $('#'+parent_id).append(panel);
100
+ panel.append(panelheading);
101
+ panel.append(panelbody);
102
+ panelbody.append(list);
103
+ panel.append(panelfooter);
104
+ panelfooter.append(button);
105
+ }
106
+
107
+ function addlistelement(parent_id, id, options){
108
+ var panel = $('<div>')
109
+ var panelbody = $('<div>')
110
+ var listelement = $('<div>');
111
+ var button = $('<button>');
112
+ var panelfooter = $('<div>')
113
+
114
+ panel.addClass('panel');
115
+ panel.addClass('panel-default');
116
+
117
+ panelbody.addClass('panel-body');
118
+
119
+ listelement.attr('id',id);
120
+ listelement.addClass('schemanode');
121
+ listelement.attr('data-schemanodetype','listelement');
122
+
123
+ panelfooter.addClass('panel-footer');
124
+
125
+ button.addClass('btn');
126
+ button.addClass('btn-success');
127
+ button.addClass('button-ok');
128
+ button.attr('type','button');
129
+ button.on('click',function(){
130
+ $('#'+id).parent().parent().fadeOut('slow', function(){
131
+ $(this).remove();
132
+ });
133
+ });
134
+ button.html('Del');
135
+
136
+ $('#'+parent_id).append(panel);
137
+ panel.append(panelbody);
138
+ panelbody.append(listelement);
139
+ panel.append(panelfooter);
140
+ panelfooter.append(button);
141
+ }
142
+
143
+ function addleaflist(parent_id, id, schemapath, schemaname, description, options){
144
+ var list = $('<div>');
145
+ var h4 = $('<h4>');
146
+ var div = $('<div>');
147
+ var table = $('<table>');
148
+ var thead = $('<thead>');
149
+ var theadtr = $('<tr>');
150
+ var theadth0 = $('<th>');
151
+ var theadth1 = $('<th>');
152
+ var tbody = $('<thead>');
153
+ var tbodytr = $('<tr>');
154
+ var tbodytd0 = $('<td>');
155
+ var tbodytd1 = $('<td>');
156
+ list.attr('id','node4');
157
+ list.addClass('schemanode');
158
+ list.addClass('form-group');
159
+ list.attr('data-schemanodetype','list');
160
+ list.attr('data-schemapath','/container1/list');
161
+ list.attr('data-schemaname','list');
162
+ h4.html('List list');
163
+ list.append(h4);
164
+ container1.append(list);
165
+
166
+ table.addClass('table');
167
+ table.addClass('table-striped');
168
+ table.addClass('table-hover');
169
+ theadth0.html('col0');
170
+ theadth1.html('col1');
171
+ tbodytd0.html('body0');
172
+ tbodytd1.html('body1');
173
+ table.append(thead);
174
+ thead.append(theadtr);
175
+ theadtr.append(theadth0);
176
+ theadtr.append(theadth1);
177
+ table.append(tbody);
178
+ tbody.append(tbodytr);
179
+ tbodytr.append(tbodytd0);
180
+ tbodytr.append(tbodytd1);
181
+ div.append(table);
182
+ list.append(div);
183
+ }
184
+
185
+ function addleaf(parent_id, id, schemapath, schemaname, description, datatype, options){
186
+ var panel = $('<div>');
187
+ var panelbody = $('<div>');
188
+ var leaf = $('<div>');
189
+ var form = $('<form>')
190
+ var formgroup = $('<div>')
191
+ var label = $('<label>');
192
+ var input;
193
+
194
+ var id_form = id + 'form';
195
+
196
+ leaf.attr('id',id);
197
+ leaf.addClass('schemanode');
198
+ leaf.attr('data-schemanodetype','leaf');
199
+ leaf.attr('data-schemapath',schemapath);
200
+ leaf.attr('data-schemaname',schemaname);
201
+
202
+ formgroup.addClass('form-group');
203
+
204
+ label.attr('for', id_form);
205
+ label.html(description);
206
+
207
+ if(datatype == "string"){
208
+ input = $('<input>');
209
+ input.attr('id',id_form);
210
+ input.addClass('form-control');
211
+ input.attr('type', 'text');
212
+ }else if(datatype == "enumration"){
213
+ input = $('<select>');
214
+ input.attr('id',id_form);
215
+ input.addClass('form-control');
216
+ var option0 = $('<option>');
217
+ var option1 = $('<option>');
218
+ var option2 = $('<option>');
219
+ option1.html('1');
220
+ option2.html('2');
221
+ input.append(option0);
222
+ input.append(option1);
223
+ input.append(option2);
224
+ }else{
225
+ }
226
+
227
+ $('#'+parent_id).append(panel);
228
+ panel.append(panelbody);
229
+ panelbody.append(leaf);
230
+ leaf.append(form);
231
+ form.append(formgroup);
232
+ formgroup.append(label);
233
+ formgroup.append(input);
234
+ }
235
+
236
+ function set_config(trees, id){
237
+ console.log('set_config start');
238
+ Object.keys(trees).forEach(function(key){
239
+ console.log(key);
240
+ if(key == 'config'){
241
+ console.log('in config');
242
+ set_config(trees[key], id);
243
+ }else{
244
+ console.log('in else');
245
+ $('#'+id + ">>>" + ".schemanode").each(function(){
246
+ if($(this).data('schemaname') == key){
247
+ var schemanodetype = $(this).data('schemanodetype');
248
+ if(schemanodetype == 'container'){
249
+ console.log('container');
250
+ set_config(trees[key], this.id);
251
+ }else if(schemanodetype == 'list'){
252
+ console.log('list');
253
+ for(var i=0; i<trees[key].length; i++){
254
+ var child_id = next_id();
255
+ addlistelement(this.id, child_id, '');
256
+ make_form(list_schema_tree[this.id],child_id);
257
+ set_config(trees[key][i], child_id);
258
+ }
259
+ }else if(schemanodetype == 'leaf'){
260
+ console.log('leaf');
261
+ $(this).find("input").val(trees[key]);
262
+ }
263
+ }
264
+ });
265
+ }
266
+ });
267
+ }
268
+
269
+ function get_config(id){
270
+ var data = {};
271
+ if($('#'+id).data('schemanodetype') == "leaf"){
272
+ var value = $('#'+id).find("input").val();
273
+ data[$('#'+id).data('schemaname')] = value;
274
+ }else if($('#'+id).data('schemanodetype') == "container"){
275
+ var tmp = {};
276
+ $('#'+id + ">>>" + ".schemanode").each(function(){
277
+ $.each(get_config(this.id),function(key,value){
278
+ tmp[key] = value;
279
+ });
280
+ });
281
+ data[$('#'+id).data('schemaname')] = tmp;
282
+ }else if($('#'+id).data('schemanodetype') == "list"){
283
+ var tmp = [];
284
+ $('#'+id + ">>>" + ".schemanode").each(function(){
285
+ tmp.push(get_config(this.id));
286
+ });
287
+ data[$('#'+id).data('schemaname')] = tmp;
288
+ }else{
289
+ var tmp = {};
290
+ $('#'+id + ">>>" + ".schemanode").each(function(){
291
+ $.each(get_config(this.id),function(key,value){
292
+ tmp[key] = value;
293
+ });
294
+ });
295
+ if(id == 'root'){
296
+ data['config'] = tmp;
297
+ }else{
298
+ data = tmp;
299
+ }
300
+ }
301
+ return data;
302
+ }
303
+
304
+ $(".button-ok").on('click',function(){
305
+ if(window.confirm("Are you sure?")){
306
+ $('#myModal').modal();
307
+ var json = JSON.stringify(get_config('root'), " ", 2);
308
+ console.log(json);
309
+ //alert(json);
310
+ $.ajax({
311
+ type: 'POST',
312
+ url: "api/data",
313
+ dataType: 'json',
314
+ data: json,
315
+ timeout: 10000
316
+ }).done(function(data, status, xhr){
317
+ console.log(data);
318
+ alert('success');
319
+ location.reload();
320
+ }).fail(function(xhr, status, error){
321
+ alert('failed to send config');
322
+ $('#myModal').modal('hide');
323
+ }).always(function(){
324
+ });
325
+ }
326
+ });