From dc842eedfefce8b74ab36a8f79bc9001c00482a3 Mon Sep 17 00:00:00 2001 From: Stephanie Neubauer Date: Mon, 16 Sep 2019 14:54:21 +0200 Subject: [PATCH] sw360(purl): using purl instead of coordinates Coordinates have been removed from the sw360 data model and purls have been added * removed coordinates to coordinates key mapping * added purls in sw360 release * represented by a map of string, string * keys are the antenna supported types of purl * test have been updated * documentation has been updated Signed-off-by: Stephanie Neubauer --- ...60CoordinateKeysToArtifactCoordinates.java | 91 ------------------- .../rest/resource/releases/SW360Release.java | 19 ++-- .../sw360/utils/SW360ReleaseAdapterUtils.java | 29 +++--- .../processors/SW360EnricherImpl.java | 32 ++----- .../workflow/generator/SW360UpdaterTest.java | 9 +- .../processors/SW360EnricherTest.java | 22 ++++- 6 files changed, 56 insertions(+), 146 deletions(-) delete mode 100644 modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/SW360CoordinateKeysToArtifactCoordinates.java diff --git a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/SW360CoordinateKeysToArtifactCoordinates.java b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/SW360CoordinateKeysToArtifactCoordinates.java deleted file mode 100644 index 61b9bf8a8..000000000 --- a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/SW360CoordinateKeysToArtifactCoordinates.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) Bosch Software Innovations GmbH 2019. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v2.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v20.html - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.eclipse.sw360.antenna.sw360.rest.resource; - -import org.eclipse.sw360.antenna.api.exceptions.ExecutionException; -import org.eclipse.sw360.antenna.model.artifact.facts.ArtifactCoordinates; -import org.eclipse.sw360.antenna.model.artifact.facts.GenericArtifactCoordinates; -import org.eclipse.sw360.antenna.model.artifact.facts.dotnet.DotNetCoordinates; -import org.eclipse.sw360.antenna.model.artifact.facts.java.BundleCoordinates; -import org.eclipse.sw360.antenna.model.artifact.facts.java.MavenCoordinates; -import org.eclipse.sw360.antenna.model.artifact.facts.javaScript.JavaScriptCoordinates; - -import java.lang.reflect.InvocationTargetException; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -public enum SW360CoordinateKeysToArtifactCoordinates { - MVN(MavenCoordinates.class, "mvn"), - DOTNET(DotNetCoordinates.class, "dotnet"), - BUNDLES(BundleCoordinates.class, "bundles"), - JAVASCRIPT(JavaScriptCoordinates.class, "javascript"), - GENERAL(GenericArtifactCoordinates.class, "general"); - - private final Class key; - private final String value; - - private static final List VALUES = Collections.unmodifiableList(Arrays.asList(values())); - - SW360CoordinateKeysToArtifactCoordinates(Class key, String value) { - this.key = key; - this.value = value; - } - - public static String get(Class key0) { - return VALUES.stream() - .filter(e -> e.key == key0) - .findFirst() - .map(e -> e.value) - .orElse(null); - } - - public static Class get(String value0) { - return VALUES.stream() - .filter(e -> e.value.equals(value0)) - .findFirst() - .map(e -> e.key) - .orElse(null); - } - - public static Set> getKeys() { - return VALUES.stream() - .map(e -> e.key) - .collect(Collectors.toSet()); - } - - public static Set getValues() { - return VALUES.stream() - .map(e -> e.value) - .collect(Collectors.toSet()); - } - - public static ArtifactCoordinates createArtifactCoordinates(String group, String name, String version, Class coordinateClass) { - try { - Object object = null; - if(coordinateClass == SW360CoordinateKeysToArtifactCoordinates.MVN.key){ - object = coordinateClass.getConstructors()[0].newInstance(name, group, version); - } else if (coordinateClass == SW360CoordinateKeysToArtifactCoordinates.DOTNET.key - || coordinateClass == SW360CoordinateKeysToArtifactCoordinates.BUNDLES.key - || coordinateClass == SW360CoordinateKeysToArtifactCoordinates.GENERAL.key) { - object = coordinateClass.getConstructors()[0].newInstance(name, version); - } else if (coordinateClass == SW360CoordinateKeysToArtifactCoordinates.JAVASCRIPT.key) { - object = coordinateClass.getConstructors()[0].newInstance(name, group, version); - } - return (ArtifactCoordinates) object; - } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { - throw new ExecutionException(); - } - } - -} diff --git a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/releases/SW360Release.java b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/releases/SW360Release.java index ed9b37708..87d1a7c02 100644 --- a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/releases/SW360Release.java +++ b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/rest/resource/releases/SW360Release.java @@ -13,12 +13,15 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import org.eclipse.sw360.antenna.sw360.rest.resource.*; +import org.eclipse.sw360.antenna.sw360.rest.resource.SW360HalResource; +import org.eclipse.sw360.antenna.sw360.rest.resource.SW360HalResourceUtility; +import org.eclipse.sw360.antenna.sw360.rest.resource.Self; import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360SparseLicense; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; public class SW360Release extends SW360HalResource { @@ -31,6 +34,10 @@ public class SW360Release extends SW360HalResource PURL_TYPE = Stream.of( + "bitbucket", "composer", "deb", "docker", + "gem", "generic", "github", "golang", "maven", + "npm", "nuget", "p2", "pypi", "rpm").collect(Collectors.toSet()); private String name; private String version; @@ -131,16 +138,16 @@ public void setDownloadurl(String downloadurl) { this.downloadurl = downloadurl; } - @JsonIgnore - public Map getCoordinates() { + public Map getPurls() { return externalIds.entrySet().stream() - .filter(e -> SW360CoordinateKeysToArtifactCoordinates.getValues().contains(e.getKey())) + .filter(e -> PURL_TYPE.contains(e.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } - public SW360Release setCoordinates(Map coordinates) { - externalIds.putAll(coordinates); + public SW360Release setPurls(Map purls) { + externalIds.putAll(purls); + return this; } diff --git a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/utils/SW360ReleaseAdapterUtils.java b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/utils/SW360ReleaseAdapterUtils.java index 2440cd6fd..490a66487 100644 --- a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/utils/SW360ReleaseAdapterUtils.java +++ b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/utils/SW360ReleaseAdapterUtils.java @@ -10,16 +10,19 @@ */ package org.eclipse.sw360.antenna.sw360.utils; +import com.github.packageurl.PackageURL; import org.eclipse.sw360.antenna.model.artifact.Artifact; import org.eclipse.sw360.antenna.model.artifact.facts.*; import org.eclipse.sw360.antenna.model.util.ArtifactLicenseUtils; import org.eclipse.sw360.antenna.model.xml.generated.LicenseInformation; -import org.eclipse.sw360.antenna.sw360.rest.resource.SW360CoordinateKeysToArtifactCoordinates; import org.eclipse.sw360.antenna.sw360.rest.resource.SW360HalResourceUtility; import org.eclipse.sw360.antenna.sw360.rest.resource.components.SW360Component; import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release; -import java.util.*; +import java.util.Collection; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; public class SW360ReleaseAdapterUtils { @@ -34,7 +37,7 @@ public static SW360Release prepareRelease(SW360Component component, Set release.setComponentId(componentId); release.setMainLicenseIds(sw360LicenseIds); - SW360ReleaseAdapterUtils.setCoordinates(release, artifact); + SW360ReleaseAdapterUtils.setPackageUrls(release, artifact); SW360ReleaseAdapterUtils.setFinalLicense(release, artifact); SW360ReleaseAdapterUtils.setDeclaredLicense(release, artifact); SW360ReleaseAdapterUtils.setObservedLicense(release, artifact); @@ -42,7 +45,7 @@ public static SW360Release prepareRelease(SW360Component component, Set SW360ReleaseAdapterUtils.setOriginalRepo(release, artifact); SW360ReleaseAdapterUtils.setSwhId(release, artifact); SW360ReleaseAdapterUtils.setHashes(release, artifact); - SW360ReleaseAdapterUtils.setClearingStatus(release,artifact); + SW360ReleaseAdapterUtils.setClearingStatus(release, artifact); SW360ReleaseAdapterUtils.setChangeStatus(release, artifact); SW360ReleaseAdapterUtils.setCopyrights(release, artifact); @@ -50,7 +53,7 @@ public static SW360Release prepareRelease(SW360Component component, Set } public static boolean isValidRelease(SW360Release release) { - if(release.getName() == null || release.getName().isEmpty()) { + if (release.getName() == null || release.getName().isEmpty()) { return false; } return release.getVersion() != null && !release.getVersion().isEmpty(); @@ -72,18 +75,12 @@ private static void setCPEId(SW360Release release, Artifact artifact) { .ifPresent(release::setCpeId); } - private static void setCoordinates(SW360Release release, Artifact artifact) { - release.setCoordinates(getMapOfCoordinates(artifact)); - } + private static void setPackageUrls(SW360Release sw360Release, Artifact artifact) { + Map packageURLS = artifact.askForAll(ArtifactCoordinates.class).stream() + .map(ArtifactCoordinates::getPurl) + .collect(Collectors.toMap(PackageURL::getType, PackageURL::canonicalize)); - private static Map getMapOfCoordinates(Artifact artifact) { - Map coordinates = new HashMap<>(); - artifact.askForAll(ArtifactCoordinates.class).forEach(coordinate -> - coordinates.put( - SW360CoordinateKeysToArtifactCoordinates.get(coordinate.getClass()), - coordinate.toString()) - ); - return coordinates; + sw360Release.setPurls(packageURLS); } private static void setFinalLicense(SW360Release release, Artifact artifact) { diff --git a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherImpl.java b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherImpl.java index 86aa9528d..38749166b 100644 --- a/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherImpl.java +++ b/modules/sw360/src/main/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherImpl.java @@ -16,12 +16,12 @@ import org.eclipse.sw360.antenna.model.artifact.facts.*; import org.eclipse.sw360.antenna.model.reporting.MessageType; import org.eclipse.sw360.antenna.model.util.ArtifactLicenseUtils; +import org.eclipse.sw360.antenna.model.util.ArtifactUtils; import org.eclipse.sw360.antenna.model.xml.generated.License; import org.eclipse.sw360.antenna.model.xml.generated.LicenseInformation; import org.eclipse.sw360.antenna.model.xml.generated.LicenseOperator; import org.eclipse.sw360.antenna.model.xml.generated.LicenseStatement; import org.eclipse.sw360.antenna.sw360.SW360MetaDataReceiver; -import org.eclipse.sw360.antenna.sw360.rest.resource.SW360CoordinateKeysToArtifactCoordinates; import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360License; import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360SparseLicense; import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release; @@ -72,7 +72,7 @@ private void addClearingStateIfAvailable(Artifact artifact, SW360Release release } private void mapExternalIdsOnSW360Release(SW360Release sw360Release, Artifact artifact) { - mapCoordinates(sw360Release) + mapCoordinatesFromPurls(sw360Release) .forEach(artifact::addFact); addLicenseFact(Optional.ofNullable(sw360Release.getDeclaredLicense()), artifact, DeclaredLicenseInformation::new); @@ -106,6 +106,12 @@ private void addLicenseFact(Optional licenseList, Artifact artifact, Fun .ifPresent(artifact::addFact); } + private Stream mapCoordinatesFromPurls(SW360Release sw360Release) { + final Map purls = sw360Release.getPurls(); + return purls.values().stream() + .map(ArtifactUtils::createArtifactCoordinatesFromPurl); + } + private License makeLicenseStatementFromString(String license) { License license1 = new License(); license1.setName(license); @@ -113,28 +119,6 @@ private License makeLicenseStatementFromString(String license) { return license1; } - private Stream mapCoordinates(SW360Release sw360Release) { - final Map coordinates = sw360Release.getCoordinates(); - return SW360CoordinateKeysToArtifactCoordinates.getKeys() - .stream() - .map(key -> { - String coordinateType = SW360CoordinateKeysToArtifactCoordinates.get(key); - final String coordinatesString = coordinates.get(coordinateType); - if(coordinatesString == null) { - return null; - } - - String[] splitCoordiantes = coordinatesString.split(":"); - - if (splitCoordiantes.length == 3) { - return SW360CoordinateKeysToArtifactCoordinates.createArtifactCoordinates(splitCoordiantes[0], sw360Release.getName(), sw360Release.getVersion(), key); - } else { - return SW360CoordinateKeysToArtifactCoordinates.createArtifactCoordinates("", sw360Release.getName(), sw360Release.getVersion(), key); - } - }) - .filter(Objects::nonNull); - } - private void addSourceUrlIfAvailable(Artifact artifact, SW360Release release) { if (release.getDownloadurl() != null && !release.getDownloadurl().isEmpty()) { artifact.addFact(new ArtifactSourceUrl(release.getDownloadurl())); diff --git a/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/generator/SW360UpdaterTest.java b/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/generator/SW360UpdaterTest.java index 76f289076..99e7cc90f 100644 --- a/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/generator/SW360UpdaterTest.java +++ b/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/generator/SW360UpdaterTest.java @@ -50,9 +50,10 @@ public void artifactIsMappedToSw360ReleaseCorrectly() { assertThat(release.getReleaseTagUrl()).isEqualTo(releaseTagUrl); assertThat(release.getSoftwareHeritageId()).isEqualTo(swhID); assertThat(new ArtifactSoftwareHeritageID.Builder(release.getSoftwareHeritageId()).build().toString()).isEqualTo(swhID); - assertThat(release.getCoordinates()).containsKeys("mvn"); - assertThat(release.getCoordinates()).hasSize(1); - assertThat(release.getCoordinates()).containsValue("org.group.id:artifactId(test1):1.2.3"); + + assertThat(release.getPurls()).hasSize(1); + assertThat(release.getPurls().containsKey("maven")).isTrue(); + assertThat(release.getPurls().containsValue("pkg:maven/org.group.id/artifactId_test1@1.2.3")).isTrue(); assertThat(release.getHashes()).hasSize(1); assertThat(release.getHashes()). @@ -104,7 +105,7 @@ public String getLinkStr() { }; Artifact artifact = new Artifact("JSON"); artifact.setProprietary(false); - artifact.addFact(new MavenCoordinates("artifactId(" + name + ")", "org.group.id", "1.2.3")); + artifact.addFact(new MavenCoordinates("artifactId_" + name, "org.group.id", "1.2.3")); artifact.addFact(new DeclaredLicenseInformation(licenseInformation)); artifact.addFact(new ObservedLicenseInformation(licenseInformation)); artifact.addFact(new ArtifactSourceUrl(sourceUrl)); diff --git a/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherTest.java b/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherTest.java index 1966c1f46..73f7cf141 100644 --- a/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherTest.java +++ b/modules/sw360/src/test/java/org/eclipse/sw360/antenna/sw360/workflow/processors/SW360EnricherTest.java @@ -11,6 +11,9 @@ package org.eclipse.sw360.antenna.sw360.workflow.processors; +import com.github.packageurl.MalformedPackageURLException; +import com.github.packageurl.PackageURL; +import com.github.packageurl.PackageURLBuilder; import org.eclipse.sw360.antenna.model.artifact.Artifact; import org.eclipse.sw360.antenna.model.artifact.facts.*; import org.eclipse.sw360.antenna.model.artifact.facts.java.MavenCoordinates; @@ -20,7 +23,6 @@ import org.eclipse.sw360.antenna.model.xml.generated.LicenseStatement; import org.eclipse.sw360.antenna.sw360.SW360MetaDataReceiver; import org.eclipse.sw360.antenna.sw360.rest.resource.LinkObjects; -import org.eclipse.sw360.antenna.sw360.rest.resource.SW360CoordinateKeysToArtifactCoordinates; import org.eclipse.sw360.antenna.sw360.rest.resource.Self; import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360License; import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360SparseLicense; @@ -76,7 +78,7 @@ public void setUp() { } @After - public void after(){ + public void after() { temporaryFolder.delete(); verify(toolConfigMock, atLeast(0)).getProxyHost(); @@ -85,7 +87,7 @@ public void after(){ @Test - public void releaseIsMappedToArtifactCorrectly() { + public void releaseIsMappedToArtifactCorrectly() throws MalformedPackageURLException { SW360Release release0 = mkSW360Release("test1"); release0.set_Embedded(new SW360ReleaseEmbedded()); release0.get_Embedded().setLicenses(Collections.emptyList()); @@ -97,6 +99,9 @@ public void releaseIsMappedToArtifactCorrectly() { assertThat(artifacts.size()).isEqualTo(1); Artifact artifact0 = artifacts.get(0); + assertThat(artifact0.askFor(MavenCoordinates.class).isPresent()).isTrue(); + assertThat(artifact0.askFor(MavenCoordinates.class).get().getVersion()).isEqualTo("1.2.3"); + assertThat(artifact0.askFor(ArtifactSourceUrl.class).isPresent()).isTrue(); assertThat(artifact0.askForGet(ArtifactSourceUrl.class).get()).isEqualTo(sourceUrl); assertThat(artifact0.askFor(ArtifactReleaseTagURL.class).isPresent()).isTrue(); @@ -227,7 +232,7 @@ private SW360License createLicenseFromSparseLicense(SW360SparseLicense sparseLic return license; } - private SW360Release mkSW360Release(String name) { + private SW360Release mkSW360Release(String name) throws MalformedPackageURLException { SW360Release sw360Release = new SW360Release(); sw360Release.setName(name); @@ -238,7 +243,14 @@ private SW360Release mkSW360Release(String name) { sw360Release.setDeclaredLicense("Apache-2.0"); sw360Release.setObservedLicense("A Test License"); - sw360Release.setCoordinates(Collections.singletonMap(SW360CoordinateKeysToArtifactCoordinates.get(MavenCoordinates.class), "test:test1:1.2.3")); + sw360Release.setPurls(Collections.singletonMap(PackageURL.StandardTypes.MAVEN, + PackageURLBuilder.aPackageURL() + .withName("test") + .withType("maven") + .withNamespace("test") + .withVersion("1.2.3") + .build() + .canonicalize())); sw360Release.setReleaseTagUrl(releaseTagUrl); sw360Release.setSoftwareHeritageId(swhID); sw360Release.setHashes(Collections.singleton(hashString));