Skip to content

Commit

Permalink
[#1034] added generation for "instance" version for TDs - simply copy…
Browse files Browse the repository at this point in the history
…ing the "model" version

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Feb 16, 2022
1 parent 22e2cd4 commit 5d2e0e9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import org.eclipse.ditto.wot.model.ThingModel;
import org.eclipse.ditto.wot.model.Title;
import org.eclipse.ditto.wot.model.UriVariables;
import org.eclipse.ditto.wot.model.Version;
import org.eclipse.ditto.wot.model.WotThingModelInvalidException;

import akka.actor.ActorSystem;
Expand Down Expand Up @@ -154,7 +155,8 @@ public ThingDescription generateThingDescription(final ThingId thingId,
final ThingModel cleanedTm = removeThingModelSpecificElements(thingModelWithExtensionsAndImports, dittoHeaders);

final ThingDescription.Builder tdBuilder = ThingDescription.newBuilder(cleanedTm.toJson());
addThingDescriptionIdAndBase(tdBuilder, thingId, featureId);
addBase(tdBuilder, thingId, featureId);
addInstanceVersion(tdBuilder, cleanedTm.getVersion().orElse(null));
addThingDescriptionLinks(tdBuilder, thingModelUrl, null != featureId, thingId);
convertThingDescriptionTmSubmodelLinksToItems(tdBuilder, dittoHeaders);
addThingDescriptionTemplateFromConfig(tdBuilder);
Expand Down Expand Up @@ -265,7 +267,7 @@ private void replaceAtType(final ThingModel thingModel, final ThingModel.Builder
.build());

if (atType.equals(SingleAtType.of("tm:ThingModel"))) {
tmBuilder.setAtType(null);
tmBuilder.setAtType(AtType.newSingleAtType("Thing"));
} else if (atType instanceof SingleAtType) {
throw WotThingModelInvalidException.newBuilder(
"The WoT ThingModel must be of '@type' being 'tm:ThingModel'")
Expand All @@ -278,7 +280,8 @@ private void replaceAtType(final ThingModel thingModel, final ThingModel.Builder
.forEach(st -> {
if (st.equals(SingleAtType.of("tm:ThingModel"))) {
tmThingModelWasPresent.set(true);
// remove tm:ThingModel by not adding it again..
// remove tm:ThingModel by not adding it again
keptTypes.add(AtType.newSingleAtType("Thing"));
} else {
keptTypes.add(st);
}
Expand All @@ -297,7 +300,7 @@ private void removeTmRequired(final ThingModel.Builder builder) {
builder.remove(ThingModel.JsonFields.TM_REQUIRED);
}

private void addThingDescriptionIdAndBase(final ThingDescription.Builder thingDescriptionBuilder,
private void addBase(final ThingDescription.Builder thingDescriptionBuilder,
final ThingId thingId, @Nullable final String featureId) {
if (null != featureId) {
final String featurePath = "/features/" + featureId;
Expand All @@ -309,6 +312,16 @@ private void addThingDescriptionIdAndBase(final ThingDescription.Builder thingDe
}
}

private void addInstanceVersion(final ThingDescription.Builder thingDescriptionBuilder,
@Nullable final Version tmVersion) {
final Version versionWithInstance = Optional.ofNullable(tmVersion)
.map(Version::toBuilder)
.map(vBuilder -> vBuilder.setInstance(tmVersion.getModel().orElseThrow()))
.map(Version.Builder::build)
.orElse(null);
thingDescriptionBuilder.setVersion(versionWithInstance);
}

private String buildThingIdBasePath(final ThingId thingId) {
return toThingDescriptionConfig.getBasePrefix() + "/api/2/things/" + thingId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.wot.model;

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.util.Optional;

import javax.annotation.Nullable;

import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObjectBuilder;

/**
* Mutable builder for {@link Version}s.
*/
final class MutableVersionBuilder implements Version.Builder {

private final JsonObjectBuilder wrappedObjectBuilder;

MutableVersionBuilder(final JsonObjectBuilder wrappedObjectBuilder) {
this.wrappedObjectBuilder = wrappedObjectBuilder;
}

@Override
public Version.Builder setInstance(final String instance) {
putValue(Version.JsonFields.INSTANCE, instance);
return this;
}

@Override
public Version.Builder setModel(final String model) {
putValue(Version.JsonFields.MODEL, model);
return this;
}

@Override
public Version build() {
return new ImmutableVersion(wrappedObjectBuilder.build());
}

private <J> void putValue(final JsonFieldDefinition<J> definition, @Nullable final J value) {
final Optional<JsonKey> keyOpt = definition.getPointer().getRoot();
if (keyOpt.isPresent()) {
final JsonKey key = keyOpt.get();
if (null != value) {
checkNotNull(value, definition.getPointer().toString());
wrappedObjectBuilder.remove(key);
wrappedObjectBuilder.set(definition, value);
} else {
wrappedObjectBuilder.remove(key);
}
}
}
}
29 changes: 29 additions & 0 deletions wot/model/src/main/java/org/eclipse/ditto/wot/model/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,39 @@ static Version fromJson(final JsonObject jsonObject) {
return new ImmutableVersion(jsonObject);
}

static Version.Builder newBuilder() {
return Version.Builder.newBuilder();
}

static Version.Builder newBuilder(final JsonObject jsonObject) {
return Version.Builder.newBuilder(jsonObject);
}

default Version.Builder toBuilder() {
return Version.Builder.newBuilder(toJson());
}

Optional<String> getInstance();

Optional<String> getModel();

interface Builder {

static Builder newBuilder() {
return new MutableVersionBuilder(JsonObject.newBuilder());
}

static Builder newBuilder(final JsonObject jsonObject) {
return new MutableVersionBuilder(jsonObject.toBuilder());
}

Builder setInstance(String instance);

Builder setModel(String model);

Version build();
}

/**
* An enumeration of the known {@link JsonFieldDefinition}s of a Version.
*/
Expand Down

0 comments on commit 5d2e0e9

Please sign in to comment.