forked from ditrit/ToP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ToscaTypes.js
501 lines (453 loc) · 12.5 KB
/
ToscaTypes.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
const path = require("path")
exports=module.exports={};
class ToscaAst {
constructor(args, context) {
if (context) {
if (args && args.data ) {
this.context = context
this.tag = args.data.tag
if (!this.tag) this.tag = ""
this.val = args.data.val
} else throw Error(`Syntax error : no value found`);
}
}
}
class ToscaStr extends ToscaAst {
constructor(args, context) {
super(args, args.data)
if (!context && typeof(args) == 'string')
this.val = args
else if (!this.tag.endsWith(':str') )
throw Error(`Syntax error : ${args.data} is not a string`);
}
}
class ToscaURL extends ToscaStr {
constructor(args, context) {
super(args, context)
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d_-]*[a-z\\d])*)\\.?)+[a-z\\d_-]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
if (!pattern.test(this.val))
throw Error(`Syntax error : '${this.val}' is not a valid url`);
}
}
class ToscaPath extends ToscaStr {
constructor(args, context) {
super(args, context)
this.path = path.parse(path.normalize(args.data.val))
}
}
class ToscaBool extends ToscaAst {
constructor(args, context) {
super(args, args.data)
if (!this.tag.endsWith(':bool') )
throw Error(`Syntax error : ${args.data} is not a boolean`);
}
}
class ToscaInteger extends ToscaAst {
constructor(args, context) {
super(args, args.data)
this.val = parseInt(this.val)
if (!this.tag.endsWith(':int') )
throw Error(`Syntax error : ${args.data} is not an integer`);
}
}
class ToscaFloat extends ToscaAst {
constructor(args, context) {
super(args, args.data)
this.val = parseFloat(this.val)
if (!this.tag.endsWith(':float') )
throw Error(`Syntax error : ${args.data} is not a float`);
}
}
class ToscaTimestamp extends ToscaAst {
constructor(args, context) {
super(args, args.data)
if (!this.tag.endsWith(':timestamp') )
throw Error(`Syntax error : ${args.data} is not a timestamp`);
}
}
class ToscaVersion extends ToscaAst {
constructor(args, context) {
super(args, args.data)
let str
if (args.data instanceof ToscaFloat || this.tag.endsWith(':float') ) {
str = args.data.val.toString()
} else if (args.data instanceof ToscaStr || this.tag.endsWith(':string')) {
str = args.data.val
} else
throw Error(`Syntax error : ${args.data} is not a version`);
let parts = str.split(".")
this.major = parts[0]
this.minor = parts[1]
this.fix = parts[2]
let last = parts[3]
if (typeof(last) == 'string') {
parts = last.split(".")
this.qualifier = parts[0]
this.build = parts[1]
}
}
}
class ToscaUnbounded extends ToscaStr {
constructor(args, context) {
super(args, context)
if (this.val != "UNBOUNDED")
throw Error(`Syntax error : ${args.data} is not an UNBOUNDED value`);
}
}
class ToscaRange extends ToscaAst {
constructor(args, context) {
super(args, context)
if (args.data instanceof Array && args.data.length == 2) {
this.min = args.data[0]
this.max = args.data[1]
} else
throw Error(`Syntax error : ${args.data} is not an array of 2 items`);
}
}
class ToscaNull extends ToscaAst {
constructor(args, context) {
super({data: null}, null)
this.val = null
}
}
class ToscaServiceTemplate extends ToscaAst {
constructor(args, context) {
super(args, context)
this.tosca_definitions_version
= this.tosca_definitions_version
this.description = this.description
this.namespace = this.namespace
this.metadata = this.metadata
this.imports = this.imports
}
}
class ToscaMetadata extends ToscaAst {
constructor(args, context) {
super(args, context)
// this.template_author = args.template_author
// this.template_version = args.template_version
// this.template_name = args.template_name
for (let metadata in this.val) {
this[metadata] = this.val[metadata]
}
}
}
class ToscaRepository extends ToscaAst {
constructor(args, context) {
super(args, context)
if (this.tag.endsWith(":str")) {
this.url = new ToscaURL(args, context)
this.description = null
this.credential = null
} else if (args.data.url) {
this.url = args.data.url
this.description = args.data.description
this.credential = args.data.credential
} else throw Error(`Syntax error : '${args.data}' is not a valid repository`);
}
}
class ToscaRepositories extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaCredential extends ToscaAst {
constructor(args, context) {
super(args, context)
this.token = args.data.token
this.protocol = args.data.protocol
this.token_type = args.data.token_type
this.user = args.data.user
}
}
class ToscaImports extends ToscaAst {
constructor(args, context) {
super(args, context)
this.items = args.data
}
}
class ToscaImport extends ToscaAst {
constructor(args, context) {
super(args, context)
let data = args.data
if (!(data instanceof Object))
throw Error(`Syntax error : no valid import found`);
let keys = Object.keys(data)
if (keys.length == 1 && keys[0] != "file") {
this.name = keys[0]
data = data[this.name]
}
if (data.file instanceof ToscaURL) {
this.file = data.file
this.repository = data.repository
this.namespace_prefix = data.namespace_prefix
this.namespace_uri = data.uri
if (!this.name) this.name = data.file
} else
throw Error(`Syntax error : no valid import found`)
}
}
class ToscaConstraints extends ToscaAst {
constructor(args, context) {
super(args, context)
this.items = args.data
}
}
class ToscaConstraint extends ToscaAst {
constructor(args, context) {
super(args, context)
this.value = args.data
}
}
class ToscaConstraintEqual extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintGreaterThan extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintGreaterOrEqual extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintLessThan extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintLessOrEqual extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintInRange extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintValidValues extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintLength extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintMinLength extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintMaxLength extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintPattern extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaConstraintSchema extends ToscaConstraint {
constructor(args, context) {
super(args, context)
}
}
class ToscaTypeDef extends ToscaAst {
constructor(args, context) {
super(args, context)
if (!(args.data instanceof Object))
throw Error(`Syntax error : no valid data type`);
if (args.data instanceof ToscaStr) {
this.type = args.data
} else if (!(args.data.type instanceof ToscaStr)) {
throw Error(`Syntax error : a data type must have a 'type' value`)
} else {
this.type = args.data.type
this.constraints = args.data.constraints
this.description = args.data.description
this.entry_schema = args.data.entry_schema
}
}
}
class ToscaParameter extends ToscaAst {
constructor(args, context) {
super(args, context)
this.type = args.data.type
this.description = args.data.description
this.status = args.data.status
this.default = args.data.default
}
}
class ToscaAttributes extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaAttribute extends ToscaParameter {
constructor(args, context) {
super(args, context)
}
}
class ToscaProperties extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaProperty extends ToscaParameter {
constructor(args, context) {
super(args, context)
this.constraints = args.data.constraints
this.required = args.data.required
this.metadata = args.data.metadata
}
}
class ToscaInputs extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaInput extends ToscaParameter {
constructor(args, context) {
super(args, context)
this.constraints = args.data.constraints
this.required = args.data.required
this.value = args.data.value
}
}
class ToscaOutputs extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaOutput extends ToscaParameter {
constructor(args, context) {
super(args, context)
this.constraints = args.data.constraints
this.required = args.data.required
this.value = args.data.value
}
}
class ToscaArtifactDefs extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaArtifactDef extends ToscaAst {
constructor(args, context) {
super(args, context)
if (!(args.data instanceof Object))
throw Error(`Syntax error : no valid ArtifactDef value`);
if (args.data instanceof ToscaStr) {
this.type = new ToscaStr("file")
this.file = args.data
} else if (!(args.data.type instanceof ToscaStr && args.data.file instanceof ToscaPath)) {
throw Error(`Syntax error : a data type must have valid 'type' and 'file' values`)
} else {
this.type = args.data.type
this.file = args.data.file
this.repository = args.data.repository
this.description = args.data.description
this.deploy_path = args.data.deploy_path
}
}
}
class ToscaCapabilityDefs extends ToscaAst {
constructor(args, context) {
super(args, context)
this.ids = args.data
}
}
class ToscaCapabilityDef extends ToscaAst {
constructor(args, context) {
super(args, context)
if (!(args.data instanceof Object))
throw Error(`Syntax error : no valid CapabilityDef value`);
if (args.data instanceof ToscaStr) {
this.type = new ToscaStr("file")
this.file = args.data
} else if (!(args.data.type instanceof ToscaStr)) {
throw Error(`Syntax error : a data type must have valid 'type'`)
} else {
this.type = args.data.type
this.file = args.data.file
this.repository = args.data.repositoryRange
this.description = args.data.description
this.properties = args.data.properties
this.attributes = args.data.attributes
this.valid_source_types = args.data.valid_source_types
this.occurrences = args.data.occurrences
}
}
}
const classes = {
ToscaAst,
ToscaStr,
ToscaURL,
ToscaPath,
ToscaBool,
ToscaInteger,
ToscaFloat,
ToscaVersion,
ToscaUnbounded,
ToscaRange,
ToscaTimestamp,
ToscaNull,
ToscaServiceTemplate,
ToscaMetadata,
ToscaRepository,
ToscaRepositories,
ToscaCredential,
ToscaImports,
ToscaImport,
ToscaConstraints,
ToscaConstraintEqual,
ToscaConstraintGreaterThan,
ToscaConstraintGreaterOrEqual,
ToscaConstraintLessThan,
ToscaConstraintLessOrEqual,
ToscaConstraintInRange,
ToscaConstraintValidValues,
ToscaConstraintLength,
ToscaConstraintMinLength,
ToscaConstraintMaxLength,
ToscaConstraintPattern,
ToscaConstraintSchema,
ToscaTypeDef,
ToscaProperty,
ToscaProperties,
ToscaAttribute,
ToscaAttributes,
ToscaInput,
ToscaInputs,
ToscaOutput,
ToscaOutputs,
ToscaArtifactDef,
ToscaArtifactDefs,
ToscaCapabilityDef,
ToscaCapabilityDefs
};
class DynamicClass {
constructor (className, args, context) {
return new classes[(args) ? className : "ToscaNull"](args, context);
}
}
exports.DynamicClass = DynamicClass
exports.classes = classes