-
Notifications
You must be signed in to change notification settings - Fork 79
/
thing-model-helpers.ts
649 lines (608 loc) · 24.4 KB
/
thing-model-helpers.ts
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/********************************************************************************
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
import Ajv, { ValidateFunction, ErrorObject } from "ajv";
import addFormats from "ajv-formats";
import * as http from "http";
import * as https from "https";
import * as fs from "fs";
import { JsonPlaceholderReplacer } from "json-placeholder-replacer";
import { LinkElement } from "wot-thing-description-types";
import { DataSchema, ExposedThingInit } from "wot-typescript-definitions";
import { ThingModel } from "wot-thing-model-types";
import TMSchema from "wot-thing-model-types/schema/tm-json-schema-validation.json";
import { Resolver } from "./resolver-interface";
// TODO: Refactor and reuse debug solution from core package
import debug from "debug";
const namespace = "node-wot:td-tools:thing-model-helpers";
const logDebug = debug(`${namespace}:debug`);
const logError = debug(`${namespace}:error`);
const tmSchema = TMSchema;
// RegExps take from https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts
const ajv = new Ajv({ strict: false });
addFormats(ajv);
export type LINK_TYPE = "tm:extends" | "tm:submodel";
export type AFFORDANCE_TYPE = "properties" | "actions" | "events";
export type COMPOSITION_TYPE = "extends" | "imports";
export type ModelImportsInput = {
uri?: string;
type: AFFORDANCE_TYPE;
name: string;
};
export type CompositionOptions = {
baseUrl?: string;
selfComposition?: boolean;
map?: Record<string, unknown>;
};
export type modelComposeInput = {
extends?: ThingModel[];
imports?: (ModelImportsInput & { affordance: DataSchema })[];
submodel?: Record<string, ThingModel>;
};
export class ThingModelHelpers {
static tsSchemaValidator = ajv.compile(tmSchema) as ValidateFunction;
private deps: string[] = [] as string[];
private resolver?: Resolver = undefined;
constructor(_resolver?: Resolver) {
if (_resolver) {
this.resolver = _resolver;
}
}
/**
* Checks if the input is a ThingModel.
*
* @param data - The record to be validated
* @returns a boolean: true if the input is a Thing Model, false otherwise
*
* @experimental
*/
public static isThingModel(_data: unknown): _data is ThingModel {
if (_data === null || _data === undefined) {
return false;
}
if (!(typeof _data === "object") || Array.isArray(_data)) {
return false;
}
const data = _data as Record<string, unknown>;
if (Array.isArray(data["@type"])) {
const valid = data["@type"].filter((x) => x === "tm:ThingModel").length > 0;
if (valid) {
return true;
}
} else if (data["@type"] === "tm:ThingModel") {
return true;
}
if (Object.keys(this.getThingModelRef(data)).length > 0) {
// FIXME: different from specifications
return true;
}
if ("links" in data && Array.isArray(data.links)) {
const foundTmExtendsRel = data.links.find((link) => link.rel === "tm:extends");
if (foundTmExtendsRel != null) return true;
}
if (data.properties !== undefined) {
if (this.isThingModel(data.properties as Record<string, unknown>)) return true;
}
if (data.actions !== undefined) {
if (this.isThingModel(data.actions as Record<string, unknown>)) return true;
}
if (data.events !== undefined) {
if (this.isThingModel(data.events as Record<string, unknown>)) return true;
}
return false;
}
/**
* Returns the version of the input Thing Model.
*
* @param data - the Thing Model where to get the version
* @returns the version of the Thing Model as string
*
* @experimental
*/
public static getModelVersion(data: ThingModel): string | undefined {
return typeof data?.version === "object" && typeof data?.version?.model === "string"
? data.version.model
: undefined;
}
/**
* Validates a Thing Model
*
* @param data - the Thing Model to be checked
* @returns an object with keys:
* -valid: the boolean for validity- and
* -errors: the string containing the errors occurred. Undefined if valid.
*
* @experimental
*/
public static validateThingModel(data: ThingModel): { valid: boolean; errors?: string } {
const isValid = ThingModelHelpers.tsSchemaValidator(data);
let errors;
if (!isValid) {
errors = ThingModelHelpers.tsSchemaValidator.errors?.map((o: ErrorObject) => o.message).join("\n");
}
return {
valid: isValid,
errors,
};
}
/**
* builds the partialTDs starting from a Thing Model.
*
* @param model - The Thing Model to start from
* @param options - Optional parameter of type CompositionOptions for passing
* further information to the building process.
* @returns an array of Partial TDs
*
* @experimental
*/
public async getPartialTDs(model: unknown, options?: CompositionOptions): Promise<ExposedThingInit[]> {
const extendedModels = await this._getPartialTDs(model, options);
const extendedPartialTDs = extendedModels.map((_data) => {
const data = _data as ExposedThingInit;
// change the @type
if (data["@type"] instanceof Array) {
data["@type"] = data["@type"].map((el) => {
if (el === "tm:ThingModel") {
return "Thing";
}
return el;
});
} else {
data["@type"] = "Thing";
}
return data;
});
return extendedPartialTDs;
}
/**
* Retrieves the Thing Model from the given uri.
*
* @param uri - The uri from where to take the Thing Model
* @returns asynchronously a Thing Model
*
* @experimental
*/
public async fetchModel(uri: string): Promise<ThingModel> {
this.addDependency(uri);
let tm: ThingModel;
if (this.resolver) {
tm = (await this.resolver.fetch(uri)) as ThingModel;
} else {
tm = (await this.localFetch(uri)) as ThingModel;
}
if (!ThingModelHelpers.isThingModel(tm)) {
throw new Error(`Data at ${uri} is not a Thing Model`);
}
return tm;
}
private localFetch(uri: string): unknown {
const proto = uri.split("://")[0];
switch (proto) {
case "file": {
const file = uri.split("://")[1];
return new Promise((resolve, reject) => {
fs.readFile(file, { encoding: "utf-8" }, function (err, data) {
if (!err) {
resolve(JSON.parse(data));
} else {
reject(err);
}
});
});
}
case "http": {
return new Promise((resolve, reject) => {
http.get(uri, (res) => {
if (res.statusCode == null || res.statusCode !== 200) {
reject(new Error(`http status code not 200 but ${res.statusCode} for ${uri}`));
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
rawData += chunk;
});
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
logDebug(`https fetched: ${parsedData}`);
resolve(parsedData);
} catch (error) {
logError(error);
}
});
}).on("error", (e) => {
reject(e);
});
});
}
case "https": {
return new Promise((resolve, reject) => {
https
.get(uri, (res) => {
if (res.statusCode == null || res.statusCode !== 200) {
reject(new Error(`https status code not 200 but ${res.statusCode} for ${uri}`));
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
rawData += chunk;
});
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
logDebug(`https fetched: ${parsedData}`);
resolve(parsedData);
} catch (error) {
logError(error);
}
});
})
.on("error", (e) => {
reject(e);
});
});
}
default:
break;
}
return null;
}
private async _getPartialTDs(model: unknown, options?: CompositionOptions): Promise<ThingModel[]> {
if (!ThingModelHelpers.isThingModel(model)) {
throw new Error(`${model} is not a Thing Model`);
}
let isValid = ThingModelHelpers.validateThingModel(model);
if (isValid.valid === false || isValid.errors !== undefined) {
throw new Error(isValid.errors);
}
isValid = this.checkPlaceholderMap(model, options?.map);
if (isValid.valid === false || isValid.errors !== undefined) {
throw new Error(isValid.errors);
}
const modelInput = await this.fetchAffordances(model);
const extendedModels = await this.composeModel(model, modelInput, options);
return extendedModels;
}
/**
* Retrieves and fills asynchronously all the external references of a Thing Model.
*
* @param data - The Thing Model to be filled
* @returns asynchronously a modelComposeInput object containing all the retrieved data
*
* @experimental
*/
private async fetchAffordances(data: ThingModel): Promise<modelComposeInput> {
const modelInput: modelComposeInput = {};
const extLinks = ThingModelHelpers.getThingModelLinks(data, "tm:extends");
if (extLinks.length > 0) {
modelInput.extends = [] as ThingModel[];
for (const s of extLinks) {
let source = await this.fetchModel(s.href);
[source] = await this._getPartialTDs(source);
modelInput.extends.push(source);
}
}
const affordanceTypes = ["properties", "actions", "events"];
modelInput.imports = [];
for (const affType of affordanceTypes) {
const affRefs = ThingModelHelpers.getThingModelRef(data[affType] as DataSchema);
if (Object.keys(affRefs).length > 0) {
for (const aff in affRefs) {
const affUri = affRefs[aff] as string;
const refObj = this.parseTmRef(affUri);
if (refObj.uri == null) {
throw new Error(`Missing remote path in ${affUri}`);
}
let source = await this.fetchModel(refObj.uri);
[source] = await this._getPartialTDs(source);
delete (data[affType] as DataSchema)[aff]["tm:ref"];
const importedAffordance = this.getRefAffordance(refObj, source) ?? {};
refObj.name = aff; // update the name of the affordance
modelInput.imports.push({ affordance: importedAffordance, ...refObj });
}
}
}
const tmLinks = ThingModelHelpers.getThingModelLinks(data, "tm:submodel");
if (tmLinks.length > 0) {
modelInput.submodel = {} as Record<string, ThingModel>;
for (const l of tmLinks) {
const submodel = await this.fetchModel(l.href);
modelInput.submodel[l.href] = submodel;
}
}
return modelInput;
}
private async composeModel(
data: ThingModel,
modelObject: modelComposeInput,
options?: CompositionOptions
): Promise<ThingModel[]> {
let tmpThingModels = [] as ThingModel[];
const title = (data.title ?? "").replace(/ /g, "");
if (!options) {
options = {} as CompositionOptions;
}
if (options.baseUrl == null) {
options.baseUrl = ".";
}
const newTMHref = this.returnNewTMHref(options.baseUrl, title);
const newTDHref = this.returnNewTDHref(options.baseUrl, title);
if ("extends" in modelObject) {
const extendObjs = modelObject.extends ?? [];
for (const extendObj of extendObjs) {
data = ThingModelHelpers.extendThingModel(extendObj, data);
}
// remove the tm:extends links
data.links = data.links?.filter((link) => link.rel !== "tm:extends");
}
if ("imports" in modelObject) {
const importObjs = modelObject.imports ?? [];
for (const importedObj of importObjs) {
data = ThingModelHelpers.importAffordance(
importedObj.type,
importedObj.name,
importedObj.affordance,
data
);
}
}
if ("submodel" in modelObject) {
const submodelObj = modelObject.submodel;
for (const key in submodelObj) {
const sub = submodelObj[key];
if (options.selfComposition === true) {
if (!data.links) {
throw new Error(
"You used self composition but links are missing; they are needed to extract the instance name"
);
}
const index = data.links.findIndex((el) => el.href === key);
const el = data.links[index];
const instanceName = el.instanceName;
if (instanceName == null) {
throw new Error("Self composition is not possible without instance names");
}
// self composition enabled, just one TD expected
const [subPartialTD] = await this._getPartialTDs(sub, options);
const affordanceTypes = ["properties", "actions", "events"];
for (const affType of affordanceTypes) {
for (const affKey in subPartialTD[affType] as DataSchema) {
const newAffKey = `${instanceName}_${affKey}`;
if (!(affType in data)) {
data[affType] = {} as DataSchema;
}
(data[affType] as DataSchema)[newAffKey] = (subPartialTD[affType] as DataSchema)[
affKey
] as DataSchema;
}
}
} else {
const subTitle = (sub.title ?? "").replace(/ /g, "");
const subNewHref = this.returnNewTDHref(options.baseUrl, subTitle);
if (!sub.links) {
sub.links = [];
}
sub.links.push({
rel: "collection",
href: newTDHref,
type: "application/td+json",
});
const tmpPartialSubTDs = await this._getPartialTDs(sub, options);
tmpThingModels.push(...tmpPartialSubTDs);
data = ThingModelHelpers.formatSubmodelLink(data, key, subNewHref);
}
}
}
if (!data.links || options.selfComposition === true) {
data.links = [];
}
// add reference to the thing model
data.links.push({
rel: "type",
href: newTMHref,
type: "application/tm+json",
});
if ("version" in data) {
delete data.version;
}
if (options.map) {
data = this.fillPlaceholder(data, options.map);
}
tmpThingModels.unshift(data); // put itself as first element
tmpThingModels = tmpThingModels.map((el) => this.fillPlaceholder(el, options?.map)); // TODO: make more efficient, since repeated each recursive call
if (this.deps.length > 0) {
this.removeDependency();
}
return tmpThingModels;
}
private static getThingModelRef(data: Record<string, unknown>): Record<string, unknown> {
const refs = {} as Record<string, unknown>;
if (data == null) {
return refs;
}
for (const key in data) {
for (const key1 in data[key] as Record<string, unknown>) {
if (key1 === "tm:ref") {
refs[key] = (data[key] as Record<string, unknown>)["tm:ref"] as string;
}
}
}
return refs;
}
private static getThingModelLinks(data: Record<string, unknown>, type: LINK_TYPE): LinkElement[] {
let links = [] as LinkElement[];
if ("links" in data && Array.isArray(data.links)) {
links = data.links;
}
return links.filter((el) => el.rel === type);
}
private static extendThingModel(source: ThingModel, dest: ThingModel): ThingModel {
let extendedModel = {} as ThingModel;
const properties = source.properties;
const actions = source.actions;
const events = source.events;
extendedModel = { ...source, ...dest };
// TODO: implement validation for extending
if (properties) {
if (!extendedModel.properties) {
extendedModel.properties = {};
}
for (const key in properties) {
if (dest.properties && dest.properties[key] != null) {
extendedModel.properties[key] = { ...properties[key], ...dest.properties[key] };
} else {
extendedModel.properties[key] = properties[key];
}
}
}
if (actions) {
if (!extendedModel.actions) {
extendedModel.actions = {};
}
for (const key in actions) {
if (dest.actions && key in dest.actions) {
extendedModel.actions[key] = { ...actions[key], ...dest.actions[key] };
} else {
extendedModel.actions[key] = actions[key];
}
}
}
if (events) {
if (!extendedModel.events) {
extendedModel.events = {};
}
for (const key in events) {
if (dest.events && key in dest.events) {
extendedModel.events[key] = { ...events[key], ...dest.events[key] };
} else {
extendedModel.events[key] = events[key];
}
}
}
return extendedModel;
}
private static importAffordance(
affordanceType: AFFORDANCE_TYPE,
affordanceName: string,
source: DataSchema,
dest: ThingModel
): ThingModel {
if (!dest[affordanceType]) {
dest[affordanceType] = {};
}
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// tsc doesn't know that dest[affordanceType] is not null
const d = dest[affordanceType]![affordanceName];
dest[affordanceType]![affordanceName] = { ...source, ...d };
for (const key in dest[affordanceType]![affordanceName]) {
if (dest[affordanceType]![affordanceName][key] === undefined) {
delete dest[affordanceType]![affordanceName][key];
}
}
/* eslint-enable @typescript-eslint/no-non-null-assertion */
return dest;
}
private static formatSubmodelLink(source: ThingModel, oldHref: string, newHref: string) {
if (!source.links) {
throw new Error("Links are missing");
}
const index = source.links.findIndex((el) => el.href === oldHref);
if (index === -1) {
throw new Error("Link not found");
}
const el = source.links[index];
if ("instanceName" in el) {
delete el.instanceName;
}
source.links[index] = {
...el,
href: newHref,
type: "application/td+json",
rel: "item",
};
return source;
}
private parseTmRef(value: string): ModelImportsInput {
const thingModelUri = value.split("#")[0];
const affordaceUri = value.split("#")[1];
const affordaceType = affordaceUri.split("/")[1] as AFFORDANCE_TYPE;
const affordaceName = affordaceUri.split("/")[2];
return { uri: thingModelUri, type: affordaceType, name: affordaceName };
}
private getRefAffordance(obj: ModelImportsInput, thing: ThingModel): DataSchema | undefined {
const affordanceType = obj.type;
const affordanceKey = obj.name;
if (!(affordanceType in thing)) {
return undefined;
}
const affordances = thing[affordanceType] as DataSchema;
if (!(affordanceKey in affordances)) {
return undefined;
}
return affordances[affordanceKey];
}
private fillPlaceholder(data: Record<string, unknown>, map: Record<string, unknown> = {}): ThingModel {
const placeHolderReplacer = new JsonPlaceholderReplacer();
placeHolderReplacer.addVariableMap(map);
return placeHolderReplacer.replace(data) as ThingModel;
}
private checkPlaceholderMap(
model: ThingModel,
map: Record<string, unknown> = {}
): { valid: boolean; errors?: string } {
const regex = "{{.*?}}";
const modelString = JSON.stringify(model);
// first check if model needs map
let keys: string[] = modelString.match(new RegExp(regex, "g")) ?? [];
keys = keys.map((el) => el.replace("{{", "").replace("}}", ""));
let isValid = true;
let errors;
if (keys?.length > 0 && (map === undefined || map === null)) {
isValid = false;
errors = `No map provided for model ${model.title}`;
} else if (keys.length > 0) {
keys.every((key) => {
if (!(key in map)) {
errors = `Missing required fields in map for model ${model.title}`;
isValid = false;
return false;
}
return true;
});
}
return {
valid: isValid,
errors,
};
}
private returnNewTMHref(baseUrl: string, tdname: string) {
return `${baseUrl}/${tdname}.tm.jsonld`;
}
private returnNewTDHref(baseUrl: string, tdname: string) {
return `${baseUrl}/${tdname}.td.jsonld`;
}
private addDependency(dep: string) {
if (this.deps.indexOf(dep) > -1) {
throw new Error(`Circular dependency found for ${dep}`);
}
this.deps.push(dep);
}
private removeDependency(dep?: string) {
if (dep != null) {
this.deps = this.deps.filter((el) => el !== dep);
} else {
this.deps.pop();
}
}
}