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

add new wot-models definition update endpoint #1843 #2103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hu-ahmed
Copy link
Contributor

@hu-ahmed hu-ahmed commented Jan 27, 2025

#1843

The migrateThingDefinition endpoint introduces a comprehensive mechanism for migrating a Thing's definition while ensuring data consistency and applying necessary migrations. This endpoint is designed to facilitate seamless updates by incorporating a structured approach to definition changes and migration payloads.

Key Features:

  • Definition Update with Skeleton Generation:
    The provided Thing definition URL is used to generate a new skeleton Thing, which serves as the baseline for the update.
    The skeleton includes a hierarchical representation of the Thing, ensuring consistency with the WoT model.

  • Patch Condition Evaluation:
    The migration payload can include multiple patch conditions, evaluated using RQL-based expressions to determine whether specific updates should be applied.
    This allows for conditional updates based on existing Thing attributes or feature values.

  • Merging Strategy:
    The update process merges the newly generated skeleton with the existing Thing while preserving essential attributes and properties.
    An optional initialization step applies default values from the skeleton to fill in missing or incomplete properties.

  • Migration Payload Application:
    The provided migration payload is selectively merged into the updated Thing, allowing for targeted modifications without overriding all properties.
    Ensures data integrity by applying only necessary changes based on predefined rules.

  • Validation and Persistence:
    Before finalizing the update, the modified Thing undergoes WoT model validation based on the current configuration, so either it will be rejected or logged as warning in case of inconsistency.

  • API Usage:
    Endpoint:
    POST /api/2/things/{thingId}/migrateDefinition
    Request Body:

{
  "thingDefinitionUrl": "https://models.example.com/thing-definition-1.0.0.tm.jsonld",
  "migrationPayload": {
    "attributes": {
      "manufacturer": "New Corp",
      "location": "Berlin, main floor"
    },
    "features": {
      "thermostat": {
        "properties": {
          "status": {
            "temperature": {
              "value": 23.5,
              "unit": "DEGREE_CELSIUS"
            }
          }
        }
      }
    }
  },
  "patchConditions": {
    "thing:/features/thermostat": "not(exists(/features/thermostat))"
  },
  "initializeMissingPropertiesFromDefaults": true
}

@thjaeckle thjaeckle added this to the 3.7.0 milestone Jan 27, 2025
@hu-ahmed hu-ahmed force-pushed the support-updating-referenced-WoT-ThingModel branch from aed41df to 5d5b2f8 Compare January 27, 2025 19:01
@thjaeckle thjaeckle self-requested a review January 28, 2025 07:11
Copy link
Member

@thjaeckle thjaeckle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hu-ahmed and thanks a lot for providing this PR.

Looks already really promising .. I did a quick check and left a few comments before going deeper :)

ThingsModelFactory.newDefinition(command.getThingDefinitionUrl()),
dittoHeaders
)
.thenApply(optionalSkeleton -> optionalSkeleton.orElseThrow());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orElseThrow is not good here .. it will throw an IllegalArgumentException which will not contain any useful information about why this failed.
I would suggest to throw a more meaningful subclass of WotException, also maintaining dittoHeaders to have proper propagation of correlation-id, trace, etc.

* before persisting changes.
*/
@Immutable
public final class MigrateThingDefinitionStrategy extends AbstractThingModifyCommandStrategy<MigrateThingDefinition> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test is missing for this strategy class.

this.thingDefinitionUrl = checkNotNull(thingDefinitionUrl, "thingDefinitionUrl");
this.migrationPayload = checkJsonSize(checkNotNull(migrationPayload, "migrationPayload"), dittoHeaders);
this.patchConditions =
Collections.unmodifiableMap(patchConditions != null ? patchConditions : Collections.emptyMap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this parameter patchConditions is not marked as @Nullable and (I think) cannot be null from the code flow.
So no need to check for null ..

final MergeThingStrategy mergeStrategy = new MergeThingStrategy(context.getActorSystem());
final Result<ThingEvent<?>> mergeResult = mergeStrategy.doApply(
context,
existingThing,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why apply the mergeStrategy for the existingThing here?
I would expect the updatedThing.

So currently this e.g. validates the old, existingThing (as part of the MergeThingStrategy) against the WoT model instead of the new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Waiting for Approval
Development

Successfully merging this pull request may close these issues.

2 participants