Skip to content

Commit

Permalink
DA Refinement logging (winery#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
lharzenetter authored Nov 25, 2022
1 parent dcb9686 commit aa931b2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 38 deletions.
10 changes: 9 additions & 1 deletion org.eclipse.winery.model.adaptation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@
<version>${checkstyle.addons.version}</version>
</dependency>
</dependencies>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ private boolean applyDeploymentArtifactMapping(RefinementCandidate refinement, T
.filter(mapping -> mapping.getDetectorElement().getId().equals(detectorNode.getId()))
.anyMatch(mapping -> {
if (ModelUtilities.isOfType(mapping.getArtifactType(), deploymentArtifact.getArtifactType(), this.artifactTypes)) {
// only do the actual transformation in the transformation step, not the "is-applicable" step
if (idMapping != null) {
TNodeTemplate addedNode = topology.getNodeTemplate(idMapping.get(mapping.getRefinementElement().getId()));
if (addedNode != null) {
Expand Down Expand Up @@ -286,46 +287,63 @@ TDeploymentArtifact translateDeploymentArtifact(OTDeploymentArtifactMapping mapp

HttpPost httpPost = new HttpPost(service.url);
httpPost.setEntity(multipartBuilder.build());
LOGGER.info("Sending request: {}", httpPost);

try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
ArtifactTemplateId translatedArtifactId = new ArtifactTemplateId(
artifactTemplateId.getNamespace().getDecoded(),
VersionSupport.getNewComponentVersionId(artifactTemplateId, mapping.getTargetArtifactType().getLocalPart()),
false
);
this.repository.setElement(
translatedArtifactId,
new TArtifactTemplate.Builder(translatedArtifactId.getXmlId().getDecoded(), mapping.getTargetArtifactType())
.build()
);

String fileName = "translated";
if (response.getFirstHeader("Content-Disposition") != null) {
String value = response.getFirstHeader("Content-Disposition").getValue();
if (value != null) {
for (String contentDisposition : value.split(" ")) {
if (contentDisposition.startsWith("filename=") && contentDisposition.length() > "filename=".length() + 1) {
fileName = contentDisposition.substring(contentDisposition.indexOf("=") + 1);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
ArtifactTemplateId translatedArtifactId = new ArtifactTemplateId(
artifactTemplateId.getNamespace().getDecoded(),
VersionSupport.getNewComponentVersionId(artifactTemplateId, mapping.getTargetArtifactType().getLocalPart()),
false
);
this.repository.setElement(
translatedArtifactId,
new TArtifactTemplate.Builder(translatedArtifactId.getXmlId().getDecoded(), mapping.getTargetArtifactType())
.setName(translatedArtifactId.getXmlId().getDecoded())
.build()
);
LOGGER.info("Created new Artifact Template with QName \"{}\"", translatedArtifactId.getQName());

String fileName = "translated";
if (response.getFirstHeader("Content-Disposition") != null) {
String value = response.getFirstHeader("Content-Disposition").getValue();
if (value != null) {
for (String contentDisposition : value.split(" ")) {
if (contentDisposition.startsWith("filename=") && contentDisposition.length() > "filename=".length() + 1) {
fileName = contentDisposition.substring(contentDisposition.indexOf("=") + 1);
}
}
}
}
}

ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(translatedArtifactId);
if (response.getEntity() != null) {
try (BufferedInputStream contentStream = new BufferedInputStream(response.getEntity().getContent())) {
repository.putContentToFile(
new RepositoryFileReference(filesId, fileName),
contentStream
);
LOGGER.info("Saving translated file \"{}\"", fileName);
ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(translatedArtifactId);
if (response.getEntity() != null) {
try (BufferedInputStream contentStream = new BufferedInputStream(response.getEntity().getContent())) {
repository.putContentToFile(
new RepositoryFileReference(filesId, fileName),
contentStream
);

BackendUtils.synchronizeReferences(RepositoryFactory.getRepository(), translatedArtifactId);
BackendUtils.synchronizeReferences(RepositoryFactory.getRepository(), translatedArtifactId);
LOGGER.info("Successfully saved file!");

return new TDeploymentArtifact.Builder(deploymentArtifact.getName() + "-translated", mapping.getTargetArtifactType())
.setArtifactRef(translatedArtifactId.getQName())
.build();
TDeploymentArtifact translatedArtifact = new TDeploymentArtifact.Builder(deploymentArtifact.getName() + "-translated", mapping.getTargetArtifactType())
.setArtifactRef(translatedArtifactId.getQName())
.build();
LOGGER.info("Created \"{}\"", translatedArtifact);

return translatedArtifact;
}
} else {
LOGGER.error("Service did not respond with a translated file!");
}
} else {
LOGGER.error("Service did not respond with a translated file!");
LOGGER.error("Server responded with Status {} and error \"{}\"",
statusCode,
new String(response.getEntity().getContent().readAllBytes())
);
}
} catch (IOException e) {
LOGGER.error("Could not refine DA...!", e);
Expand All @@ -334,14 +352,13 @@ TDeploymentArtifact translateDeploymentArtifact(OTDeploymentArtifactMapping mapp
}
} catch (IOException e) {
LOGGER.error("Could not refine DA...!", e);
LOGGER.warn("Defaulting to already contained DA!");
} catch (WineryRepositoryException e) {
LOGGER.error("Error while zipping the files in the artifact!", e);
}
}
}

LOGGER.warn("Using existing DA...");
LOGGER.warn("Defaulting to already contained DA!");

return deploymentArtifact;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.eclipse.winery.common.configuration.DARefinementConfigurationObject;
import org.eclipse.winery.model.ids.definitions.ArtifactTemplateId;
import org.eclipse.winery.model.tosca.TArtifactTemplate;
import org.eclipse.winery.model.tosca.TDeploymentArtifact;
import org.eclipse.winery.model.tosca.extensions.OTDeploymentArtifactMapping;
import org.eclipse.winery.repository.TestWithGitBackedRepository;
Expand Down Expand Up @@ -71,9 +72,9 @@ public void testDeploymentArtifactTranslation(WireMockRuntimeInfo mockServer) th
service.url = mockServer.getHttpBaseUrl() + "/endpoint";
service.canRefine = new DARefinementConfigurationObject.TransformationCapabilities();
service.canRefine.from = new ArrayList<>();
service.canRefine.from.add("TestType");
service.canRefine.from.add(sourceType.getLocalPart());
service.canRefine.to = new ArrayList<>();
service.canRefine.to.add("TargetType");
service.canRefine.to.add(targetType.getLocalPart());

Map<String, DARefinementConfigurationObject.DARefinementService> configMap = new HashMap<>();
configMap.put("testRefinementService", service);
Expand Down Expand Up @@ -114,8 +115,17 @@ public void testDeploymentArtifactTranslation(WireMockRuntimeInfo mockServer) th

assertNotNull(transformationResult);
assertNotEquals(testDa, transformationResult);
assertEquals(targetType, transformationResult.getArtifactType());
assertEquals(testDa.getName() + "-translated", transformationResult.getName());

ArtifactTemplateFilesDirectoryId filesDirectoryId = new ArtifactTemplateFilesDirectoryId(new ArtifactTemplateId(transformationResult.getArtifactRef()));
ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId(transformationResult.getArtifactRef());

TArtifactTemplate element = repository.getElement(artifactTemplateId);
assertEquals(targetType, element.getType());
assertEquals("ArtifactTemplateWithFilesAndSources-ArtifactTypeWithoutProperties_TargetType-w1-wip1", element.getId());
assertEquals("ArtifactTemplateWithFilesAndSources-ArtifactTypeWithoutProperties_TargetType-w1-wip1", element.getName());

ArtifactTemplateFilesDirectoryId filesDirectoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
RepositoryFileReference repositoryFileReference = new RepositoryFileReference(filesDirectoryId, "test.zip");
assertEquals(
Files.size(Paths.get(ClassLoader.getSystemClassLoader().getResource("__files/test.zip").toURI())), // 128 bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public void accept(Visitor visitor) {
visitor.visit(this);
}

@Override
public String toString() {
return "TDeploymentArtifact{" +
"name='" + name + '\'' +
", artifactType=" + artifactType +
", artifactRef=" + artifactRef +
'}';
}

public static class Builder extends TDeploymentOrImplementationArtifact.Builder<Builder> {

public Builder(String name, QName artifactType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public void writeInputStreamToPath(Path targetPath, InputStream inputStream) thr
FileUtils.createDirectory(targetPath.getParent());

try {
LOGGER.debug("Wrote {} bytes to \"{}\"",
LOGGER.debug("Wrote {} bytes to \"{}\"",
Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING),
targetPath
);
Expand Down

0 comments on commit aa931b2

Please sign in to comment.