Skip to content

Commit

Permalink
update processor extension EXTENSION_SOURCE_TARGET
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Jan 2, 2025
1 parent 0947354 commit d529344
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@
import dynamic.mapping.processor.model.C8YRequest;
import dynamic.mapping.processor.model.ProcessingContext;
import dynamic.mapping.processor.model.RepairStrategy;
import dynamic.mapping.processor.ProcessingException;

import dynamic.mapping.core.C8YAgent;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import jakarta.ws.rs.ProcessingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void substituteInTargetAndSend(ProcessingContext<byte[]> context, C8YAgen
for (int i = 0; i < deviceEntries.size(); i++) {
// for (MappingSubstitution.SubstituteValue device : deviceEntries) {
getBuildProcessingContext(context, deviceEntries.get(i),
i, deviceEntries.size(), c8yAgent);
i, deviceEntries.size(), c8yAgent);
}
log.info("Tenant {} - Context is completed, sequentially processed, createNonExistingDevice: {} !", tenant,
mapping.createNonExistingDevice);
Expand All @@ -122,7 +123,6 @@ private ProcessingContext<byte[]> getBuildProcessingContext(ProcessingContext<by
Set<String> pathTargets = context.getPathTargets();
Mapping mapping = context.getMapping();
String tenant = context.getTenant();
List<String> pathsTargetForDeviceIdentifiers = context.getPathsTargetForDeviceIdentifiers();
int predecessor = -1;
DocumentContext payloadTarget = JsonPath.parse(mapping.targetTemplate);
for (String pathTarget : pathTargets) {
Expand All @@ -148,64 +148,20 @@ private ProcessingContext<byte[]> getBuildProcessingContext(ProcessingContext<by
pathTarget, substitute.repairStrategy);
}

if (!mapping.targetAPI.equals(API.INVENTORY)) {
if (pathsTargetForDeviceIdentifiers.contains(pathTarget) && mapping.useExternalId) {

ExternalIDRepresentation sourceId = c8yAgent.resolveExternalId2GlobalId(tenant,
new ID(mapping.externalIdType, substitute.value.toString()), context);
// since the attributes identifying the MEA and Inventory requests are removed
// during the design time, they have to be added before sending
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
if (sourceId == null && mapping.createNonExistingDevice) {
ManagedObjectRepresentation attocDevice = null;
Map<String, Object> request = new HashMap<String, Object>();
request.put("name",
"device_" + mapping.externalIdType + "_" + substitute.value);
request.put(MappingRepresentation.MAPPING_GENERATED_TEST_DEVICE, null);
request.put("c8y_IsDevice", null);
request.put("com_cumulocity_model_Agent", null);
try {
var requestString = objectMapper.writeValueAsString(request);
var newPredecessor = context.addRequest(
new C8YRequest(predecessor, RequestMethod.PATCH, device.value.toString(),
mapping.externalIdType, requestString, null, API.INVENTORY, null));
attocDevice = c8yAgent.upsertDevice(tenant,
new ID(mapping.externalIdType, substitute.value.toString()), context);
var response = objectMapper.writeValueAsString(attocDevice);
context.getCurrentRequest().setResponse(response);
substitute.value = attocDevice.getId().getValue();
predecessor = newPredecessor;
} catch (ProcessingException | JsonProcessingException e) {
context.getCurrentRequest().setError(e);
} catch (dynamic.mapping.processor.ProcessingException e) {
context.getCurrentRequest().setError(e);
}
} else if (sourceId == null && context.isSendPayload()) {
throw new RuntimeException(String.format(
"External id %s for type %s not found!",
substitute.toString(),
mapping.externalIdType));
} else if (sourceId == null) {
substitute.value = null;
} else {
substitute.value = sourceId.getManagedObject().getId().getValue();
}

}
substituteValueInPayload( substitute, payloadTarget,
mapping.transformGenericPath2C8YPath(pathTarget));
} else if (!pathsTargetForDeviceIdentifiers.contains(pathTarget)) {
substituteValueInPayload( substitute, payloadTarget,
mapping.transformGenericPath2C8YPath(pathTarget));
}
/*
* step 4 resolve externalIds to c8ySourceIds and create attroc devices
*/
// check if the targetPath == externalId and we need to resolve an external id
prepareAndSubstituteInPayload(context, payloadTarget, pathTarget, substitute, c8yAgent);
}
/*
* step 4 prepare target payload for sending to c8y
* step 5 prepare target payload for sending to c8y
*/
if (mapping.targetAPI.equals(API.INVENTORY)) {
ManagedObjectRepresentation attocDevice = null;
var newPredecessor = context.addRequest(
new C8YRequest(predecessor, RequestMethod.PATCH, device.value.toString(),
new C8YRequest(predecessor,
context.getMapping().updateExistingDevice ? RequestMethod.POST : RequestMethod.PATCH,
device.value.toString(),
mapping.externalIdType,
payloadTarget.jsonString(),
null, API.INVENTORY, null));
Expand All @@ -214,10 +170,11 @@ private ProcessingContext<byte[]> getBuildProcessingContext(ProcessingContext<by
ExternalIDRepresentation sourceId = c8yAgent.resolveExternalId2GlobalId(tenant,
identity, context);
context.setSourceId(sourceId.getManagedObject().getId().getValue());
attocDevice = c8yAgent.upsertDevice(tenant,
ManagedObjectRepresentation attocDevice = c8yAgent.upsertDevice(tenant,
identity, context);
var response = objectMapper.writeValueAsString(attocDevice);
context.getCurrentRequest().setResponse(response);
context.getCurrentRequest().setSourceId(attocDevice.getId().getValue());
} catch (Exception e) {
context.getCurrentRequest().setError(e);
}
Expand Down Expand Up @@ -250,4 +207,63 @@ private ProcessingContext<byte[]> getBuildProcessingContext(ProcessingContext<by
return context;
}

private void prepareAndSubstituteInPayload(ProcessingContext<byte[]> context, DocumentContext payloadTarget,
String pathTarget, MappingSubstitution.SubstituteValue substitute, C8YAgent c8yAgent) {
Mapping mapping = context.getMapping();
String tenant = context.getTenant();
if ((Mapping.IDENTITY + ".externalId").equals(pathTarget)) {
ID identity = new ID(mapping.externalIdType, substitute.value.toString());
MappingSubstitution.SubstituteValue sourceId = new MappingSubstitution.SubstituteValue(substitute.value,
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING);
if (!mapping.targetAPI.equals(API.INVENTORY)) {
var resolvedSourceId = c8yAgent.resolveExternalId2GlobalId(tenant, identity, context);
if (resolvedSourceId == null) {
if (mapping.createNonExistingDevice) {
sourceId.value = createAttocDevice(identity, context, c8yAgent);
}
} else {
sourceId.value = resolvedSourceId.getManagedObject().getId().getValue();
}
substituteValueInPayload(sourceId, payloadTarget, mapping.transformGenericPath2C8YPath(pathTarget));
context.setSourceId(sourceId.value.toString());
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
}
} else if ((Mapping.IDENTITY + ".c8ySourceId").equals(pathTarget)) {
MappingSubstitution.SubstituteValue sourceId = new MappingSubstitution.SubstituteValue(substitute.value,
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING);
// in this case the device needs to exists beforehand
substituteValueInPayload(sourceId, payloadTarget, mapping.transformGenericPath2C8YPath(pathTarget));
context.setSourceId(sourceId.value.toString());
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
} else {
substituteValueInPayload(substitute, payloadTarget, pathTarget);
}
}

private String createAttocDevice(ID identity, ProcessingContext<byte[]> context, C8YAgent c8yAgent) {
Map<String, Object> request = new HashMap<String, Object>();
request.put("name",
"device_" + identity.getType() + "_" + identity.getValue());
request.put(MappingRepresentation.MAPPING_GENERATED_TEST_DEVICE, null);
request.put("c8y_IsDevice", null);
request.put("com_cumulocity_model_Agent", null);
try {
var predecessor = context.getRequests().size();
var requestString = objectMapper.writeValueAsString(request);
context.addRequest(
new C8YRequest(predecessor,
context.getMapping().updateExistingDevice ? RequestMethod.POST : RequestMethod.PATCH, null,
context.getMapping().externalIdType, requestString, null, API.INVENTORY, null));
ManagedObjectRepresentation attocDevice = c8yAgent.upsertDevice(context.getTenant(),
identity, context);
var response = objectMapper.writeValueAsString(attocDevice);
context.getCurrentRequest().setResponse(response);
context.getCurrentRequest().setSourceId(attocDevice.getId().getValue());
return attocDevice.getId().getValue();
} catch (ProcessingException | JsonProcessingException e) {
context.getCurrentRequest().setError(e);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -202,35 +202,8 @@ private ProcessingContext<T> getBuildProcessingContext(ProcessingContext<T> cont
/*
* step 4 resolve externalIds to c8ySourceIds and create attroc devices
*/
ID identity;
// check if the targetPath == externalId and we need to resolve an external id
if ((Mapping.IDENTITY + ".externalId").equals(pathTarget)) {
identity = new ID(mapping.externalIdType, substitute.value.toString());
MappingSubstitution.SubstituteValue sourceId = new MappingSubstitution.SubstituteValue(substitute.value,
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING);
if (!mapping.targetAPI.equals(API.INVENTORY)) {
var resolvedSourceId = c8yAgent.resolveExternalId2GlobalId(tenant, identity, context);
if (resolvedSourceId == null) {
if (mapping.createNonExistingDevice) {
sourceId.value = createAttocDevice(identity, context);
}
} else {
sourceId.value = resolvedSourceId.getManagedObject().getId().getValue();
}
substituteValueInPayload(sourceId, payloadTarget, mapping.transformGenericPath2C8YPath(pathTarget));
context.setSourceId(sourceId.value.toString());
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
}
} else if ((Mapping.IDENTITY + ".c8ySourceId").equals(pathTarget)) {
MappingSubstitution.SubstituteValue sourceId = new MappingSubstitution.SubstituteValue(substitute.value,
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING);
// in this case the device needs to exists beforehand
substituteValueInPayload(sourceId, payloadTarget, mapping.transformGenericPath2C8YPath(pathTarget));
context.setSourceId(sourceId.value.toString());
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
} else {
substituteValueInPayload(substitute, payloadTarget, pathTarget);
}
prepareAndSubstituteInPayload(context, payloadTarget, pathTarget, substitute);
}
/*
* step 5 prepare target payload for sending to c8y
Expand Down Expand Up @@ -285,6 +258,39 @@ private ProcessingContext<T> getBuildProcessingContext(ProcessingContext<T> cont
return context;
}

private void prepareAndSubstituteInPayload(ProcessingContext<T> context, DocumentContext payloadTarget,
String pathTarget, MappingSubstitution.SubstituteValue substitute) {
Mapping mapping = context.getMapping();
String tenant = context.getTenant();
if ((Mapping.IDENTITY + ".externalId").equals(pathTarget)) {
ID identity = new ID(mapping.externalIdType, substitute.value.toString());
MappingSubstitution.SubstituteValue sourceId = new MappingSubstitution.SubstituteValue(substitute.value,
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING);
if (!mapping.targetAPI.equals(API.INVENTORY)) {
var resolvedSourceId = c8yAgent.resolveExternalId2GlobalId(tenant, identity, context);
if (resolvedSourceId == null) {
if (mapping.createNonExistingDevice) {
sourceId.value = createAttocDevice(identity, context);
}
} else {
sourceId.value = resolvedSourceId.getManagedObject().getId().getValue();
}
substituteValueInPayload(sourceId, payloadTarget, mapping.transformGenericPath2C8YPath(pathTarget));
context.setSourceId(sourceId.value.toString());
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
}
} else if ((Mapping.IDENTITY + ".c8ySourceId").equals(pathTarget)) {
MappingSubstitution.SubstituteValue sourceId = new MappingSubstitution.SubstituteValue(substitute.value,
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING);
// in this case the device needs to exists beforehand
substituteValueInPayload(sourceId, payloadTarget, mapping.transformGenericPath2C8YPath(pathTarget));
context.setSourceId(sourceId.value.toString());
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
} else {
substituteValueInPayload(substitute, payloadTarget, pathTarget);
}
}

protected String createAttocDevice(ID identity, ProcessingContext<T> context) {
Map<String, Object> request = new HashMap<String, Object>();
request.put("name",
Expand Down

0 comments on commit d529344

Please sign in to comment.