diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/BdioNodeFactory.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/BdioNodeFactory.java new file mode 100644 index 0000000000..bd85b972cf --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/BdioNodeFactory.java @@ -0,0 +1,127 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.UUID; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.synopsys.integration.detectable.detectable.dependency.model.BdioBillOfMaterials; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioComponent; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioCreationInfo; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioExternalIdentifier; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioId; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioProject; +import com.synopsys.integration.detectable.detectable.dependency.model.SpdxCreator; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public class BdioNodeFactory { + public static final String UNKNOWN_LIBRARY_VERSION = "UnknownVersion"; + + private static final String VERSION_RESOURCE_PATH = "com/synopsys/integration/bdio/version.txt"; + + private final BdioPropertyHelper bdioPropertyHelper; + + public BdioNodeFactory(final BdioPropertyHelper bdioPropertyHelper) { + this.bdioPropertyHelper = bdioPropertyHelper; + } + + public BdioBillOfMaterials createBillOfMaterials(final String projectName, final String projectVersion) { + final String codeLocationName = String.format("%s/%s Black Duck I/O Export", projectName, projectVersion); + + return createBillOfMaterials(codeLocationName, projectName, projectVersion); + } + + public BdioBillOfMaterials createBillOfMaterials(final String codeLocationName, final String projectName, final String projectVersion) { + final BdioBillOfMaterials billOfMaterials = new BdioBillOfMaterials(); + billOfMaterials.id = BdioId.createFromUUID(UUID.randomUUID().toString()); + if (StringUtils.isNotBlank(codeLocationName)) { + billOfMaterials.spdxName = codeLocationName; + } else { + billOfMaterials.spdxName = String.format("%s/%s Black Duck I/O Export", projectName, projectVersion); + } + billOfMaterials.bdioSpecificationVersion = "1.1.0"; + + billOfMaterials.creationInfo = new BdioCreationInfo(); + billOfMaterials.creationInfo.created = Instant.now().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT); + String version = BdioNodeFactory.UNKNOWN_LIBRARY_VERSION; + try (final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(BdioNodeFactory.VERSION_RESOURCE_PATH)) { + if (inputStream != null) { + version = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + } + } catch (final IOException ignored) { + // Library version is not critical to BdioBillOfMaterials creation. + } + billOfMaterials.creationInfo.addSpdxCreator(SpdxCreator.createToolSpdxCreator("IntegrationBdio", version)); + + return billOfMaterials; + } + + public BdioProject createProject(final String projectName, final String projectVersion, final BdioId bdioId, final ExternalId externalId) { + final BdioExternalIdentifier externalIdentifier = bdioPropertyHelper.createExternalIdentifier(externalId); + return createProject(projectName, projectVersion, bdioId, externalIdentifier); + } + + public BdioProject createProject(final String projectName, final String projectVersion, final BdioId bdioId, final BdioExternalIdentifier externalIdentifier) { + final BdioProject project = createProject(projectName, projectVersion, bdioId); + project.bdioExternalIdentifier = externalIdentifier; + + return project; + } + + public BdioProject createProject(final String projectName, final String projectVersion) { + return createProject(projectName, projectVersion, BdioId.createFromPieces(projectName, projectVersion)); + } + + public BdioProject createProject(final String projectName, final String projectVersion, final BdioId bdioId) { + final BdioProject project = new BdioProject(); + project.id = bdioId; + project.name = projectName; + project.version = projectVersion; + + return project; + } + + public BdioComponent createComponent(final String componentName, final String componentVersion, final ExternalId externalId) { + final BdioExternalIdentifier externalIdentifier = bdioPropertyHelper.createExternalIdentifier(externalId); + return createComponent(componentName, componentVersion, externalId.createBdioId(), externalIdentifier); + } + + public BdioComponent createComponent(final String componentName, final String componentVersion, final BdioId bdioId, final BdioExternalIdentifier externalIdentifier) { + final BdioComponent component = new BdioComponent(); + component.id = bdioId; + component.name = componentName; + component.version = componentVersion; + component.bdioExternalIdentifier = externalIdentifier; + + return component; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/BdioPropertyHelper.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/BdioPropertyHelper.java new file mode 100644 index 0000000000..82b1682265 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/BdioPropertyHelper.java @@ -0,0 +1,54 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency; + +import java.util.List; + +import com.synopsys.integration.detectable.detectable.dependency.model.BdioExternalIdentifier; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioNode; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioRelationship; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public class BdioPropertyHelper { + public void addRelationships(final BdioNode node, final List children) { + for (final BdioNode child : children) { + addRelationship(node, child); + } + } + + public void addRelationship(final BdioNode node, final BdioNode child) { + final BdioRelationship relationship = new BdioRelationship(); + relationship.related = child.id; + relationship.relationshipType = "DYNAMIC_LINK"; + node.relationships.add(relationship); + } + + public BdioExternalIdentifier createExternalIdentifier(final ExternalId externalId) { + final BdioExternalIdentifier externalIdentifier = new BdioExternalIdentifier(); + externalIdentifier.externalId = externalId.createExternalId(); + externalIdentifier.forge = externalId.getForge().getName(); + externalIdentifier.externalIdMetaData = externalId; + return externalIdentifier; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraph.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraph.java new file mode 100644 index 0000000000..7d3508f711 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraph.java @@ -0,0 +1,57 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.graph; + +import java.util.Set; + +import com.synopsys.integration.detectable.detectable.dependency.model.dependency.Dependency; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public interface DependencyGraph { + Set getRootDependencies(); + + Set getRootDependencyExternalIds(); + + boolean hasDependency(Dependency dependency); + + boolean hasDependency(ExternalId dependency); + + Dependency getDependency(ExternalId dependency); + + Set getChildrenForParent(Dependency parent); + + Set getChildrenExternalIdsForParent(Dependency parent); + + Set getChildrenForParent(ExternalId parent); + + Set getChildrenExternalIdsForParent(ExternalId parent); + + Set getParentExternalIdsForChild(Dependency child); + + Set getParentsForChild(ExternalId child); + + Set getParentsForChild(Dependency child); + + Set getParentExternalIdsForChild(ExternalId child); + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraphCombiner.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraphCombiner.java new file mode 100644 index 0000000000..fcb39900b4 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraphCombiner.java @@ -0,0 +1,58 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.graph; + +import java.util.HashSet; +import java.util.Set; + +import com.synopsys.integration.detectable.detectable.dependency.model.dependency.Dependency; + +public class DependencyGraphCombiner { + public void addGraphAsChildrenToRoot(final MutableDependencyGraph destinationGraph, final DependencyGraph sourceGraph) { + final Set encountered = new HashSet<>(); + for (final Dependency dependency : sourceGraph.getRootDependencies()) { + destinationGraph.addChildToRoot(dependency); + copyDependencyFromGraph(destinationGraph, dependency, sourceGraph, encountered); + } + } + + public void addGraphAsChildrenToParent(final MutableDependencyGraph destinationGraph, final Dependency parent, final DependencyGraph sourceGraph) { + final Set encountered = new HashSet<>(); + for (final Dependency dependency : sourceGraph.getRootDependencies()) { + destinationGraph.addChildWithParent(dependency, parent); + copyDependencyFromGraph(destinationGraph, dependency, sourceGraph, encountered); + } + } + + public void copyDependencyFromGraph(final MutableDependencyGraph destinationGraph, final Dependency parentDependency, final DependencyGraph sourceGraph, final Set encountered) { + for (final Dependency dependency : sourceGraph.getChildrenForParent(parentDependency)) { + if (!encountered.contains(dependency)) { + encountered.add(dependency); + + copyDependencyFromGraph(destinationGraph, dependency, sourceGraph, encountered); + } + destinationGraph.addChildWithParent(dependency, parentDependency); + } + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraphTransformer.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraphTransformer.java new file mode 100644 index 0000000000..b35e66427f --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/DependencyGraphTransformer.java @@ -0,0 +1,75 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.graph; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.synopsys.integration.detectable.detectable.dependency.BdioNodeFactory; +import com.synopsys.integration.detectable.detectable.dependency.BdioPropertyHelper; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioComponent; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioExternalIdentifier; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioId; +import com.synopsys.integration.detectable.detectable.dependency.model.BdioNode; +import com.synopsys.integration.detectable.detectable.dependency.model.dependency.Dependency; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +// TODO: Convert for bdio-2 +public class DependencyGraphTransformer { + private final BdioPropertyHelper bdioPropertyHelper; + private final BdioNodeFactory bdioNodeFactory; + + public DependencyGraphTransformer(final BdioPropertyHelper bdioPropertyHelper, final BdioNodeFactory bdioNodeFactory) { + this.bdioPropertyHelper = bdioPropertyHelper; + this.bdioNodeFactory = bdioNodeFactory; + } + + public List transformDependencyGraph(final DependencyGraph graph, final BdioNode currentNode, final Set dependencies, final Map existingComponents) { + final List addedComponents = new ArrayList<>(); + for (final Dependency dependency : dependencies) { + if (!existingComponents.containsKey(dependency.getExternalId())) { + final BdioComponent addedNode = componentFromDependency(dependency); + addedComponents.add(addedNode); + existingComponents.put(dependency.getExternalId(), addedNode); + final List addedChildren = transformDependencyGraph(graph, addedNode, graph.getChildrenForParent(dependency), existingComponents); + addedComponents.addAll(addedChildren); + } + bdioPropertyHelper.addRelationship(currentNode, existingComponents.get(dependency.getExternalId())); + } + + return addedComponents; + } + + private BdioComponent componentFromDependency(final Dependency dependency) { + final String componentName = dependency.getName(); + final String componentVersion = dependency.getVersion(); + final BdioId componentId = dependency.getExternalId().createBdioId(); + final BdioExternalIdentifier componentExternalIdentifier = bdioPropertyHelper.createExternalIdentifier(dependency.getExternalId()); + + final BdioComponent component = bdioNodeFactory.createComponent(componentName, componentVersion, componentId, componentExternalIdentifier); + return component; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/MutableDependencyGraph.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/MutableDependencyGraph.java new file mode 100644 index 0000000000..9652e5d20e --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/MutableDependencyGraph.java @@ -0,0 +1,59 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.graph; + +import java.util.List; +import java.util.Set; + +import com.synopsys.integration.detectable.detectable.dependency.model.dependency.Dependency; + +public interface MutableDependencyGraph extends DependencyGraph { + void addGraphAsChildrenToRoot(DependencyGraph sourceGraph); + + void addGraphAsChildrenToParent(Dependency parent, DependencyGraph sourceGraph); + + void addParentWithChild(final Dependency parent, final Dependency child); + + void addParentWithChildren(final Dependency parent, final List children); + + void addParentWithChildren(final Dependency parent, final Set children); + + void addParentWithChildren(final Dependency parent, final Dependency... children); + + void addChildWithParent(final Dependency child, final Dependency parent); + + void addChildWithParents(final Dependency child, final List parents); + + void addChildWithParents(final Dependency child, final Set parents); + + void addChildWithParents(final Dependency child, final Dependency... parents); + + void addChildToRoot(final Dependency child); + + void addChildrenToRoot(final List children); + + void addChildrenToRoot(final Set children); + + void addChildrenToRoot(final Dependency... children); + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/MutableMapDependencyGraph.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/MutableMapDependencyGraph.java new file mode 100644 index 0000000000..ac2c2ccbc6 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/MutableMapDependencyGraph.java @@ -0,0 +1,249 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.graph; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.synopsys.integration.detectable.detectable.dependency.model.dependency.Dependency; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public class MutableMapDependencyGraph implements MutableDependencyGraph { + private final Set rootDependencies = new HashSet<>(); + private final Map dependencies = new HashMap<>(); + private final Map> relationships = new HashMap<>(); + private final DependencyGraphCombiner dependencyGraphCombiner = new DependencyGraphCombiner(); + + @Override + public void addGraphAsChildrenToRoot(final DependencyGraph sourceGraph) { + dependencyGraphCombiner.addGraphAsChildrenToRoot(this, sourceGraph); + } + + @Override + public void addGraphAsChildrenToParent(final Dependency parent, final DependencyGraph sourceGraph) { + dependencyGraphCombiner.addGraphAsChildrenToParent(this, parent, sourceGraph); + } + + @Override + public boolean hasDependency(final ExternalId dependency) { + return dependencies.containsKey(dependency); + } + + @Override + public boolean hasDependency(final Dependency dependency) { + return dependencies.containsKey(dependency.getExternalId()); + } + + @Override + public Dependency getDependency(final ExternalId dependency) { + if (dependencies.containsKey(dependency)) { + return dependencies.get(dependency); + } + return null; + } + + @Override + public Set getChildrenForParent(final ExternalId parent) { + final Set childIds = getChildrenExternalIdsForParent(parent); + return dependenciesFromExternalIds(childIds); + } + + @Override + public Set getParentsForChild(final ExternalId child) { + final Set parentIds = getParentExternalIdsForChild(child); + return dependenciesFromExternalIds(parentIds); + } + + @Override + public Set getChildrenExternalIdsForParent(final ExternalId parent) { + final Set children = new HashSet<>(); + if (relationships.containsKey(parent)) { + children.addAll(relationships.get(parent)); + } + return children; + } + + @Override + public Set getParentExternalIdsForChild(final ExternalId child) { + final Set parents = new HashSet<>(); + for (final Map.Entry> externalIdSetEntry : relationships.entrySet()) { + final ExternalId parentId = externalIdSetEntry.getKey(); + for (final ExternalId childId : externalIdSetEntry.getValue()) { + if (childId.equals(child)) { + parents.add(parentId); + } + } + } + return parents; + } + + @Override + public Set getChildrenForParent(final Dependency parent) { + return getChildrenForParent(parent.getExternalId()); + } + + @Override + public Set getParentsForChild(final Dependency child) { + return getParentsForChild(child.getExternalId()); + } + + @Override + public Set getChildrenExternalIdsForParent(final Dependency parent) { + return getChildrenExternalIdsForParent(parent.getExternalId()); + } + + @Override + public Set getParentExternalIdsForChild(final Dependency child) { + return getParentExternalIdsForChild(child.getExternalId()); + } + + @Override + public void addParentWithChild(final Dependency parent, final Dependency child) { + ensureDependencyAndRelationshipExists(parent); + ensureDependencyExists(child); + addRelationship(parent, child); + } + + @Override + public void addChildWithParent(final Dependency child, final Dependency parent) { + addParentWithChild(parent, child); + } + + @Override + public void addParentWithChildren(final Dependency parent, final List children) { + ensureDependencyAndRelationshipExists(parent); + for (final Dependency child : children) { + ensureDependencyExists(child); + addRelationship(parent, child); + } + } + + @Override + public void addChildWithParents(final Dependency child, final List parents) { + ensureDependencyExists(child); + for (final Dependency parent : parents) { + ensureDependencyAndRelationshipExists(parent); + addRelationship(parent, child); + } + + } + + @Override + public void addParentWithChildren(final Dependency parent, final Set children) { + ensureDependencyAndRelationshipExists(parent); + for (final Dependency child : children) { + ensureDependencyExists(child); + addRelationship(parent, child); + } + } + + @Override + public void addChildWithParents(final Dependency child, final Set parents) { + ensureDependencyExists(child); + for (final Dependency parent : parents) { + ensureDependencyAndRelationshipExists(parent); + addRelationship(parent, child); + } + } + + @Override + public void addParentWithChildren(final Dependency parent, final Dependency... children) { + addParentWithChildren(parent, Arrays.asList(children)); + } + + @Override + public void addChildWithParents(final Dependency child, final Dependency... parents) { + addChildWithParents(child, Arrays.asList(parents)); + } + + @Override + public Set getRootDependencyExternalIds() { + final HashSet copy = new HashSet<>(); + copy.addAll(rootDependencies); + return copy; + } + + @Override + public Set getRootDependencies() { + return dependenciesFromExternalIds(getRootDependencyExternalIds()); + } + + @Override + public void addChildToRoot(final Dependency child) { + ensureDependencyExists(child); + rootDependencies.add(child.getExternalId()); + } + + @Override + public void addChildrenToRoot(final List children) { + for (final Dependency child : children) { + addChildToRoot(child); + } + } + + @Override + public void addChildrenToRoot(final Set children) { + for (final Dependency child : children) { + addChildToRoot(child); + } + } + + @Override + public void addChildrenToRoot(final Dependency... children) { + for (final Dependency child : children) { + addChildToRoot(child); + } + } + + private void ensureDependencyExists(final Dependency dependency) { + if (!dependencies.containsKey(dependency.getExternalId())) { + dependencies.put(dependency.getExternalId(), dependency); + } + } + + private void ensureDependencyAndRelationshipExists(final Dependency dependency) { + ensureDependencyExists(dependency); + if (!relationships.containsKey(dependency.getExternalId())) { + relationships.put(dependency.getExternalId(), new HashSet<>()); + } + } + + private void addRelationship(final Dependency parent, final Dependency child) { + relationships.get(parent.getExternalId()).add(child.getExternalId()); + } + + private Set dependenciesFromExternalIds(final Set ids) { + final Set foundDependencies = new HashSet<>(); + for (final ExternalId id : ids) { + if (dependencies.containsKey(id)) { + foundDependencies.add(dependencies.get(id)); + } + } + return foundDependencies; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/builder/LazyExternalIdDependencyGraphBuilder.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/builder/LazyExternalIdDependencyGraphBuilder.java new file mode 100644 index 0000000000..606ff46db2 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/graph/builder/LazyExternalIdDependencyGraphBuilder.java @@ -0,0 +1,234 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.graph.builder; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.synopsys.integration.detectable.detectable.dependency.graph.DependencyGraph; +import com.synopsys.integration.detectable.detectable.dependency.graph.MutableDependencyGraph; +import com.synopsys.integration.detectable.detectable.dependency.graph.MutableMapDependencyGraph; +import com.synopsys.integration.detectable.detectable.dependency.model.dependency.Dependency; +import com.synopsys.integration.detectable.detectable.dependency.model.dependencyid.DependencyId; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public class LazyExternalIdDependencyGraphBuilder { + private static class LazyDependencyInfo { + private Set children = new HashSet<>(); + private DependencyId aliasId; + private ExternalId externalId; + private String name; + private String version; + + public Set getChildren() { + return children; + } + + public void setChildren(final Set children) { + this.children = children; + } + + public DependencyId getAliasId() { + return aliasId; + } + + public void setAliasId(final DependencyId aliasId) { + this.aliasId = aliasId; + } + + public ExternalId getExternalId() { + return externalId; + } + + public void setExternalId(final ExternalId externalId) { + this.externalId = externalId; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(final String version) { + this.version = version; + } + } + + private final Set rootDependencyIds = new HashSet<>(); + private final Map dependencyInfo = new HashMap<>(); + + private LazyDependencyInfo infoForId(final DependencyId id) { + LazyDependencyInfo info = dependencyInfo.get(id); + if (info.getAliasId() != null) { + info = dependencyInfo.get(info.getAliasId()); + } + return info; + } + + public DependencyGraph build() { + final MutableDependencyGraph mutableDependencyGraph = new MutableMapDependencyGraph(); + + for (final DependencyId dependencyId : dependencyInfo.keySet()) { + final LazyDependencyInfo lazyDependencyInfo = infoForId(dependencyId); + if (lazyDependencyInfo.getExternalId() == null) { + throw new IllegalStateException(String.format("A dependency (%s) in a relationship in the graph never had it's external id set.", dependencyId.toString())); + } + } + + for (final DependencyId dependencyId : dependencyInfo.keySet()) { + final LazyDependencyInfo lazyDependencyInfo = infoForId(dependencyId); + final Dependency dependency = new Dependency(lazyDependencyInfo.getName(), lazyDependencyInfo.getVersion(), lazyDependencyInfo.getExternalId()); + + for (final DependencyId child : lazyDependencyInfo.getChildren()) { + final LazyDependencyInfo childInfo = infoForId(child); + mutableDependencyGraph.addParentWithChild(dependency, new Dependency(childInfo.getName(), childInfo.getVersion(), childInfo.getExternalId())); + } + + if (rootDependencyIds.contains(dependencyId) || rootDependencyIds.contains(lazyDependencyInfo.getAliasId())) { + mutableDependencyGraph.addChildToRoot(dependency); + } + } + + return mutableDependencyGraph; + } + + private void ensureDependencyInfoExists(final DependencyId dependencyId) { + if (!dependencyInfo.containsKey(dependencyId)) { + dependencyInfo.put(dependencyId, new LazyDependencyInfo()); + } + } + + public void setDependencyAsAlias(final DependencyId realDependencyId, final DependencyId fakeDependencyId) { + ensureDependencyInfoExists(realDependencyId); + ensureDependencyInfoExists(fakeDependencyId); + final LazyDependencyInfo info = dependencyInfo.get(fakeDependencyId); + info.setAliasId(realDependencyId); + } + + public void setDependencyInfo(final DependencyId id, final String name, final String version, final ExternalId externalId) { + ensureDependencyInfoExists(id); + final LazyDependencyInfo info = dependencyInfo.get(id); + info.setName(name); + info.setVersion(version); + info.setExternalId(externalId); + } + + public void setDependencyName(final DependencyId id, final String name) { + ensureDependencyInfoExists(id); + final LazyDependencyInfo info = dependencyInfo.get(id); + info.setName(name); + } + + public void setDependencyVersion(final DependencyId id, final String version) { + ensureDependencyInfoExists(id); + final LazyDependencyInfo info = dependencyInfo.get(id); + info.setVersion(version); + } + + public void setDependencyExternalId(final DependencyId id, final ExternalId externalId) { + ensureDependencyInfoExists(id); + final LazyDependencyInfo info = dependencyInfo.get(id); + info.setExternalId(externalId); + } + + public void addParentWithChild(final DependencyId parent, final DependencyId child) { + ensureDependencyInfoExists(child); + ensureDependencyInfoExists(parent); + dependencyInfo.get(parent).getChildren().add(child); + + } + + public void addParentWithChildren(final DependencyId parent, final List children) { + for (final DependencyId child : children) { + addParentWithChild(parent, child); + } + } + + public void addParentWithChildren(final DependencyId parent, final Set children) { + for (final DependencyId child : children) { + addParentWithChild(parent, child); + } + } + + public void addParentWithChildren(final DependencyId parent, final DependencyId... children) { + for (final DependencyId child : children) { + addParentWithChild(parent, child); + } + } + + public void addChildWithParent(final DependencyId child, final DependencyId parent) { + addParentWithChild(parent, child); + } + + public void addChildWithParents(final DependencyId child, final List parents) { + for (final DependencyId parent : parents) { + addChildWithParent(child, parent); + } + } + + public void addChildWithParents(final DependencyId child, final Set parents) { + for (final DependencyId parent : parents) { + addChildWithParent(child, parent); + } + } + + public void addChildWithParents(final DependencyId child, final DependencyId... parents) { + for (final DependencyId parent : parents) { + addChildWithParent(child, parent); + } + } + + public void addChildToRoot(final DependencyId child) { + ensureDependencyInfoExists(child); + rootDependencyIds.add(child); + } + + public void addChildrenToRoot(final List children) { + for (final DependencyId child : children) { + addChildToRoot(child); + } + } + + public void addChildrenToRoot(final Set children) { + for (final DependencyId child : children) { + addChildToRoot(child); + } + } + + public void addChildrenToRoot(final DependencyId... children) { + for (final DependencyId child : children) { + addChildToRoot(child); + } + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioBillOfMaterials.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioBillOfMaterials.java new file mode 100644 index 0000000000..cf527f806e --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioBillOfMaterials.java @@ -0,0 +1,41 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import com.google.gson.annotations.SerializedName; + +public class BdioBillOfMaterials extends BdioNode { + @SerializedName("specVersion") + public String bdioSpecificationVersion; + + @SerializedName("spdx:name") + public String spdxName; + + @SerializedName("creationInfo") + public BdioCreationInfo creationInfo; + + public BdioBillOfMaterials() { + type = "BillOfMaterials"; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioComponent.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioComponent.java new file mode 100644 index 0000000000..73e6b67706 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioComponent.java @@ -0,0 +1,38 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import com.google.gson.annotations.SerializedName; + +public class BdioComponent extends BdioNode { + @SerializedName("name") + public String name; + + @SerializedName("revision") + public String version; + + public BdioComponent() { + type = "Component"; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioCreationInfo.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioCreationInfo.java new file mode 100644 index 0000000000..0b91915d67 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioCreationInfo.java @@ -0,0 +1,52 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.google.gson.annotations.SerializedName; + +public class BdioCreationInfo { + @SerializedName("spdx:creator") + private final List creator = new ArrayList<>(); + + @SerializedName("spdx:created") + public String created; + + // ekerwin 2018-06-11 + // Black Duck only supports a single creator and if there are multiple creators, Black Duck will use only the first one. + public void setPrimarySpdxCreator(final SpdxCreator spdxCreator) { + creator.add(0, spdxCreator.getData()); + } + + public void addSpdxCreator(final SpdxCreator spdxCreator) { + creator.add(spdxCreator.getData()); + } + + public List getCreator() { + return Collections.unmodifiableList(creator); + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioExternalIdentifier.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioExternalIdentifier.java new file mode 100644 index 0000000000..865b65230e --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioExternalIdentifier.java @@ -0,0 +1,38 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import com.google.gson.annotations.SerializedName; +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public class BdioExternalIdentifier { + @SerializedName("externalSystemTypeId") + public String forge; + + @SerializedName("externalId") + public String externalId; + + @SerializedName("externalIdMetaData") + // this horrible name exists because 'externalId' is reserved by the bdio specification + public ExternalId externalIdMetaData; +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioId.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioId.java new file mode 100644 index 0000000000..e34d8035c1 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioId.java @@ -0,0 +1,83 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import org.apache.commons.lang3.StringUtils; + +import com.google.gson.annotations.JsonAdapter; +import com.synopsys.integration.util.IntegrationEscapeUtil; + +@JsonAdapter(BdioIdAdapter.class) +public class BdioId { + public static final String BDIO_ID_SEPARATOR = "/"; + + private static final IntegrationEscapeUtil integrationEscapeUtil = new IntegrationEscapeUtil(); + + private final String id; + + public static BdioId createFromPieces(final List pieces) { + return new BdioId( + "http:" + StringUtils.join(integrationEscapeUtil.escapePiecesForUri(pieces), com.synopsys.integration.bdio.model.BdioId.BDIO_ID_SEPARATOR)); + } + + public static BdioId createFromPieces(final String... pieces) { + return BdioId.createFromPieces(Arrays.asList(pieces)); + } + + public static BdioId createFromUUID(final String uuid) { + return new BdioId(String.format("uuid:%s", uuid)); + } + + /** + * Avoid using this constructor - bdio id's have uniqueness and formatting constraints that the static builders handle better. + * This is mainly for use internally within the library. + */ + public BdioId(final String id) { + this.id = id; + } + + @Override + public boolean equals(final Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + final BdioId bdioId = (BdioId) o; + return Objects.equals(id, bdioId.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return id; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioIdAdapter.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioIdAdapter.java new file mode 100644 index 0000000000..ebe464818d --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioIdAdapter.java @@ -0,0 +1,42 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import java.io.IOException; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +public class BdioIdAdapter extends TypeAdapter { + @Override + public void write(final JsonWriter writer, final BdioId value) throws IOException { + writer.value(value.toString()); + } + + @Override + public BdioId read(final JsonReader in) throws IOException { + return new BdioId(in.nextString()); + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioNode.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioNode.java new file mode 100644 index 0000000000..93e6b04ba3 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioNode.java @@ -0,0 +1,43 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import java.util.ArrayList; +import java.util.List; + +import com.google.gson.annotations.SerializedName; + +public class BdioNode { + @SerializedName("@id") + public BdioId id; + + @SerializedName("@type") + public String type; + + @SerializedName("externalIdentifier") + public BdioExternalIdentifier bdioExternalIdentifier; + + @SerializedName("relationship") + public List relationships = new ArrayList<>(); + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioProject.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioProject.java new file mode 100644 index 0000000000..1e2edf920f --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioProject.java @@ -0,0 +1,30 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +public class BdioProject extends BdioComponent { + public BdioProject() { + type = "Project"; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioRelationship.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioRelationship.java new file mode 100644 index 0000000000..d52f109865 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/BdioRelationship.java @@ -0,0 +1,34 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import com.google.gson.annotations.SerializedName; + +public class BdioRelationship { + @SerializedName("related") + public BdioId related; + + @SerializedName("relationshipType") + public String relationshipType; + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/Forge.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/Forge.java new file mode 100644 index 0000000000..44f2105ef2 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/Forge.java @@ -0,0 +1,150 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; + +import com.synopsys.integration.util.Stringable; + +public class Forge extends Stringable { + // forges that use the slash as the separator + public static final Forge ALPINE = new Forge("/", "alpine"); + public static final Forge ANACONDA = new Forge("/", "anaconda"); + public static final Forge APACHE_SOFTWARE = new Forge("/", "apache_software"); + public static final Forge BITBUCKET = new Forge("/", "bitbucket"); + public static final Forge BOWER = new Forge("/", "bower"); + public static final Forge BUSYBOX = new Forge("/", "busybox"); + public static final Forge CENTOS = new Forge("/", "centos"); + public static final Forge CODEPLEX = new Forge("/", "codeplex"); + public static final Forge CODEPLEX_GROUP = new Forge("/", "codeplex_group"); + public static final Forge CPAN = new Forge("/", "cpan"); + public static final Forge CRAN = new Forge("/", "cran"); + public static final Forge DEBIAN = new Forge("/", "debian"); + public static final Forge FEDORA = new Forge("/", "fedora"); + public static final Forge FREEDESKTOP_ORG = new Forge("/", "freedesktop_org"); + public static final Forge GITCAFE = new Forge("/", "gitcafe"); + public static final Forge GITLAB = new Forge("/", "gitlab"); + public static final Forge GITORIOUS = new Forge("/", "gitorious"); + public static final Forge GOGET = new Forge("/", "goget"); + public static final Forge GNU = new Forge("/", "gnu"); + public static final Forge GOOGLECODE = new Forge("/", "googlecode"); + public static final Forge HEX = new Forge("/", "hex"); + public static final Forge JAVA_NET = new Forge("/", "java_net"); + public static final Forge KDE_ORG = new Forge("/", "kde_org"); + public static final Forge LAUNCHPAD = new Forge("/", "launchpad"); + public static final Forge LONG_TAIL = new Forge("/", "long_tail"); + public static final Forge NUGET = new Forge("/", "nuget"); + public static final Forge NPMJS = new Forge("/", "npmjs"); + public static final Forge PEAR = new Forge("/", "pear"); + public static final Forge PYPI = new Forge("/", "pypi"); + public static final Forge REDHAT = new Forge("/", "redhat"); + public static final Forge RUBYFORGE = new Forge("/", "rubyforge"); + public static final Forge RUBYGEMS = new Forge("/", "rubygems"); + public static final Forge SOURCEFORGE = new Forge("/", "sourceforge"); + public static final Forge SOURCEFORGE_JP = new Forge("/", "sourceforge_jp"); + public static final Forge UBUNTU = new Forge("/", "ubuntu"); + public static final Forge YOCTO = new Forge("/", "yocto"); + + // forges that use the colon as the separator + public static final Forge ANDROID = new Forge(":", "android"); + public static final Forge COCOAPODS = new Forge(":", "cocoapods"); + public static final Forge CPE = new Forge(":", "cpe"); + public static final Forge GITHUB = new Forge(":", "github"); + public static final Forge GOLANG = new Forge(":", "golang"); + public static final Forge MAVEN = new Forge(":", "maven"); + public static final Forge PACKAGIST = new Forge(":", "packagist"); + + private final String name; + private final String separator; + private Boolean usePreferredNamespaceAlias; + + public Forge(final String separator, final String name) { + if (StringUtils.isBlank(name)) { + throw new IllegalArgumentException("A non-blank name is required."); + } + + this.name = name.toLowerCase(); + this.separator = separator; + } + + public Forge(final String separator, final String name, final boolean usePreferredNamespaceAlias) { + this(separator, name); + this.usePreferredNamespaceAlias = usePreferredNamespaceAlias; + } + + /** + * The various separators here are to support three systems 1) bdio, 2) Black Duck, 3) Black Duck Knowledge Base. + * + * To support matching in various Black Duck versions, the separators need to conform to the separators defined in the source of the bdio project here: + * https://github.com/blackducksoftware/bdio/blob/2.1.1/bdio/src/main/java/com/blackducksoftware/bdio/model/ExternalIdentifierBuilder.java + * + * However, as of Black Duck 4.3.2, those separators are internally transformed to the KB separators, which are (currently) defined here: https://confluence.dc1.lan/display/KB/KB+Details+Service#KBDetailsService-ForgeNames + * + * As of 2017-11-29, only forward slashes and colons are used for KB separators. + */ + + public static Map getKnownForges() { + final Map knownForges = new HashMap<>(); + + final List knownStaticFinalForgeFields = Arrays + .stream(Forge.class.getFields()) + .filter(f -> Modifier.isStatic(f.getModifiers())) + .filter(f -> Modifier.isFinal(f.getModifiers())) + .filter(f -> f.getType().isAssignableFrom(Forge.class)) + .collect(Collectors.toList()); + + for (final Field field : knownStaticFinalForgeFields) { + try { + final Forge forge = (Forge) field.get(null); + knownForges.put(forge.getName(), forge); + } catch (final IllegalAccessException ignored) { + // okay to ignore, the filtering above took care of this + } + } + + return knownForges; + } + + @Override + public String toString() { + return name; + } + + public String getName() { + final String formatString = usePreferredNamespaceAlias != null && usePreferredNamespaceAlias ? "@%s" : "%s"; + return String.format(formatString, name); + } + + public String getSeparator() { + return separator; + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/SimpleBdioDocument.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/SimpleBdioDocument.java new file mode 100644 index 0000000000..1c277d40ce --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/SimpleBdioDocument.java @@ -0,0 +1,56 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +import java.util.ArrayList; +import java.util.List; + +public class SimpleBdioDocument { + private BdioBillOfMaterials billOfMaterials; + private BdioProject project; + private List components = new ArrayList<>(); + + public BdioBillOfMaterials getBillOfMaterials() { + return billOfMaterials; + } + + public void setBillOfMaterials(final BdioBillOfMaterials billOfMaterials) { + this.billOfMaterials = billOfMaterials; + } + + public BdioProject getProject() { + return project; + } + + public void setProject(final BdioProject project) { + this.project = project; + } + + public List getComponents() { + return components; + } + + public void setComponents(final List components) { + this.components = components; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/SpdxCreator.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/SpdxCreator.java new file mode 100644 index 0000000000..3e7f06a619 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/SpdxCreator.java @@ -0,0 +1,58 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model; + +public class SpdxCreator { + private String type; + private String identifier; + + public SpdxCreator(final String type, final String identifier) { + this.type = type; + this.identifier = identifier; + } + + public String getData() { + return String.format("%s: %s", type, identifier); + } + + public static SpdxCreator createToolSpdxCreator(final String name, final String version) { + final String identifier = String.format("%s-%s", name, version); + return new SpdxCreator("Tool", identifier); + } + + public String getType() { + return type; + } + + public void setType(final String type) { + this.type = type; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(final String identifier) { + this.identifier = identifier; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependency/Dependency.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependency/Dependency.java new file mode 100644 index 0000000000..df74415713 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependency/Dependency.java @@ -0,0 +1,89 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.dependency; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.RecursiveToStringStyle; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; + +import com.synopsys.integration.detectable.detectable.dependency.model.externalid.ExternalId; + +public class Dependency { + private String name; + private String version; + private ExternalId externalId; + + public Dependency(final String name, final String version, final ExternalId externalId) { + this.name = name; + this.version = version; + this.externalId = externalId; + } + + public Dependency(final String name, final ExternalId externalId) { + this(name, null, externalId); + } + + public Dependency(final ExternalId externalId) { + this(null, externalId); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } + + @Override + public boolean equals(final Object obj) { + return EqualsBuilder.reflectionEquals(this, obj); + } + + @Override + public String toString() { + return ReflectionToStringBuilder.toString(this, RecursiveToStringStyle.JSON_STYLE); + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(final String version) { + this.version = version; + } + + public ExternalId getExternalId() { + return externalId; + } + + public void setExternalId(final ExternalId externalId) { + this.externalId = externalId; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/DependencyId.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/DependencyId.java new file mode 100644 index 0000000000..b74986276e --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/DependencyId.java @@ -0,0 +1,46 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.dependencyid; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.RecursiveToStringStyle; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; + +public abstract class DependencyId { + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } + + @Override + public boolean equals(final Object obj) { + return EqualsBuilder.reflectionEquals(this, obj); + } + + @Override + public String toString() { + return ReflectionToStringBuilder.toString(this, RecursiveToStringStyle.JSON_STYLE); + } + +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/NameDependencyId.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/NameDependencyId.java new file mode 100644 index 0000000000..e179f25675 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/NameDependencyId.java @@ -0,0 +1,39 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.dependencyid; + +public class NameDependencyId extends DependencyId { + private String name; + + public NameDependencyId(final String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/NameVersionDependencyId.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/NameVersionDependencyId.java new file mode 100644 index 0000000000..f3bf52018e --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/NameVersionDependencyId.java @@ -0,0 +1,40 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.dependencyid; + +public class NameVersionDependencyId extends NameDependencyId { + private String version; + + public NameVersionDependencyId(final String name, final String version) { + super(name); + this.version = version; + } + + public String getVersion() { + return version; + } + + public void setVersion(final String version) { + this.version = version; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/StringDependencyId.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/StringDependencyId.java new file mode 100644 index 0000000000..a1680fe16f --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/dependencyid/StringDependencyId.java @@ -0,0 +1,39 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.dependencyid; + +public class StringDependencyId extends DependencyId { + private String value; + + public StringDependencyId(final String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(final String value) { + this.value = value; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/externalid/ExternalId.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/externalid/ExternalId.java new file mode 100644 index 0000000000..8a7123c766 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/externalid/ExternalId.java @@ -0,0 +1,169 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.externalid; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; + +import com.synopsys.integration.detectable.detectable.dependency.model.BdioId; +import com.synopsys.integration.detectable.detectable.dependency.model.Forge; +import com.synopsys.integration.util.Stringable; + +public class ExternalId extends Stringable { + private final Forge forge; + private String layer; + private String group; + private String name; + private String version; + private String architecture; + private String[] moduleNames; + private String path; + + public ExternalId(final Forge forge) { + this.forge = forge; + } + + /** + * A forge is always required. The other fields to populate depend on what + * external id type you need. The currently supported types are: + * "name/version": populate name and version (if version is blank, only name is included) + * "architecture": populate name, version, and architecture (if version is blank, only name is included) + * "layer": populate name, version, and layer (if version is blank, only name is included) + * "maven": populate name, version, and group (if version is blank, group and name are included) + * "module names": populate moduleNames + * "path": populate path + */ + public String[] getExternalIdPieces() { + if (StringUtils.isNotBlank(path)) { + return new String[] { path }; + } else if (moduleNames != null && moduleNames.length > 0) { + return moduleNames; + } else if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(version)) { + if (StringUtils.isNotBlank(group)) { + return new String[] { group, name, version }; + } else if (StringUtils.isNotBlank(architecture)) { + return new String[] { name, version, architecture }; + } else if (StringUtils.isNotBlank(layer)) { + return new String[] { layer, name, version }; + } else { + return new String[] { name, version }; + } + } else if (StringUtils.isNotBlank(name)) { + //Black Duck now (2019.6.0) supports version-less components + if (StringUtils.isNotBlank(group)) { + return new String[] { group, name }; + } else if (StringUtils.isNotBlank(layer)) { + return new String[] { layer, name }; + } else { + return new String[] { name }; + } + } + + // if we can't be positive about what kind of external id we are, just give everything we have in a reasonable order + final List bestGuessPieces = new ArrayList<>(); + + final List bestGuessCandidates = Arrays.asList(layer, group, name, version, architecture); + for (final String candidate : bestGuessCandidates) { + if (StringUtils.isNotBlank(candidate)) { + bestGuessPieces.add(candidate); + } + } + + return bestGuessPieces.toArray(new String[0]); + } + + public BdioId createBdioId() { + final List bdioIdPieces = new ArrayList<>(); + bdioIdPieces.add(forge.toString()); + bdioIdPieces.addAll((Arrays.asList(getExternalIdPieces()))); + + return BdioId.createFromPieces(bdioIdPieces); + } + + public String createExternalId() { + return StringUtils.join(getExternalIdPieces(), forge.getSeparator()); + } + + public Forge getForge() { + return forge; + } + + public String getLayer() { + return layer; + } + + public void setLayer(final String layer) { + this.layer = layer; + } + + public String getGroup() { + return group; + } + + public void setGroup(final String group) { + this.group = group; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(final String version) { + this.version = version; + } + + public String getArchitecture() { + return architecture; + } + + public void setArchitecture(final String architecture) { + this.architecture = architecture; + } + + public String[] getModuleNames() { + return moduleNames; + } + + public void setModuleNames(final String[] moduleNames) { + this.moduleNames = moduleNames; + } + + public String getPath() { + return path; + } + + public void setPath(final String path) { + this.path = path; + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/externalid/ExternalIdFactory.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/externalid/ExternalIdFactory.java new file mode 100644 index 0000000000..f88ea12618 --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectable/dependency/model/externalid/ExternalIdFactory.java @@ -0,0 +1,78 @@ +/** + * integration-bdio + * + * Copyright (c) 2019 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://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. + */ +package com.synopsys.integration.detectable.detectable.dependency.model.externalid; + +import com.synopsys.integration.detectable.detectable.dependency.model.Forge; + +public class ExternalIdFactory { + public ExternalId createNameVersionExternalId(final Forge forge, final String name, final String version) { + final ExternalId externalId = new ExternalId(forge); + externalId.setName(name); + externalId.setVersion(version); + checkForValidity(externalId); + return externalId; + } + + public ExternalId createYoctoExternalId(final String layer, final String name, final String version) { + final ExternalId externalId = createNameVersionExternalId(Forge.YOCTO, name, version); + externalId.setLayer(layer); + checkForValidity(externalId); + return externalId; + } + + public ExternalId createMavenExternalId(final String group, final String name, final String version) { + final ExternalId externalId = createNameVersionExternalId(Forge.MAVEN, name, version); + externalId.setGroup(group); + checkForValidity(externalId); + return externalId; + } + + public ExternalId createArchitectureExternalId(final Forge forge, final String name, final String version, final String architecture) { + final ExternalId externalId = createNameVersionExternalId(forge, name, version); + externalId.setArchitecture(architecture); + checkForValidity(externalId); + return externalId; + } + + public ExternalId createModuleNamesExternalId(final Forge forge, final String... moduleNames) { + final ExternalId externalId = new ExternalId(forge); + externalId.setModuleNames(moduleNames); + checkForValidity(externalId); + return externalId; + } + + public ExternalId createPathExternalId(final Forge forge, final String path) { + final ExternalId externalId = new ExternalId(forge); + externalId.setPath(path); + checkForValidity(externalId); + return externalId; + } + + /** + * An ExternalId should be able to create a bdioId - if it can not, an appropriate IllegalStateException will be thrown. + */ + private void checkForValidity(final ExternalId externalId) { + externalId.createBdioId(); + } + +}