-
Notifications
You must be signed in to change notification settings - Fork 16
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
Feat: Implement Cleaning Logic on Reserve Task #516
Merged
nicoprow
merged 5 commits into
eclipse-tractusx:main
from
catenax-ng:feat/cleaning_service/Create_cleaning_result
Oct 18, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9c6b25
feat(Cleaning Service): Add logic for cleaning data
fabiodmota dd7a3db
feat(Cleaning Service): Add unit test
fabiodmota 3bc7778
feat(Cleaning Service): Fix comments to PR and add unit test for poss…
fabiodmota 50de80f
feat(Cleaning Service): Change Logic on Address Creation
fabiodmota 96bc9a7
feat(Cleaning Service): Add unit testing for variations and rest api …
fabiodmota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...-service-dummy/src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/config/ClientsConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021,2023 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 Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
******************************************************************************/ | ||
|
||
package org.eclipse.tractusx.bpdm.cleaning.config | ||
|
||
|
||
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient | ||
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClientImpl | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.MediaType | ||
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository | ||
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import java.util.function.Consumer | ||
|
||
|
||
@Configuration | ||
class ClientsConfig { | ||
|
||
|
||
@Bean | ||
@ConditionalOnProperty( | ||
value = ["bpdm.orchestrator.security-enabled"], | ||
havingValue = "false", | ||
matchIfMissing = true | ||
) | ||
fun orchestratorClientNoAuth(poolConfigProperties: OrchestratorConfigProperties): OrchestrationApiClient { | ||
val url = poolConfigProperties.baseUrl | ||
return OrchestrationApiClientImpl { webClientBuilder(url).build() } | ||
} | ||
|
||
|
||
@Bean | ||
@ConditionalOnProperty( | ||
value = ["bpdm.orchestrator.security-enabled"], | ||
havingValue = "true" | ||
) | ||
fun orchestratorClientWithAuth( | ||
poolConfigProperties: OrchestratorConfigProperties, | ||
clientRegistrationRepository: ClientRegistrationRepository, | ||
authorizedClientService: OAuth2AuthorizedClientService | ||
): OrchestrationApiClient { | ||
val url = poolConfigProperties.baseUrl | ||
val clientRegistrationId = poolConfigProperties.oauth2ClientRegistration | ||
?: throw IllegalArgumentException("bpdm.orchestrator.oauth2-client-registration is required if bpdm.orchestrator.security-enabled is set") | ||
return OrchestrationApiClientImpl { | ||
webClientBuilder(url) | ||
.apply(oauth2Configuration(clientRegistrationRepository, authorizedClientService, clientRegistrationId)) | ||
.build() | ||
} | ||
} | ||
|
||
|
||
private fun webClientBuilder(url: String) = | ||
WebClient.builder() | ||
.baseUrl(url) | ||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
|
||
private fun oauth2Configuration( | ||
clientRegistrationRepository: ClientRegistrationRepository, | ||
authorizedClientService: OAuth2AuthorizedClientService, | ||
clientRegistrationId: String | ||
): Consumer<WebClient.Builder> { | ||
val authorizedClientManager = authorizedClientManager(clientRegistrationRepository, authorizedClientService) | ||
val oauth = ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager) | ||
oauth.setDefaultClientRegistrationId(clientRegistrationId) | ||
return oauth.oauth2Configuration() | ||
} | ||
|
||
private fun authorizedClientManager( | ||
clientRegistrationRepository: ClientRegistrationRepository, | ||
authorizedClientService: OAuth2AuthorizedClientService | ||
): OAuth2AuthorizedClientManager { | ||
val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder().clientCredentials().build() | ||
val authorizedClientManager = AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientService) | ||
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider) | ||
return authorizedClientManager | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/config/OrchestratorConfigProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021,2023 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 Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
******************************************************************************/ | ||
|
||
package org.eclipse.tractusx.bpdm.cleaning.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
|
||
@ConfigurationProperties(prefix = "bpdm.orchestrator") | ||
data class OrchestratorConfigProperties( | ||
val baseUrl: String = "http://localhost:8085/", | ||
val securityEnabled: Boolean = false, | ||
val oauth2ClientRegistration: String? | ||
) |
157 changes: 157 additions & 0 deletions
157
...-dummy/src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceDummy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021,2023 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 Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
******************************************************************************/ | ||
|
||
package org.eclipse.tractusx.bpdm.cleaning.service | ||
|
||
|
||
import mu.KotlinLogging | ||
import org.eclipse.tractusx.bpdm.common.dto.AddressType | ||
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient | ||
import org.eclipse.tractusx.orchestrator.api.model.* | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Service | ||
import java.util.* | ||
|
||
@Service | ||
class CleaningServiceDummy( | ||
private val orchestrationApiClient: OrchestrationApiClient, | ||
|
||
) { | ||
|
||
private val logger = KotlinLogging.logger { } | ||
|
||
|
||
@Scheduled(cron = "\${cleaningService.pollingCron:-}", zone = "UTC") | ||
fun pollForCleaningTasks() { | ||
try { | ||
logger.info { "Starting polling for cleaning tasks from Orchestrator..." } | ||
|
||
// Step 1: Fetch and reserve the next cleaning request | ||
val cleaningRequest = orchestrationApiClient.goldenRecordTasks | ||
.reserveTasksForStep(TaskStepReservationRequest(amount = 10, TaskStep.CleanAndSync)) | ||
|
||
val cleaningTasks = cleaningRequest.reservedTasks | ||
|
||
logger.info { "${cleaningTasks.size} tasks found for cleaning. Proceeding with cleaning..." } | ||
|
||
if (cleaningTasks.isNotEmpty()) { | ||
|
||
val cleaningResults = cleaningTasks.map { reservedTask -> | ||
// Step 2: Generate dummy cleaning results | ||
processCleaningTask(reservedTask) | ||
} | ||
|
||
// Step 3: Send the cleaning result back to the Orchestrator | ||
orchestrationApiClient.goldenRecordTasks.resolveStepResults(TaskStepResultRequest(cleaningResults)) | ||
logger.info { "Cleaning tasks processing completed for this iteration." } | ||
} | ||
} catch (e: Exception) { | ||
logger.error(e) { "Error while processing cleaning task" } | ||
} | ||
} | ||
|
||
fun processCleaningTask(reservedTask: TaskStepReservationEntryDto): TaskStepResultEntryDto { | ||
val genericBusinessPartner = reservedTask.businessPartner.generic | ||
|
||
val addressPartner = createAddressRepresentation(genericBusinessPartner) | ||
|
||
val addressType = genericBusinessPartner.postalAddress.addressType ?: AddressType.AdditionalAddress | ||
|
||
val legalEntityDto = createLegalEntityRepresentation(addressPartner, addressType, genericBusinessPartner) | ||
|
||
val siteDto = createSiteDtoIfNeeded(genericBusinessPartner, addressPartner) | ||
|
||
val addressDto = shouldCreateAddress(addressType, addressPartner) | ||
|
||
return TaskStepResultEntryDto(reservedTask.taskId, BusinessPartnerFullDto(genericBusinessPartner, legalEntityDto, siteDto, addressDto)) | ||
} | ||
|
||
private fun shouldCreateAddress( | ||
addressType: AddressType, | ||
addressPartner: LogisticAddressDto | ||
): LogisticAddressDto? { | ||
val addressDto = if (addressType == AddressType.AdditionalAddress) { | ||
addressPartner | ||
} else { | ||
null | ||
} | ||
return addressDto | ||
} | ||
|
||
fun createSiteDtoIfNeeded(businessPartner: BusinessPartnerGenericDto, addressPartner: LogisticAddressDto): SiteDto? { | ||
if (!shouldCreateSite(businessPartner)) return null | ||
|
||
val siteAddressReference = when (businessPartner.postalAddress.addressType) { | ||
AddressType.SiteMainAddress, AddressType.LegalAndSiteMainAddress -> addressPartner.bpnAReference | ||
else -> generateNewBpnRequestIdentifier() | ||
} | ||
fabiodmota marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val siteMainAddress = addressPartner.copy(bpnAReference = siteAddressReference) | ||
return createSiteRepresentation(businessPartner, siteMainAddress) | ||
} | ||
|
||
fun createLegalEntityRepresentation( | ||
addressPartner: LogisticAddressDto, | ||
addressType: AddressType, | ||
genericPartner: BusinessPartnerGenericDto | ||
): LegalEntityDto { | ||
val legalAddressBpnReference = if (addressType == AddressType.LegalAddress || addressType == AddressType.LegalAndSiteMainAddress) { | ||
addressPartner.bpnAReference | ||
} else { | ||
generateNewBpnRequestIdentifier() | ||
} | ||
|
||
val legalAddress = addressPartner.copy(bpnAReference = legalAddressBpnReference) | ||
|
||
val bpnReferenceDto = createBpnReference(genericPartner.bpnL) | ||
|
||
return genericPartner.toLegalEntityDto(bpnReferenceDto, legalAddress) | ||
|
||
} | ||
|
||
fun createAddressRepresentation(genericPartner: BusinessPartnerGenericDto): LogisticAddressDto { | ||
val bpnReferenceDto = createBpnReference(genericPartner.bpnA) | ||
return genericPartner.toLogisticAddressDto(bpnReferenceDto) | ||
} | ||
|
||
fun createSiteRepresentation(genericPartner: BusinessPartnerGenericDto, siteAddressReference: LogisticAddressDto): SiteDto { | ||
val legalName = genericPartner.nameParts.joinToString(" ") | ||
val bpnReferenceDto = createBpnReference(genericPartner.bpnS) | ||
return genericPartner.toSiteDto(bpnReferenceDto, legalName, siteAddressReference) | ||
} | ||
|
||
fun createBpnReference(bpn: String?): BpnReferenceDto { | ||
return if (bpn != null) { | ||
BpnReferenceDto(bpn, BpnReferenceType.Bpn) | ||
} else { | ||
// Generate a new UUID and create a BpnReferenceDto object if bpnL/bpnS/bpnA is null | ||
generateNewBpnRequestIdentifier() | ||
} | ||
} | ||
|
||
private fun generateNewBpnRequestIdentifier() = BpnReferenceDto(UUID.randomUUID().toString(), BpnReferenceType.BpnRequestIdentifier) | ||
|
||
fun shouldCreateSite(genericPartner: BusinessPartnerGenericDto): Boolean { | ||
return genericPartner.postalAddress.addressType == AddressType.SiteMainAddress || | ||
genericPartner.postalAddress.addressType == AddressType.LegalAndSiteMainAddress || | ||
genericPartner.bpnS != null | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic diagram can be seen on this #433 (comment)