-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
disable by default, add simple node directory
- Loading branch information
1 parent
3a8971f
commit 8641e42
Showing
12 changed files
with
295 additions
and
5 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft | ||
* | ||
* 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 | ||
*/ | ||
|
||
plugins { | ||
`maven-publish` | ||
`java-library` | ||
} | ||
|
||
dependencies { | ||
implementation(libs.edc.spi.core) | ||
implementation(libs.edc.fc.spi.crawler) | ||
|
||
testImplementation(libs.edc.junit) | ||
} |
80 changes: 80 additions & 0 deletions
80
...og/src/main/java/org/eclipse/tractusx/edc/federatedcatalog/FederatedCatalogExtension.java
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,80 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft | ||
* | ||
* 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.edc.federatedcatalog; | ||
|
||
import org.eclipse.edc.crawler.spi.TargetNode; | ||
import org.eclipse.edc.crawler.spi.TargetNodeDirectory; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Extension; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Inject; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Provider; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Setting; | ||
import org.eclipse.edc.spi.system.ServiceExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.edc.spi.types.TypeManager; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static org.eclipse.tractusx.edc.federatedcatalog.FederatedCatalogExtension.NAME; | ||
|
||
|
||
@Extension(value = NAME) | ||
public class FederatedCatalogExtension implements ServiceExtension { | ||
|
||
public static final String NAME = "Tractus-X Federated Catalog Extension"; | ||
|
||
@Setting(value = "File path to a JSON file containing TargetNode entries for the Federated Catalog Crawler") | ||
public static final String NODE_LIST_FILE = "tx.edc.catalog.node.list.file"; | ||
|
||
@Inject | ||
private TypeManager typeManager; | ||
|
||
|
||
@Override | ||
public String name() { | ||
return NAME; | ||
} | ||
|
||
@Provider | ||
public TargetNodeDirectory createFileBasedNodeDirectory(ServiceExtensionContext context) { | ||
return ofNullable(context.getConfig().getString(NODE_LIST_FILE, null)) | ||
.map(File::new) | ||
.map(f -> (TargetNodeDirectory) new FileBasedTargetNodeDirectory(f, context.getMonitor(), typeManager.getMapper())) | ||
.orElseGet(() -> { | ||
context.getMonitor().warning("No TargetNode file configured ('%s'). Federated Catalog Crawler will be inactive.".formatted(NODE_LIST_FILE)); | ||
return new NoopNodeDirectory(); | ||
}); | ||
|
||
} | ||
|
||
private static class NoopNodeDirectory implements TargetNodeDirectory { | ||
@Override | ||
public List<TargetNode> getAll() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public void insert(TargetNode targetNode) { | ||
|
||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...src/main/java/org/eclipse/tractusx/edc/federatedcatalog/FileBasedTargetNodeDirectory.java
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,72 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft | ||
* | ||
* 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.edc.federatedcatalog; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.eclipse.edc.crawler.spi.TargetNode; | ||
import org.eclipse.edc.crawler.spi.TargetNodeDirectory; | ||
import org.eclipse.edc.spi.EdcException; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* File-based implementation of the {@link TargetNodeDirectory} that returns a static {@code List<TargetNode>} that are | ||
* serialized as JSON and stored in a file. | ||
*/ | ||
class FileBasedTargetNodeDirectory implements TargetNodeDirectory { | ||
|
||
private static final TypeReference<List<TargetNode>> LIST_TYPE = new TypeReference<>() { | ||
}; | ||
private final File nodeFile; | ||
private final Monitor monitor; | ||
private final ObjectMapper objectMapper; | ||
private List<TargetNode> nodes; | ||
|
||
FileBasedTargetNodeDirectory(File nodeFile, Monitor monitor, ObjectMapper objectMapper) { | ||
|
||
this.nodeFile = nodeFile; | ||
this.monitor = monitor; | ||
this.objectMapper = objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); | ||
} | ||
|
||
@Override | ||
public List<TargetNode> getAll() { | ||
|
||
if (nodes == null) { | ||
try { | ||
nodes = objectMapper.readValue(nodeFile, LIST_TYPE); | ||
} catch (IOException e) { | ||
throw new EdcException(e); | ||
} | ||
} | ||
return nodes; | ||
|
||
} | ||
|
||
@Override | ||
public void insert(TargetNode targetNode) { | ||
monitor.warning("Inserting nodes into the file-based TargetNodeDirectory is not supported."); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...-catalog/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension
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,21 @@ | ||
################################################################################# | ||
# Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
# 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 | ||
################################################################################# | ||
|
||
org.eclipse.tractusx.edc.federatedcatalog.FederatedCatalogExtension |
63 changes: 63 additions & 0 deletions
63
...test/java/org/eclipse/tractusx/edc/federatedcatalog/FileBasedTargetNodeDirectoryTest.java
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,63 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft | ||
* | ||
* 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.edc.federatedcatalog; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.eclipse.edc.crawler.spi.TargetNode; | ||
import org.eclipse.edc.junit.testfixtures.TestUtils; | ||
import org.eclipse.edc.spi.EdcException; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class FileBasedTargetNodeDirectoryTest { | ||
|
||
@Test | ||
void getAll_hasEntries() { | ||
var nodeDir = new FileBasedTargetNodeDirectory(TestUtils.getFileFromResourceName("nodes.json"), mock(), new ObjectMapper()); | ||
assertThat(nodeDir.getAll()).hasSize(2); | ||
} | ||
|
||
@Test | ||
void getAll_fileNotExist() { | ||
var nodeDir = new FileBasedTargetNodeDirectory(new File("not-exist.json"), mock(), new ObjectMapper()); | ||
assertThatThrownBy(nodeDir::getAll) | ||
.isInstanceOf(EdcException.class) | ||
.hasRootCauseInstanceOf(FileNotFoundException.class); | ||
} | ||
|
||
@Test | ||
void insert() { | ||
var monitor = mock(Monitor.class); | ||
var nodeDir = new FileBasedTargetNodeDirectory(new File("not-exist.json"), monitor, new ObjectMapper()); | ||
|
||
assertThatNoException().isThrownBy(() -> nodeDir.insert(new TargetNode("foo", "bar", "https://foobar.com", List.of()))); | ||
verify(monitor).warning("Inserting nodes into the file-based TargetNodeDirectory is not supported."); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
edc-extensions/federated-catalog/src/test/resources/nodes.json
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,14 @@ | ||
[ | ||
{ | ||
"name": "test-1", | ||
"url": "http://nodes.com/test-1", | ||
"id": "1", | ||
"supportedProtocols": "test-protocol" | ||
}, | ||
{ | ||
"name": "test-2", | ||
"url": "http://nodes.com/test-2", | ||
"id": "2", | ||
"supportedProtocols": "another-test-protocol" | ||
} | ||
] |
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
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