Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use base in hrefs for AID #1135

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions packages/td-tools/src/util/asset-interface-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,17 @@ export class AssetInterfaceDescriptionUtil {
return ""; // TODO what is the right value if information cannot be found
}

private getSecurityDefinitionsFromEndpointMetadata(endpointMetadata?: Record<string, unknown>): {
[k: string]: SecurityScheme;
} {
private updateRootMetadata(thing: Thing, endpointMetadata?: Record<string, unknown>) {
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<SecurityScheme> = [];
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) {
Expand All @@ -286,19 +286,7 @@ export class AssetInterfaceDescriptionUtil {
}
}
}
}
}
}
return securityDefinitions;
}

private getSecurityFromEndpointMetadata(
endpointMetadata?: Record<string, unknown>
): 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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -568,8 +557,8 @@ export class AssetInterfaceDescriptionUtil {
const secNamesForEndpointMetadata = new Map<Record<string, unknown>, string[]>();
for (const endpointMetadata of smInformation.endpointMetadataArray) {
const secNames: Array<string> = [];
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)) {
Expand Down Expand Up @@ -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?
Expand Down
15 changes: 8 additions & 7 deletions packages/td-tools/test/AssetInterfaceDescriptionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down