This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
topar.js
293 lines (263 loc) · 9.86 KB
/
topar.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const fs = require("fs")
const yaml = require('yaml-js')
const pointer = require('json-pointer');
const ToscaSchemas = require("./ToscaSchema.js");
const ToscaTypes = require("./ToscaTypes.js")
const tosca_definitions = 'tosca_definitions';
var _errors=[]
var debug=false
function set_debug(dbg) {
debug = dbg
}
//var _ = require('lodash');
exports=module.exports={};
function load_schemas() {
let schemas = {}
let factory = {}
versions = fs.readdirSync(tosca_definitions);
for (let version of versions) {
schemas[version] = {};
factory[version] = {};
const schema_dir = `${tosca_definitions}/${version}/definitions`;
let files;
try {
files = fs.readdirSync(schema_dir);
} catch(e) {
console.error(`Warning : No schemas found for '${version}'`);
continue;
}
for (let schema_file of files) {
if (schema_file.endsWith(".json")) {
try {
schema = JSON.parse(fs.readFileSync(`${schema_dir}/${schema_file}`, 'UTF-8'));
} catch(e) {
console.error(`Error: '${schema_dir}/${schema_file}' does not contain a valid JSON :`);
console.error(e);
return;
}
try {
for (let name in schema) {
schemas[version][name] = ToscaSchemas.ajv.compile(schema[name].schema);
factory[version][name] = schema[name].factory;
};
} catch(e) {
console.error(e);
}
}
}
}
return [ schemas, factory ];
}
const [ _schemas, _factory ] = load_schemas();
function adaptVals(ast) {
if (ast instanceof yaml.nodes.MappingNode) {
ast.val = {}
for (let assoc of ast.value) {
let [k,v] = assoc
if (k.value in ast.val) {
_errors.push(`Syntax Error: duplicate '${k.value}' key`)
} else {
ast.val[k.value] = adaptVals(v)
}
}
} else if (ast instanceof yaml.nodes.SequenceNode) {
ast.val = ast.value.map(x => adaptVals(x))
} else ast.val = ast.value
return ast
}
function buildYamlAst(input, filename) {
let yamled
try {
yamled = yaml.compose(input);
} catch(e) {
let start = (e.context_mark) ? `${e.context_mark.line + 1}:${e.context_mark.column + 1}` : ''
let end = (e.problem_mark) ? `${e.problem_mark.line + 1}:${e.problem_mark.column + 1}` : ''
let buffer = (e.problem_mark) ? `${e.problem_mark.buffer}` : (e.context_mark) ? `${e.context_mark.buffer}` : ''
let data = (e.context_mark && e.problem_mark) ? buffer.substring(e.context_mark.pointer, e.problem_mark.pointer) : buffer
let errorMsg = `Yaml syntax error: ${e.context}, ${e.problem} (${filename}:${start}->${end} = "${data}")`
_errors.push(errorMsg)
return null
}
let ast = adaptVals(yamled);
return (_errors.length) ? null : ast;
}
function validateToscaSyntax(ast, schema, version, filename) {
let tosca_version = (version) ? version : "tosca_simple_yaml_1_2"
let tosca_schema = (schema) ? schema : "service_template"
schema = _schemas[tosca_version][tosca_schema]
valid = schema(ast)
if (!valid) {
for (let error of schema.errors) {
let dpath = error.dataPath;
srcPath = (dpath.endsWith('/val')) ? dpath.substring(0,dpath.lastIndexOf('/')) : dpath
let data = pointer.get(ast, srcPath)
let start = (data.start_mark) ? `${data.start_mark.line + 1}:${data.start_mark.column + 1}` : ''
let end = (data.end_mark) ? `${data.end_mark.line + 1}:${data.end_mark.column + 1}` : ''
let buffer = (data.end_mark) ? `${data.end_mark.buffer}` : (data.start_mark) ? `${data.start_mark.buffer}` : ''
let txt = (data.start_mark && data.end_mark) ? buffer.substring(data.start_mark.pointer, data.end_mark.pointer) : buffer
dataTxt = (txt.length < 300) ? `"${txt}"` : `"${txt.trim().slice(0, 146)} [ ... ] ${txt.trim().slice(-146)}"`
let keywordMsg = error.message
switch(error.keyword) {
case "additionalProperties":
keywordMsg = `${error.message}, keyword '${error.params.additionalProperty}' is not allowed`
break;
case "dictProperties":
keywordMsg = `error in TOSCA definition`
break;
case "dictIdsDefinition":
keywordMsg = `should be a map of well formed named TOSCA definitions`
break;
}
let errorMsg = `\nTosca syntax error: ${keywordMsg}, \n ${filename}:${start}->${end}, \n ${dataTxt})`
_errors.push(errorMsg)
}
}
return valid
}
function getFromPath(input, path, isin, isnotin) {
if (!input) { return [] }
let data = [ { data: input, keys: {} } ]
if (!path || path == "/" || path =="") {
return data
}
let parts = path.split("/")
for (var i=1, len = parts.length; i < len; i++) {
data = data.map( x => ("val" in x.data) ? { data: x.data.val, keys: x.keys } : "error")
if (data.includes("error")) { return [] }
let part = parts[i]
if (part.startsWith('@')) {
let newdata = []
data.forEach(function(x) {
if (x.data instanceof Object && !(x.data instanceof Array)) {
for (let ele in x.data) {
if ( (!isin || (ele.includes(isin)) ) && ( !isnotin || (!(ele.includes(isnotin))) ) ) {
let newkeys = {}
for (k in x.keys) { newkeys[k] = x.keys[k] }
newkeys[part] = ele
newdata.push({ data: x.data[ele], keys: newkeys })
}
}
} else newdata.push("error")
})
data = newdata
} else {
data = data.map(x => (x.data instanceof Object && !(x.data instanceof Array) && part in x.data)
? { data: x.data[part], keys: x.keys } : "error")
}
if (data.includes("error")) { return [] }
}
return data
}
function toscaFactoryRef(input, ref, tosca_version) {
if (!ref) return null;
debugger
let data = toscaFactory(input.data, _factory[tosca_version][ref], tosca_version)
return (data.length == 1) ? { data: data[0].data, keys: input.keys } : null
}
function toscaFactoryAnyOf(input, anyOf, tosca_version) {
if (!anyOf) return null;
let found = false
let data = null
for (let i = 0; i < anyOf.length && !found; i++ ) {
_errors = []
data = toscaFactory(input.data, anyOf[i], tosca_version)
found = ( _errors.length == 0 )
}
return { data: data[0].data, keys: input.keys }
}
function toscaFactoryItems(input, items, tosca_version) {
if (!items) return null;
let data_list = []
if (input.data.val instanceof Array) {
for (let ele of input.data.val ) {
let item = toscaFactory(ele, items, tosca_version)
for (let item_ele of item) {
data_list.push(item_ele.data)
}
}
return { data: data_list, keys: {} }
} else {
_errors.push(`Syntax error: input should be a list (${JSON.stringify(input)}, null,2))`)
return null
}
}
function toscaFactoryArgs(input, args, tosca_version) {
if (!args) return null;
let newArgs = {}
for (let ele in args) {
let eleData = toscaFactory(input.data, args[ele], tosca_version)
if (eleData.length == 0) {
if (ele.indexOf('@') < 0) newArgs[ele] = null
// handles simple ids
} else if (ele.indexOf('@') < 0) {
newArgs[ele] = eleData[0].data
// handles generic ids
} else {
eleData.forEach(function(eleData_item) {
let keys = eleData_item.keys
let newEle = ele
for (k in keys) { newEle = newEle.replace(k, keys[k]) }
newArgs[newEle] = eleData_item.data
})
}
}
return { data: newArgs, keys: input.keys }
}
function toscaFactory(input, factory, tosca_version) {
if (!factory) return []
// 1. extract data from path ==> [ {val: ... , keys: ... } ]
let data = getFromPath(input, factory.path, factory.isin, factory.isnotin)
// 2. collect results for each data from path,
data = data.map(function(data_item) {
return toscaFactoryRef( data_item, factory.ref, tosca_version ) ||
toscaFactoryAnyOf( data_item, factory.anyOf, tosca_version ) ||
toscaFactoryArgs( data_item, factory.args, tosca_version ) ||
toscaFactoryItems( data_item, factory.items, tosca_version ) ||
data_item
})
// 3. Create ToscaObject
let type = factory.type
if (type) {
if (type in ToscaTypes.classes) {
if (data.length == 0) _errors.push(`No data to instanciate ${type}`)
data = data.map(function(data_item) {
try {
return { data: new ToscaTypes.DynamicClass(type, data_item, input),
keys: (data_item.keys) ? data_item.keys : {} }
} catch (e) {
_errors.push(`${e.name}: ${e.message}`)
return { data: null, keys: {} }
}
})
} else throw Error(`invalid type (${type}) in tosca factory definition`)
}
return data
}
function buildToscaAst(ast, schema_name, version, filename) {
let tosca_version = (version) ? version : "tosca_simple_yaml_1_2"
let factory_name = (schema_name) ? schema_name : "service_template"
let factory = _factory[tosca_version][factory_name]
let ret = toscaFactory(ast, factory, tosca_version)
return ret
}
function parse_file(filename, schema_name=null, version=null) {
let input = fs.readFileSync(filename, 'UTF-8');
return parse(input, filename, schema_name, version, dbg);
}
function parse_string(input, schema_name=null, version=null) {
return parse(input, null, schema_name, version);
}
function parse(input, filename, schema_name, version) {
_errors = []
let yamlAst = buildYamlAst(input, filename);
if (yamlAst && validateToscaSyntax(yamlAst, schema_name, version, filename) ) {
toscaAst = buildToscaAst(yamlAst, schema_name, version, filename)
if ((toscaAst instanceof Array) && (toscaAst.length > 0) && ("data" in toscaAst[0])) {
return toscaAst[0].data
}
}
// return (_errors.length) ? _errors : (toscaAst) ? toscaAst[0].data : null
};
exports.parse_file=parse_file
exports.parse_string=parse_string
exports.set_debug=set_debug