use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class SelfContainmentPackager method createSelfContainedVersion.
public DefinitionsChildId createSelfContainedVersion(DefinitionsChildId entryId) throws IOException {
ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(VersionSupport.getSelfContainedVersion(entryId));
if (!repository.exists(newServiceTemplateId)) {
repository.duplicate(entryId, newServiceTemplateId);
TServiceTemplate serviceTemplate = repository.getElement(newServiceTemplateId);
Collection<DefinitionsChildId> referencedElements = repository.getReferencedDefinitionsChildIds(newServiceTemplateId);
for (DefinitionsChildId elementId : referencedElements) {
if (elementId instanceof NodeTypeId) {
Collection<NodeTypeImplementationId> nodeTypeImplementationIds = repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, elementId.getQName());
if (nodeTypeImplementationIds.stream().noneMatch(DefinitionsChildId::isSelfContained)) {
// self-contained element does not exist yet!
List<TNodeTypeImplementation> nodeTypeImplementations = nodeTypeImplementationIds.stream().map(repository::getElement).filter(element -> element.getImplementationArtifacts() != null).collect(Collectors.toList());
for (TNodeTypeImplementation impl : nodeTypeImplementations) {
Optional<SelfContainmentPlugin> nodeTypeBasedPlugin = this.selfContainmentPlugins.stream().filter(plugin -> plugin.canHandleNodeType(elementId.getQName(), repository)).findFirst();
if (nodeTypeBasedPlugin.isPresent()) {
NodeTypeImplementationId selfContainedNodeTypeImpl = getSelfContainedNodeTypeImplId(impl);
try {
repository.duplicate(new NodeTypeImplementationId(impl.getTargetNamespace(), impl.getIdFromIdOrNameField(), false), selfContainedNodeTypeImpl);
TNodeTypeImplementation selfContained = this.repository.getElement(selfContainedNodeTypeImpl);
nodeTypeBasedPlugin.get().downloadDependenciesBasedOnNodeType(selfContained, this.repository);
repository.setElement(selfContainedNodeTypeImpl, selfContained);
} catch (IOException e) {
logger.error("While creating self-contained Node Type Implementation", e);
}
} else if (impl.getImplementationArtifacts() != null) {
createSelfContainedNodeTypeImplementation(impl);
}
}
}
} else if (elementId instanceof ArtifactTemplateId) {
if (serviceTemplate.getTopologyTemplate() != null) {
TArtifactTemplate artifactTemplate = repository.getElement(elementId);
SelfContainmentPlugin.GeneratedArtifacts generatedArtifacts = this.downloadArtifacts(elementId.getQName(), artifactTemplate.getType());
if (generatedArtifacts != null && generatedArtifacts.selfContainedArtifactQName != null) {
// first, we need to identify the element that is referencing the artifact
serviceTemplate.getTopologyTemplate().getNodeTemplates().stream().map(TNodeTemplate::getDeploymentArtifacts).filter(Objects::nonNull).filter(daList -> daList.stream().anyMatch(da -> da.getArtifactRef() != null && da.getArtifactRef().equals(elementId.getQName()))).flatMap(Collection::stream).forEach(da -> da.setArtifactRef(generatedArtifacts.selfContainedArtifactQName));
}
}
}
}
repository.setElement(newServiceTemplateId, serviceTemplate);
} else {
logger.info("Self-contained version already exists! '{}'", newServiceTemplateId.getQName());
}
return newServiceTemplateId;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class ScriptPluginTest method downloadDeps.
@Test
void downloadDeps() throws Exception {
this.setRevisionTo("origin/plain");
ScriptPlugin scriptPlugin = new ScriptPlugin();
QName originalQName = QName.valueOf("{http://opentosca.org/add/management/to/instances/nodetypeimplementations}MySQL-DBMS_5.7-Install-w1");
SelfContainmentPlugin.GeneratedArtifacts generatedArtifacts = scriptPlugin.downloadDependenciesBasedOnArtifact(originalQName, RepositoryFactory.getRepository());
ArtifactTemplateId selfContainedId = new ArtifactTemplateId("http://opentosca.org/add/management/to/instances/nodetypeimplementations", "MySQL-DBMS_5.7-Install-w1-selfContained-w1-wip1", false);
ArtifactTemplateId generatedDAId = new ArtifactTemplateId("http://opentosca.org/add/management/to/instances/nodetypeimplementations", "MySQL-DBMS_5.7-Install-w1-mysql-server-5.7-DA-w1-wip1", false);
assertNotNull(generatedArtifacts);
assertEquals(1, generatedArtifacts.deploymentArtifactsToAdd.size());
assertEquals(generatedDAId.getQName(), generatedArtifacts.deploymentArtifactsToAdd.get(0));
assertEquals(ToscaBaseTypes.archiveArtifactType, repository.getElement(generatedDAId).getType());
assertEquals(originalQName, generatedArtifacts.artifactToReplaceQName);
assertEquals(selfContainedId.getQName(), generatedArtifacts.selfContainedArtifactQName);
assertTrue(repository.exists(selfContainedId));
assertTrue(repository.exists(generatedDAId));
ArtifactTemplateFilesDirectoryId installScriptId = new ArtifactTemplateFilesDirectoryId(selfContainedId);
SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(installScriptId);
assertEquals(1, containedFiles.size());
String targetFileStr = IOUtils.toString(repository.newInputStream(containedFiles.first()), StandardCharsets.UTF_8);
assertEquals("#!/bin/sh\n" + "sudo sh -c \"echo '127.0.0.1' $(hostname) >> /etc/hosts\";\n" + "sudo apt-get update -qq;\n" + "# disables setting the root password with gui, root password etc. will be set in the configure.sh\n" + "export DEBIAN_FRONTEND=noninteractive;\n" + "#//Self-contained CSAR//mysql-server-5.7.tar.gz\n" + "innerCsarRoot=$(find ~ -maxdepth 1 -path \"*.csar\");\n" + "IFS=';' read -ra NAMES <<< \"$DAs\";\n" + "for k in \"${NAMES[@]}\"; do\n" + " IFS=',' read -ra PATH <<< \"$k\"; \n" + " selfContainedDaDirName=$(/usr/bin/sudo /usr/bin/dirname $innerCsarRoot${PATH[1]})\n" + " selfContainedDaName=$(/usr/bin/sudo /usr/bin/basename $innerCsarRoot${PATH[1]})\n" + " selfContainedExtractFolderName=\"${selfContainedDaName%.*}\"\n" + " if [[ \"${PATH[1]}\" == *mysql-server-5.7.tar.gz ]];\n" + " then\n" + " /usr/bin/sudo mkdir -p \"$selfContainedDaDirName/$selfContainedExtractFolderName\"\n" + " /bin/tar -xvzf \"$selfContainedDaDirName/$selfContainedDaName\" -C \"$selfContainedDaDirName/$selfContainedExtractFolderName\"\n" + " break\n" + " fi\n" + "done\n" + "export DEBIAN_FRONTEND=noninteractive\n" + "/usr/bin/sudo -E /usr/bin/dpkg -i -R -E -B \"$selfContainedDaDirName/$selfContainedExtractFolderName\"\n", targetFileStr);
ArtifactTemplateFilesDirectoryId daId = new ArtifactTemplateFilesDirectoryId(generatedDAId);
containedFiles = repository.getContainedFiles(daId);
assertEquals(1, containedFiles.size());
assertEquals("mysql-server-5.7.tar.gz", containedFiles.first().getFileName());
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class DataFlowResource method createNodeTemplate.
/**
* Create a NodeTemplate corresponding to the given filter with the given type, properties and artifacts and add it
* to the topology of the incomplete deployment model.
*/
private TTopologyTemplate createNodeTemplate(TTopologyTemplate topology, NodeTypeId nodeTypeId, String templateName, Map<String, String> properties, List<QName> artifacts) {
// get NodeType to access Requirements for the completion and available properties
IRepository repo = RepositoryFactory.getRepository();
TNodeType nodeType = repo.getElement(nodeTypeId);
if (Objects.isNull(nodeType)) {
return null;
}
TNodeTemplate.Builder templateBuilder = new TNodeTemplate.Builder(templateName, nodeTypeId.getQName());
// add the defined properties to the NodeTemplate
if (Objects.nonNull(properties)) {
LinkedHashMap<String, String> propertyList = new LinkedHashMap<>();
if (Objects.nonNull(nodeType.getWinerysPropertiesDefinition())) {
// add empty property for NodeType properties to avoid errors due to missing properties
WinerysPropertiesDefinition def = nodeType.getWinerysPropertiesDefinition();
for (PropertyDefinitionKV prop : def.getPropertyDefinitions()) {
propertyList.put(prop.getKey(), "");
}
}
// add all properties which are defined at the filter
propertyList.putAll(properties);
TEntityTemplate.WineryKVProperties nodeProperties = new TEntityTemplate.WineryKVProperties();
nodeProperties.setKVProperties(propertyList);
templateBuilder.setProperties(nodeProperties);
}
// add all requirements which are defined by the corresponding NodeType
if (nodeType.getRequirementDefinitions() != null) {
for (TRequirementDefinition requirementDef : nodeType.getRequirementDefinitions()) {
String requirementId = templateName + "-" + requirementDef.getName();
templateBuilder.addRequirement(new TRequirement.Builder(requirementId, requirementDef.getName(), requirementDef.getRequirementType()).build());
}
}
// add the DAs to the NodeTemplate
if (Objects.nonNull(artifacts) && !artifacts.isEmpty()) {
LOGGER.debug("{} artifacts specified for filter {}", artifacts.size(), templateName);
// get the IDs of all available ArtifactTemplates
List<ArtifactTemplateId> artifactTemplateIds = repo.getAllDefinitionsChildIds().stream().filter(id -> id.getGroup().equals(ARTIFACT_TEMPLATE_GROUP) && id instanceof ArtifactTemplateId).map(id -> (ArtifactTemplateId) id).collect(Collectors.toList());
for (QName artifactName : artifacts) {
Optional<ArtifactTemplateId> idOptional = artifactTemplateIds.stream().filter(id -> id.getQName().equals(artifactName)).findFirst();
if (idOptional.isPresent()) {
ArtifactTemplateId artifactTemplateId = idOptional.get();
TArtifactTemplate artifactTemplate = repo.getElement(artifactTemplateId);
templateBuilder.addDeploymentArtifact(new TDeploymentArtifact.Builder(artifactName.toString(), artifactTemplate.getType()).setArtifactRef(artifactName).build());
} else {
LOGGER.warn("Filter '{}' specifies DA with name '{}' but no such artifact available in repository!", templateName, artifactName);
}
}
}
topology.addNodeTemplate(templateBuilder.build());
return topology;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class GenericArtifactsResource method generateArtifact.
/**
* @return TImplementationArtifact | TDeploymentArtifact (XML) | URL of generated IA zip (in case of autoGenerateIA)
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a new implementation/deployment artifact. If an implementation artifact with the same name already exists, it is <em>overridden</em>.")
@SuppressWarnings("unchecked")
public Response generateArtifact(GenerateArtifactApiData apiData, @Context UriInfo uriInfo) {
// we assume that the parent ComponentInstance container exists
final IRepository repository = RepositoryFactory.getRepository();
if (StringUtils.isEmpty(apiData.artifactName)) {
return Response.status(Status.BAD_REQUEST).entity("Empty artifactName").build();
}
if (StringUtils.isEmpty(apiData.artifactType)) {
if (StringUtils.isEmpty(apiData.artifactTemplateName) || StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
if (StringUtils.isEmpty(apiData.artifactTemplate)) {
return Response.status(Status.BAD_REQUEST).entity("No artifact type given and no template given. Cannot guess artifact type").build();
}
}
}
if (!StringUtils.isEmpty(apiData.autoGenerateIA)) {
if (StringUtils.isEmpty(apiData.javaPackage)) {
return Response.status(Status.BAD_REQUEST).entity("no java package name supplied for IA auto generation.").build();
}
if (StringUtils.isEmpty(apiData.interfaceName)) {
return Response.status(Status.BAD_REQUEST).entity("no interface name supplied for IA auto generation.").build();
}
}
// convert second calling form to first calling form
if (!StringUtils.isEmpty(apiData.artifactTemplate)) {
QName qname = QName.valueOf(apiData.artifactTemplate);
apiData.artifactTemplateName = qname.getLocalPart();
apiData.artifactTemplateNamespace = qname.getNamespaceURI();
}
Document doc = null;
// if invalid, abort and do not create anything
if (!StringUtils.isEmpty(apiData.artifactSpecificContent)) {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
StringReader sr = new StringReader(apiData.artifactSpecificContent);
is.setCharacterStream(sr);
doc = db.parse(is);
} catch (Exception e) {
// FIXME: currently we allow a single element only. However, the content should be internally wrapped by an (arbitrary) XML element as the content will be nested in the artifact element, too
LOGGER.debug("Invalid content", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
}
}
// determine artifactTemplate and artifactType
ArtifactTypeId artifactTypeId;
ArtifactTemplateId artifactTemplateId = null;
boolean doAutoCreateArtifactTemplate = !(StringUtils.isEmpty(apiData.autoCreateArtifactTemplate) || apiData.autoCreateArtifactTemplate.equalsIgnoreCase("no") || apiData.autoCreateArtifactTemplate.equalsIgnoreCase("false"));
if (!doAutoCreateArtifactTemplate) {
// no auto creation
if (!StringUtils.isEmpty(apiData.artifactTemplateName) && !StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
QName artifactTemplateQName = new QName(apiData.artifactTemplateNamespace, apiData.artifactTemplateName);
artifactTemplateId = BackendUtils.getDefinitionsChildId(ArtifactTemplateId.class, artifactTemplateQName);
}
if (StringUtils.isEmpty(apiData.artifactType)) {
// derive the type from the artifact template
if (artifactTemplateId == null) {
return Response.status(Status.NOT_ACCEPTABLE).entity("No artifactTemplate and no artifactType provided. Deriving the artifactType is not possible.").build();
}
@NonNull final QName type = repository.getElement(artifactTemplateId).getType();
artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, type);
} else {
// artifactTypeStr is directly given, use that
artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, apiData.artifactType);
}
} else {
if (StringUtils.isEmpty(apiData.artifactType)) {
return Response.status(Status.BAD_REQUEST).entity("Artifact template auto creation requested, but no artifact type supplied.").build();
}
artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, apiData.artifactType);
// ensure that given type exists
if (!repository.exists(artifactTypeId)) {
LOGGER.debug("Artifact type {} is created", apiData.artifactType);
final TArtifactType element = repository.getElement(artifactTypeId);
try {
repository.setElement(artifactTypeId, element);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
if (StringUtils.isEmpty(apiData.artifactTemplateName) || StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
// no explicit name provided
// we use the artifactNameStr as prefix for the
// artifact template name
// we create a new artifact template in the namespace of the parent
// element
Namespace namespace = this.resWithNamespace.getNamespace();
artifactTemplateId = new ArtifactTemplateId(namespace, new XmlId(apiData.artifactName + "artifactTemplate", false));
} else {
QName artifactTemplateQName = new QName(apiData.artifactTemplateNamespace, apiData.artifactTemplateName);
artifactTemplateId = new ArtifactTemplateId(artifactTemplateQName);
}
// even if artifactTemplate does not exist, it is loaded
final TArtifactTemplate artifactTemplate = repository.getElement(artifactTemplateId);
artifactTemplate.setType(artifactTypeId.getQName());
try {
repository.setElement(artifactTemplateId, artifactTemplate);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
// variable artifactTypeId is set
// variable artifactTemplateId is not null if artifactTemplate has been generated
// we have to generate the DA/IA resource now
// Doing it here instead of doing it at the subclasses is dirty on the
// one hand, but quicker to implement on the other hand
// Create the artifact itself
ArtifactT resultingArtifact;
if (this instanceof ImplementationArtifactsResource) {
TImplementationArtifact.Builder builder = new TImplementationArtifact.Builder(artifactTypeId.getQName()).setName(apiData.artifactName).setInterfaceName(apiData.interfaceName).setOperationName(apiData.operationName);
if (artifactTemplateId != null) {
builder.setArtifactRef(artifactTemplateId.getQName());
}
if (doc != null) {
// the content has been checked for validity at the beginning of the method.
// If this point in the code is reached, the XML has been parsed into doc
// just copy over the dom node. Hopefully, that works...
builder.setAny(Collections.singletonList(doc.getDocumentElement()));
}
resultingArtifact = (ArtifactT) builder.build();
} else {
// for comments see other branch
TDeploymentArtifact.Builder builder = new TDeploymentArtifact.Builder(apiData.artifactName, artifactTypeId.getQName());
if (artifactTemplateId != null) {
builder.setArtifactRef(artifactTemplateId.getQName());
}
if (doc != null) {
builder.setAny(Collections.singletonList(doc.getDocumentElement()));
}
resultingArtifact = (ArtifactT) builder.build();
}
this.list.add(resultingArtifact);
// TODO: Check for error, and in case one found return it
RestUtils.persist(super.res);
if (StringUtils.isEmpty(apiData.autoGenerateIA)) {
// No IA generation
return Response.created(URI.create(EncodingUtil.URLencode(apiData.artifactName))).entity(resultingArtifact).build();
} else {
// after everything was created, we fire up the artifact generation
return this.generateImplementationArtifact(apiData.interfaceName, apiData.javaPackage, uriInfo, artifactTemplateId);
}
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class BackendUtils method getGitInformation.
/**
* @param directoryId DirectoryId of the ArtifactTemplate that should contain a reference to a git repository.
* @return The URL and the branch/tag that contains the files for the ArtifactTemplate. null if no git information
* is given.
*/
public static GitInfo getGitInformation(DirectoryId directoryId, IRepository repo) {
if (!(directoryId.getParent() instanceof ArtifactTemplateId)) {
return null;
}
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
try {
TDefinitions defs = repo.definitionsFromRef(ref);
Map<QName, String> attributes = defs.getOtherAttributes();
String src = attributes.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitsrc"));
String branch = attributes.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitbranch"));
if (src == null && branch == null) {
return null;
}
if (src == null || branch == null) {
LOGGER.error("Git information not complete, URL or branch missing");
return null;
}
return new GitInfo(src, branch);
} catch (IOException e) {
LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
}
return null;
}
Aggregations