From bfe0a806f6864becc7e725991b804cb01b46940c Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Tue, 24 Oct 2023 11:06:14 +0200 Subject: [PATCH] refactor: use base in hrefs for AID --- .../src/util/asset-interface-description.ts | 46 ++++++++++--------- .../test/AssetInterfaceDescriptionTest.ts | 15 +++--- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/packages/td-tools/src/util/asset-interface-description.ts b/packages/td-tools/src/util/asset-interface-description.ts index 79d7cdddb..d0927638c 100644 --- a/packages/td-tools/src/util/asset-interface-description.ts +++ b/packages/td-tools/src/util/asset-interface-description.ts @@ -258,17 +258,17 @@ export class AssetInterfaceDescriptionUtil { return ""; // TODO what is the right value if information cannot be found } - private getSecurityDefinitionsFromEndpointMetadata(endpointMetadata?: Record): { - [k: string]: SecurityScheme; - } { + private updateRootMetadata(thing: Thing, endpointMetadata?: Record) { const securityDefinitions: { [k: string]: SecurityScheme; } = {}; + const security: string[] = []; if (endpointMetadata?.value instanceof Array) { for (const v of endpointMetadata.value) { - if (v.idShort === "securityDefinitions") { - // const securitySchemes: Array = []; + if (v.idShort === "base") { + thing.base = v.value; + } else if (v.idShort === "securityDefinitions") { if (v.value instanceof Array) { for (const securityDefinitionsValues of v.value) { if (securityDefinitionsValues.idShort != null) { @@ -286,19 +286,7 @@ export class AssetInterfaceDescriptionUtil { } } } - } - } - } - return securityDefinitions; - } - - private getSecurityFromEndpointMetadata( - endpointMetadata?: Record - ): string | [string, ...string[]] { - const security: string[] = []; - if (endpointMetadata?.value instanceof Array) { - for (const v of endpointMetadata.value) { - if (v.idShort === "security") { + } else if (v.idShort === "security") { if (v.value instanceof Array) { for (const securityValue of v.value) { if (securityValue.value != null) { @@ -310,7 +298,8 @@ export class AssetInterfaceDescriptionUtil { } } - return security as string | [string, ...string[]]; + thing.securityDefinitions = securityDefinitions; + thing.security = security as string | [string, ...string[]]; } private createInteractionForm(vi: AASInteraction, addSecurity: boolean): TD.Form { @@ -568,8 +557,8 @@ export class AssetInterfaceDescriptionUtil { const secNamesForEndpointMetadata = new Map, string[]>(); for (const endpointMetadata of smInformation.endpointMetadataArray) { const secNames: Array = []; - thing.securityDefinitions = this.getSecurityDefinitionsFromEndpointMetadata(endpointMetadata); - thing.security = this.getSecurityFromEndpointMetadata(endpointMetadata); + // update base, securityDefinitions, security, ... + this.updateRootMetadata(thing, endpointMetadata); // iterate over securitySchemes // eslint-disable-next-line unused-imports/no-unused-vars for (const [key, value] of Object.entries(thing.securityDefinitions)) { @@ -925,7 +914,20 @@ export class AssetInterfaceDescriptionUtil { // walk over string values like: "href", "contentType", "htv:methodName", ... for (let formTerm in formElementPicked) { - const formValue = formElementPicked[formTerm]; + let formValue = formElementPicked[formTerm]; + + // Note: node-wot uses absolute URIs *almost* everywhere but we want to use "base" in AID + // --> try to create relative href's as much as possible + if ( + formTerm === "href" && + td.base != null && + td.base.length > 0 && + typeof formValue === "string" && + formValue.startsWith(td.base) + ) { + formValue = formValue.substring(td.base.length); + console.log("dsadsa: " + formValue); + } // Note: AID does not allow idShort to contain values with colon (i.e., ":") --> "_" used instead // TODO are there more characters we need to deal with? diff --git a/packages/td-tools/test/AssetInterfaceDescriptionTest.ts b/packages/td-tools/test/AssetInterfaceDescriptionTest.ts index 2e26344fd..2a3ad399c 100644 --- a/packages/td-tools/test/AssetInterfaceDescriptionTest.ts +++ b/packages/td-tools/test/AssetInterfaceDescriptionTest.ts @@ -264,10 +264,14 @@ class AssetInterfaceDescriptionUtilTest { hasEndpointMetadata = true; const endpointMetadata = smValue; expect(endpointMetadata).to.have.property("value").to.be.an("array").to.have.lengthOf.greaterThan(0); + let hasBase = false; let hasSecurity = false; let hasSecurityDefinitions = false; for (const endpointMetadataValue of endpointMetadata.value) { - if (endpointMetadataValue.idShort === "security") { + if (endpointMetadataValue.idShort === "base") { + hasBase = true; + expect(endpointMetadataValue.value).to.equal("modbus+tcp://192.168.178.146:502/"); + } else if (endpointMetadataValue.idShort === "security") { hasSecurity = true; expect(endpointMetadataValue) .to.have.property("value") @@ -301,6 +305,7 @@ class AssetInterfaceDescriptionUtilTest { expect(hasBasicSC).to.equal(true); } } + expect(hasBase).to.equal(true); expect(hasSecurity).to.equal(true); expect(hasSecurityDefinitions).to.equal(true); } @@ -358,9 +363,7 @@ class AssetInterfaceDescriptionUtilTest { for (const formEntry of propProperty.value) { if (formEntry.idShort === "href") { hasHref = true; - expect(formEntry.value).to.equal( - "modbus+tcp://192.168.178.146:502/1/40020?quantity=16" - ); + expect(formEntry.value).to.equal("1/40020?quantity=16"); } else if (formEntry.idShort === "op") { hasOp = true; expect(formEntry.value).to.equal("readproperty"); @@ -425,9 +428,7 @@ class AssetInterfaceDescriptionUtilTest { for (const formEntry of propProperty.value) { if (formEntry.idShort === "href") { hasHref = true; - expect(formEntry.value).to.equal( - "modbus+tcp://192.168.178.146:502/40361?quantity=1" - ); + expect(formEntry.value).to.equal("40361?quantity=1"); // use base } else if (formEntry.idShort === "op") { hasOp = true; expect(formEntry.value).to.equal("readproperty");