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

Feature/local parameter #1323

Draft
wants to merge 6 commits into
base: feature/distributed-create-entity
Choose a base branch
from
Draft
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
2 changes: 0 additions & 2 deletions search-service/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( entityUri: URI, ngsiLdAttributes: List&lt;NgsiLdAttribute&gt;, expandedAttributes: ExpandedAttributes, disallowOverwrite: Boolean, createdAt: ZonedDateTime, sub: Sub? )</ID>
<ID>LongParameterList:TemporalEntityHandler.kt$TemporalEntityHandler$( @RequestHeader httpHeaders: HttpHeaders, @PathVariable entityId: URI, @PathVariable attrId: String, @PathVariable instanceId: URI, @RequestBody requestBody: Mono&lt;String&gt;, @AllowedParameters(notImplemented = [QP.LOCAL, QP.VIA]) @RequestParam queryParams: MultiValueMap&lt;String, String&gt; )</ID>
<ID>LongParameterList:V0_29__JsonLd_migration.kt$V0_29__JsonLd_migration$( entityId: URI, attributeName: ExpandedTerm, datasetId: URI?, attributePayload: ExpandedAttributeInstance, ngsiLdAttributeInstance: NgsiLdAttributeInstance, defaultCreatedAt: ZonedDateTime )</ID>
<ID>MaxLineLength:DistributedEntityConsumptionServiceTests.kt$DistributedEntityConsumptionServiceTests$fun</ID>
<ID>MaximumLineLength:DistributedEntityConsumptionServiceTests.kt$DistributedEntityConsumptionServiceTests$</ID>
<ID>NestedBlockDepth:V0_29__JsonLd_migration.kt$V0_29__JsonLd_migration$override fun migrate(context: Context)</ID>
<ID>SwallowedException:TemporalQueryUtils.kt$e: IllegalArgumentException</ID>
</CurrentIssues>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class EntityHandler(
suspend fun create(
@RequestHeader httpHeaders: HttpHeaders,
@RequestBody requestBody: Mono<String>,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@AllowedParameters(implemented = [QueryParameter.LOCAL], notImplemented = [QueryParameter.VIA])
@RequestParam queryParams: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
Expand All @@ -94,8 +94,11 @@ class EntityHandler(
val expandedEntity = expandJsonLdEntity(body, contexts)
expandedEntity.toNgsiLdEntity().bind()

val (distributionStatuses, remainingEntity) = distributedEntityProvisionService
.distributeCreateEntity(expandedEntity, contexts)
val (distributionStatuses, remainingEntity) =
if (queryParams.getFirst(QP.LOCAL.key)?.toBoolean() != true) {
distributedEntityProvisionService
.distributeCreateEntity(expandedEntity, contexts)
} else emptyList<DistributionStatus>() to expandedEntity

val allStatuses = if (remainingEntity != null) {
val localStatus: DistributionStatus = either {
Expand Down Expand Up @@ -226,9 +229,9 @@ class EntityHandler(
implemented = [
QP.OPTIONS, QP.FORMAT, QP.COUNT, QP.OFFSET, QP.LIMIT, QP.ID, QP.TYPE, QP.ID_PATTERN, QP.ATTRS, QP.Q,
QP.GEOMETRY, QP.GEOREL, QP.COORDINATES, QP.GEOPROPERTY, QP.GEOMETRY_PROPERTY,
QP.LANG, QP.SCOPEQ, QP.CONTAINED_BY, QP.JOIN, QP.JOIN_LEVEL, QP.DATASET_ID,
QP.LANG, QP.SCOPEQ, QP.CONTAINED_BY, QP.JOIN, QP.JOIN_LEVEL, QP.DATASET_ID, QP.LOCAL
],
notImplemented = [QP.PICK, QP.OMIT, QP.EXPAND_VALUES, QP.CSF, QP.ENTITY_MAP, QP.LOCAL, QP.VIA]
notImplemented = [QP.PICK, QP.OMIT, QP.EXPAND_VALUES, QP.CSF, QP.ENTITY_MAP, QP.VIA]
)
@RequestParam queryParams: MultiValueMap<String, String>
): ResponseEntity<*> = either {
Expand All @@ -240,35 +243,38 @@ class EntityHandler(
val entitiesQuery = composeEntitiesQueryFromGet(applicationProperties.pagination, queryParams, contexts).bind()
.validateMinimalQueryEntitiesParameters().bind()

val (entities, localCount) = entityQueryService.queryEntities(entitiesQuery, sub.getOrNull()).bind()
val (localExpandedEntities, localCount) =
entityQueryService.queryEntities(entitiesQuery, sub.getOrNull()).bind()

val filteredEntities = entities.filterAttributes(entitiesQuery.attrs, entitiesQuery.datasetId)
val filteredEntities = localExpandedEntities.filterAttributes(entitiesQuery.attrs, entitiesQuery.datasetId)

val localEntities =
compactEntities(filteredEntities, contexts).let {
linkedEntityService.processLinkedEntities(it, entitiesQuery, sub.getOrNull()).bind()
}

val (queryWarnings, remoteEntitiesWithCSR, remoteCounts) =
distributedEntityConsumptionService.distributeQueryEntitiesOperation(
entitiesQuery,
httpHeaders,
queryParams
)

val maxCount = (remoteCounts + localCount).maxBy { it ?: 0 } ?: 0

val (warnings, mergedEntities) = ContextSourceUtils.mergeEntitiesLists(
localEntities,
remoteEntitiesWithCSR
).toPair().let { (mergeWarnings, mergedEntities) ->
val warnings = mergeWarnings?.let { queryWarnings + it } ?: queryWarnings
warnings to (mergedEntities ?: emptyList())
}
val (warnings, entities, count) =
if (queryParams.getFirst(QP.LOCAL.key)?.toBoolean() != true) {
val (queryWarnings, remoteEntitiesWithCSR, remoteCounts) =
distributedEntityConsumptionService.distributeQueryEntitiesOperation(
entitiesQuery,
httpHeaders,
queryParams
)
val maxCount = (remoteCounts + localCount).maxBy { it ?: 0 } ?: 0
val (warnings, mergedEntities) = ContextSourceUtils.mergeEntitiesLists(
localEntities,
remoteEntitiesWithCSR
).toPair().let { (mergeWarnings, mergedEntities) ->
val warnings = mergeWarnings?.let { queryWarnings + it } ?: queryWarnings
warnings to (mergedEntities ?: emptyList())
}
Triple(warnings, mergedEntities, maxCount)
} else Triple(emptyList(), localEntities, localCount)

buildQueryResponse(
mergedEntities.toFinalRepresentation(ngsiLdDataRepresentation),
maxCount,
entities.toFinalRepresentation(ngsiLdDataRepresentation),
count,
"/ngsi-ld/v1/entities",
entitiesQuery.paginationQuery,
queryParams,
Expand All @@ -290,9 +296,9 @@ class EntityHandler(
@AllowedParameters(
implemented = [
QP.OPTIONS, QP.FORMAT, QP.TYPE, QP.ATTRS, QP.GEOMETRY_PROPERTY,
QP.LANG, QP.CONTAINED_BY, QP.JOIN, QP.JOIN_LEVEL, QP.DATASET_ID,
QP.LANG, QP.CONTAINED_BY, QP.JOIN, QP.JOIN_LEVEL, QP.DATASET_ID, QP.LOCAL
],
notImplemented = [QP.PICK, QP.OMIT, QP.ENTITY_MAP, QP.LOCAL, QP.VIA]
notImplemented = [QP.PICK, QP.OMIT, QP.ENTITY_MAP, QP.VIA]
)
@RequestParam queryParams: MultiValueMap<String, String>
): ResponseEntity<*> = either {
Expand All @@ -316,26 +322,30 @@ class EntityHandler(
compactEntity(filteredExpandedEntity, contexts)
}

val (warnings, remoteEntitiesWithCSR) = distributedEntityConsumptionService.distributeRetrieveEntityOperation(
entityId,
httpHeaders,
queryParams
).let { (warnings, it) -> warnings.toMutableList() to it }

val (mergeWarnings, mergedEntity) = ContextSourceUtils.mergeEntities(
localEntity.getOrNull(),
remoteEntitiesWithCSR
).toPair()

mergeWarnings?.let { warnings.addAll(it) }

if (mergedEntity == null) {
val (entity, warnings) =
if (queryParams.getFirst(QP.LOCAL.key)?.toBoolean() != true) {
val (warnings, remoteEntitiesWithCSR) = distributedEntityConsumptionService
.distributeRetrieveEntityOperation(
entityId,
httpHeaders,
queryParams
).let { (warnings, it) -> warnings.toMutableList() to it }
val (mergeWarnings, mergedEntity) = ContextSourceUtils.mergeEntities(
localEntity.getOrNull(),
remoteEntitiesWithCSR
).toPair()

mergeWarnings?.let { warnings.addAll(it) }
mergedEntity to warnings.toList()
} else localEntity.getOrNull() to emptyList()

if (entity == null) {
val localError = localEntity.leftOrNull()
return localError!!.toErrorResponse().addWarnings(warnings)
}

val mergedEntityWithLinkedEntities =
linkedEntityService.processLinkedEntities(mergedEntity, entitiesQuery, sub.getOrNull()).bind()
linkedEntityService.processLinkedEntities(entity, entitiesQuery, sub.getOrNull()).bind()

prepareGetSuccessResponseHeaders(mediaType, contexts)
.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class EntityHandlerTests {
"""
{
"type": "https://uri.etsi.org/ngsi-ld/errors/InvalidRequest",
"title": "The ['invalid'] parameters are not allowed on this endpoint. This endpoint does not accept any query parameters. ",
"title": "The ['invalid'] parameters are not allowed on this endpoint. Accepted query parameters are 'local'. ",
"detail": "$DEFAULT_DETAIL"
}
""".trimIndent()
Expand All @@ -348,15 +348,15 @@ class EntityHandlerTests {
val jsonLdFile = ClassPathResource("/ngsild/aquac/breedingService.jsonld")

webClient.post()
.uri("/ngsi-ld/v1/entities?local=true")
.uri("/ngsi-ld/v1/entities?Via=true")
.bodyValue(jsonLdFile)
.exchange()
.expectStatus().isEqualTo(501)
.expectBody().json(
"""
{
"type": "https://uri.etsi.org/ngsi-ld/errors/NotImplemented",
"title": "The ['local'] parameters have not been implemented yet. This endpoint does not accept any query parameters. ",
"title": "The ['Via'] parameters have not been implemented yet. Accepted query parameters are 'local'. ",
"detail": "$DEFAULT_DETAIL"
}
""".trimIndent()
Expand Down
Loading