Skip to content

Commit

Permalink
Merge pull request eclipse-tractusx#623 from catenax-ng/TRI-210-add-c…
Browse files Browse the repository at this point in the history
…ache-for-bpnl-endpoints

feat(impl):[210] added cache mechanism to ConnectorEndpointsService
  • Loading branch information
ds-psosnowski authored Nov 9, 2023
2 parents b208c6e + ca5b276 commit 56ac9e4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
2 changes: 2 additions & 0 deletions charts/irs-helm/templates/configmap-spring-app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ data:
operator: {{ .operator | quote }}
rightOperand: {{ .rightOperand | quote }}
{{- end }}
connectorEndpointService:
cacheTTL: {{ .Values.edc.connectorEndpointService.cacheTTL | int64 }}
ess:
localBpn: {{ tpl (.Values.bpn | default "") . | quote }}
localEdcEndpoint: {{ tpl (.Values.edc.provider.host | default "") . | quote }}
Expand Down
3 changes: 2 additions & 1 deletion charts/irs-helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ edc:
- leftOperand: "Membership"
operator: "eq"
rightOperand: "active"

connectorEndpointService:
cacheTTL: 86400000
discovery:
oAuthClientId: portal # ID of the OAuth2 client registration to use, see config spring.security.oauth2.client

Expand Down
2 changes: 2 additions & 0 deletions irs-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ irs-edc-client:
- leftOperand: "Membership"
operator: "eq"
rightOperand: "active"
connectorEndpointService:
cacheTTL: 86400000

digitalTwinRegistry:
type: ${DIGITALTWINREGISTRY_TYPE:decentral} # The type of DTR. This can be either "central" or "decentral". If "decentral", descriptorEndpoint, shellLookupEndpoint and oAuthClientId is not required.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/********************************************************************************
* Copyright (c) 2021,2022,2023
* 2022: ZF Friedrichshafen AG
* 2022: ISTOS GmbH
* 2022,2023: Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* 2022,2023: BOSCH AG
* Copyright (c) 2021,2022,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.irs;

import org.assertj.core.api.Assertions;
import org.eclipse.tractusx.irs.registryclient.discovery.ConnectorEndpointsService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "digitalTwinRegistry.type=central" })
@ActiveProfiles(profiles = "test")
@Import(TestConfig.class)
@ExtendWith({ MockitoExtension.class,
SpringExtension.class
})
class ConnectorEndpointServiceTest {

@Autowired
ConnectorEndpointsService connectorEndpointsService;

@Autowired
CacheManager cacheManager;

@Test
void shouldGenerateCacheRecordWhenFetchConnectorEndpointsCalled() {
// given
final String bpnRecord = "";

// when
connectorEndpointsService.fetchConnectorEndpoints(bpnRecord);

// then
final Cache connectorEndpointServiceCache = cacheManager.getCache("connector_endpoint_service_cache");
Assertions.assertThat(connectorEndpointServiceCache.get(bpnRecord)).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;

/**
* Connector Endpoints service to find connectors in Discovery Finder
Expand All @@ -39,7 +42,9 @@
public class ConnectorEndpointsService {

private final DiscoveryFinderClient discoveryFinderClient;
private static final String CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME = "connector_endpoint_service_cache";

@Cacheable(CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME)
public List<String> fetchConnectorEndpoints(final String bpn) {
if (StringUtils.isBlank(bpn)) {
log.warn("BPN was null, cannot search for any connector endpoints. Returning empty list.");
Expand All @@ -65,4 +70,9 @@ public List<String> fetchConnectorEndpoints(final String bpn) {
return endpoints;
}

@CacheEvict(value = CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME, allEntries = true)
@Scheduled(fixedRateString = "${irs-edc-client.connectorEndpointService.cacheTTL}")
public void evictCachesValues() {
log.debug("Clearing \"{}\" cache.", CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME);
}
}

0 comments on commit 56ac9e4

Please sign in to comment.