-
Notifications
You must be signed in to change notification settings - Fork 8
/
yang.js
227 lines (184 loc) · 4.63 KB
/
yang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var levelup = require('level')
var fs = require('fs')
var Promise = require('promise')
var rmdir = require('rimraf')
var yang_parser = require('yang-parser')
var Yang
Yang = (function(db_file) {
function Yang() {}
var db
var ensureDbOpen = function() {
db = levelup(db_file)
}
/*
* check yang module name if matches identifier
* https://tools.ietf.org/html/rfc6020#page-163
*
* client side and pyang will handle yang validation in more detail
*
* @identifier - yang identifier - mostly yang module name
*/
Yang.identifier_valid = function(identifier) {
if (!identifier)
return false
return new RegExp(/([A-z]|'_')+([A-z0-9]|\_|\-|\.)*/).test(identifier)
}
Yang.initial_yang_load = function() {
return new Promise(function(resolve, reject) {
ensureDbOpen()
var dir = "./ietf-yangs"
var files
try {
files = fs.readdirSync(dir)
}
catch (e) {
console.error(e.toString())
return reject("can't read default yang dir")
}
var batch = db.batch()
for (var i = 0, len = files.length; i < len; i++) {
var yang_module_name = files[i]
var yang_module_content
try {
yang_module_content = fs.readFileSync(dir + '/' + yang_module_name, 'utf-8')
} catch (e) {
console.error(e.toString())
console.log("can't read default yang file", yang_module_name)
}
batch = batch.put(yang_module_name, yang_module_content, { "sync": true })
}
batch.write(function(error) {
db.close()
if (error) {
console.error(error)
return reject("unable to save yang module to database")
}
resolve()
})
})
}
Yang.register = function() {
return new Promise(function(resolve, reject) {
try {
fs.statSync(db_file)
}
catch (e) {
console.log('db not found. registering user')
Yang.initial_yang_load().then(resolve, reject)
}
resolve()
})
}
Yang.all_names = function() {
return new Promise(function(resolve, reject) {
ensureDbOpen()
var keys = []
db.createReadStream({
values: false
})
.on('data', function(key) {
keys.push(key)
})
.on('error', function(error) {
console.error("database error when reading yang files")
console.error(error)
db.close()
})
.on('end', function() {
resolve(keys)
})
.on('close', function() {
db.close()
})
})
}
Yang.find = function(yang_module_name) {
if (!Yang.identifier_valid(yang_module_name)) {
return Promise.reject("invalid YANG module name")
}
return new Promise(function(resolve, reject) {
ensureDbOpen()
db.get(yang_module_name, function(error, value) {
db.close()
if (error) {
console.error("unable to get yang module from database")
console.error(error)
return reject(error.toString())
}
try {
return resolve(yang_parser.parse(value))
} catch (e) {
console.error(e)
return reject(e)
}
})
})
}
Yang.update = function(yang_module_name, yang_module_content) {
if (!Yang.identifier_valid(yang_module_name)) {
return Promise.reject("invalid YANG module name")
}
if (!yang_module_content || yang_module_content.length < 10) {
return Promise.reject("invalid YANG module content")
}
return new Promise(function(resolve, reject) {
console.log("saving yang module: " + yang_module_name)
ensureDbOpen()
db.put(yang_module_name, yang_module_content, function(error) {
db.close()
if (error) {
console.error(error)
return reject(res, error)
}
return resolve()
})
})
}
Yang.reset_db = function() {
return new Promise(function(resolve, reject) {
rmdir(db_file, function(error) {
if (error) {
return reject(error)
}
return Yang.initial_yang_load().then(resolve, reject)
})
})
}
Yang.delete = function(name) {
if (!Yang.identifier_valid(name)) {
return Promise.reject("invalid YANG module name")
}
return new Promise(function(resolve, reject) {
ensureDbOpen()
db.del(name, function(error) {
db.close()
if (error) {
console.error("database error when deleting yang files")
console.error(error)
return reject(error)
}
resolve()
})
})
}
return Yang
});
var YangFactory
YangFactory = (function() {
function YangFactory() {}
/*
* function that we use to get 'uniq' user id
*/
function uniq_n(email, userpass_hash) {
return email + "_" + userpass_hash
}
YangFactory.setup = function(leveldb_dir) {
this.leveldb_dir = leveldb_dir
}
YangFactory.createYang = function (email, userpass_hash) {
var user_id = uniq_n(email, userpass_hash)
return Yang(this.leveldb_dir + user_id)
}
return YangFactory
})()
module.exports = YangFactory