use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class IRepository method getReferencedTOSCAComponentImplementationArtifactIds.
/**
* Helper method
*
* @param ids the list of ids to add the new ids to
* @param implementationArtifacts the implementation artifacts belonging to the given id
* @param id the id to handle
*/
default Collection<DefinitionsChildId> getReferencedTOSCAComponentImplementationArtifactIds(Collection<DefinitionsChildId> ids, List<TImplementationArtifact> implementationArtifacts, DefinitionsChildId id) {
if (implementationArtifacts != null) {
for (TImplementationArtifact ia : implementationArtifacts) {
QName qname;
if ((qname = ia.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
ids.add(new ArtifactTypeId(ia.getArtifactType()));
}
}
return ids;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId id) {
// We have to use a HashSet to ensure that no duplicate ids are added<
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
TServiceTemplate serviceTemplate = this.getElement(id);
// add included things to export queue
TBoundaryDefinitions boundaryDefs;
if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
List<TPolicy> policies = boundaryDefs.getPolicies();
if (policies != null) {
for (TPolicy policy : policies) {
PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
ids.add(policyTypeId);
PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
ids.add(policyTemplateId);
}
}
// reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
}
final TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
if (topology != null) {
if (Objects.nonNull(topology.getPolicies())) {
topology.getPolicies().stream().filter(Objects::nonNull).forEach(p -> {
QName type = p.getPolicyType();
PolicyTypeId policyTypeIdId = new PolicyTypeId(type);
ids.add(policyTypeIdId);
});
}
for (TEntityTemplate entityTemplate : topology.getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through policies
List<TPolicy> policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
QName template = pol.getPolicyRef();
if (template != null) {
PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
ids.add(policyTemplateId);
}
}
}
// Crawl RequirementTypes and Capabilities for their references
getReferencedRequirementTypeIds(ids, n);
getCapabilitiesReferences(ids, n);
// TODO: this information is collected differently for YAML and XML modes
// crawl through deployment artifacts
List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
if (da.getArtifactType() != null) {
// This is considered Nullable, because the test case ConsistencyCheckerTest#hasError
// expects an empty artifactType and thus it may be null.
ids.add(new ArtifactTypeId(da.getArtifactType()));
}
if (da.getArtifactRef() != null) {
ids.add(new ArtifactTemplateId(da.getArtifactRef()));
}
}
}
// Store all referenced artifact types
List<TArtifact> artifacts = n.getArtifacts();
if (Objects.nonNull(artifacts)) {
artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
}
TNodeType nodeType = this.getElement(new NodeTypeId(qname));
if (Objects.nonNull(nodeType.getInterfaceDefinitions())) {
nodeType.getInterfaceDefinitions().stream().filter(Objects::nonNull).forEach(iDef -> {
if (Objects.nonNull(iDef.getType())) {
ids.add(new InterfaceTypeId(iDef.getType()));
}
});
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class DockerPlugin method createDockerImage.
private QName createDockerImage(Path tempDirectory, ArtifactTemplateId artifactTemplate, IRepository repository) {
String dockerImageName = artifactTemplate.getQName().getLocalPart().toLowerCase().replaceAll("(\\s)|(_)|(-)", "");
QName selfContainedVersion = VersionSupport.getSelfContainedVersion(artifactTemplate);
ArtifactTemplateId generatedArtifactTemplateId = new ArtifactTemplateId(selfContainedVersion);
if (!repository.exists(generatedArtifactTemplateId)) {
try {
Utils.execute(tempDirectory.toString(), "docker", "build", "-t", dockerImageName, ".");
String tarball = dockerImageName + ".tar";
Utils.execute(tempDirectory.toString(), "docker", "save", "-o", tarball, dockerImageName + ":latest");
Utils.compressTarBallAndAddToArtifact(tempDirectory, repository, generatedArtifactTemplateId, tarball);
FileUtils.forceDelete(tempDirectory);
} catch (InterruptedException | IOException e) {
logger.error("Error while creating Dockerfile...", e);
return null;
}
}
return selfContainedVersion;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class SelfContainmentPackager method createSelfContainedNodeTypeImplementation.
private void createSelfContainedNodeTypeImplementation(TNodeTypeImplementation impl) {
if (impl.getImplementationArtifacts() != null) {
List<SelfContainmentPlugin.GeneratedArtifacts> generatedArtifacts = impl.getImplementationArtifacts().stream().filter(ia -> Objects.nonNull(ia.getArtifactRef())).filter(ia -> !new ArtifactTemplateId(ia.getArtifactRef()).isSelfContained()).map(ia -> this.downloadArtifacts(ia.getArtifactRef(), ia.getArtifactType())).filter(Objects::nonNull).filter(SelfContainmentPlugin.GeneratedArtifacts::containsNewElements).collect(Collectors.toList());
if (!generatedArtifacts.isEmpty()) {
NodeTypeImplementationId nodeTypeImplementationId = getSelfContainedNodeTypeImplId(impl);
try {
this.repository.duplicate(new NodeTypeImplementationId(impl.getQName()), nodeTypeImplementationId);
TNodeTypeImplementation implementation = this.repository.getElement(nodeTypeImplementationId);
generatedArtifacts.forEach(generatedArtifact -> {
if (implementation.getImplementationArtifacts() != null) {
implementation.getImplementationArtifacts().forEach(ia -> {
if (generatedArtifact.artifactToReplaceQName.equals(ia.getArtifactRef())) {
ia.setArtifactRef(generatedArtifact.selfContainedArtifactQName);
TArtifactTemplate artifactTemplate = generatedArtifact.selfContainedArtifactTemplate;
if (artifactTemplate == null) {
artifactTemplate = repository.getElement(new ArtifactTemplateId(generatedArtifact.selfContainedArtifactQName));
}
if (artifactTemplate.getType() == null) {
logger.error("Artifact Template does not have a type assigned! {}", generatedArtifact.selfContainedArtifactQName);
}
ia.setArtifactType(artifactTemplate.getType());
}
});
List<TDeploymentArtifact> deploymentArtifacts = implementation.getDeploymentArtifacts();
if (deploymentArtifacts == null) {
deploymentArtifacts = new ArrayList<>();
implementation.setDeploymentArtifacts(deploymentArtifacts);
} else {
deploymentArtifacts.removeIf(da -> da.getArtifactRef() != null && generatedArtifact.deploymentArtifactsToRemove.contains(da.getArtifactRef()));
}
for (QName artifactTemplate : generatedArtifact.deploymentArtifactsToAdd) {
TArtifactTemplate generatedAT = repository.getElement(new ArtifactTemplateId(artifactTemplate));
deploymentArtifacts.add(new TDeploymentArtifact.Builder(artifactTemplate.getLocalPart(), generatedAT.getType()).setArtifactRef(artifactTemplate).build());
}
}
});
repository.setElement(nodeTypeImplementationId, implementation);
} catch (IOException e) {
logger.error("Error while creating new self-contained NodeTypeImplementation of {}", impl, e);
}
}
} else {
logger.info("No processable IAs found in Node Type Implementation {}", impl.getQName());
}
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class UbuntuVMPlugin method downloadDependenciesBasedOnNodeType.
@Override
public void downloadDependenciesBasedOnNodeType(TNodeTypeImplementation nodeTypeImplementation, IRepository repository) {
QName nodeType = nodeTypeImplementation.getNodeType();
WineryVersion nodeTypeVersion = VersionUtils.getVersion(nodeType.getLocalPart());
String componentVersion = nodeTypeVersion.getComponentVersion();
if (componentVersion != null) {
String codeName = getCodeName(componentVersion);
if (codeName != null) {
logger.info("Found code name '{}' for Ubuntu Node Type {}", codeName, nodeType);
String nameWithoutVersion = VersionUtils.getNameWithoutVersion(nodeType.getLocalPart());
WineryVersion artifactVersion = new WineryVersion(nodeTypeVersion.getComponentVersion() + "-CloudImage", 1, 1);
ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId(OpenToscaBaseTypes.artifactTemplateNamespace, nameWithoutVersion + "-DA" + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + artifactVersion, false);
TArtifactTemplate element = repository.getElement(artifactTemplateId);
element.setType(OpenToscaBaseTypes.cloudImageArtifactType);
logger.info("Generated ArtifactTemplate {}", artifactTemplateId.getQName());
if (!repository.exists(artifactTemplateId)) {
logger.info("Trying to download image file...");
String baseUrl = "https://cloud-images.ubuntu.com/" + codeName + "/current/" + codeName + "-server-cloudimg-amd64";
try {
URL url = new URL(baseUrl + imageFileType);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
connection.disconnect();
logger.info("Image not found, trying with '-disk' suffix...");
url = new URL(baseUrl + imageDiskType);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
responseCode = connection.getResponseCode();
}
if (responseCode == 200) {
repository.setElement(artifactTemplateId, element);
ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
String fileName = url.getFile().substring(url.getFile().lastIndexOf("/") + 1);
RepositoryFileReference repositoryFileReference = new RepositoryFileReference(filesId, fileName);
try (InputStream inputStream = url.openStream()) {
repository.putContentToFile(repositoryFileReference, inputStream, MediaType.parse("application/x-image"));
}
BackendUtils.synchronizeReferences(repository, artifactTemplateId);
TDeploymentArtifact imageDa = new TDeploymentArtifact.Builder("CloudImage", OpenToscaBaseTypes.cloudImageArtifactType).setArtifactRef(artifactTemplateId.getQName()).build();
List<TDeploymentArtifact> deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
if (deploymentArtifacts == null) {
deploymentArtifacts = new ArrayList<>();
nodeTypeImplementation.setDeploymentArtifacts(deploymentArtifacts);
}
deploymentArtifacts.add(imageDa);
} else {
logger.info("Could not download image -- the URLs do not exist: \n\t{}\n\t{}", baseUrl + imageFileType, baseUrl + imageDiskType);
}
} catch (IOException e) {
logger.info("Error while downloading image file!", e);
}
}
} else {
logger.info("Could not identify code name of given Ubuntu Node Type! {}", nodeType);
}
}
}
Aggregations