use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class AbstractFileBasedRepository method getAllIdsInNamespace.
public Collection<? extends DefinitionsChildId> getAllIdsInNamespace(Class<? extends DefinitionsChildId> clazz, Namespace namespace) {
Collection<DefinitionsChildId> result = new HashSet<>();
String rootPathFragment = IdUtil.getRootPathFragment(clazz);
Path dir = this.getRepositoryRoot().resolve(rootPathFragment);
dir = dir.resolve(namespace.getEncoded());
if (Files.exists(dir) && Files.isDirectory(dir)) {
DirectoryStream<Path> directoryStream;
try {
directoryStream = Files.newDirectoryStream(dir);
for (Path path : directoryStream) {
Constructor<? extends DefinitionsChildId> constructor = clazz.getConstructor(String.class, String.class, boolean.class);
DefinitionsChildId definitionsChildId = constructor.newInstance(namespace.getDecoded(), path.getFileName().toString(), false);
result.add(definitionsChildId);
}
directoryStream.close();
} catch (IOException e) {
LOGGER.debug("Cannot close ds", e);
} catch (NoSuchMethodException e) {
LOGGER.debug("Cannot find constructor", e);
} catch (InstantiationException e) {
LOGGER.debug("Cannot instantiate object", e);
} catch (IllegalAccessException e) {
LOGGER.debug("IllegalAccessException", e);
} catch (InvocationTargetException e) {
LOGGER.debug("InvocationTargetException", e);
}
}
return result;
}
use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class AbstractFileBasedRepository method forceDelete.
public void forceDelete(Class<? extends DefinitionsChildId> definitionsChildIdClazz, Namespace namespace) {
// instantiate new definitions child id with "ID" as id
// this is used to get the absolute path
DefinitionsChildId id = BackendUtils.getDefinitionsChildId(definitionsChildIdClazz, namespace.getEncoded(), "ID", true);
Path path = this.id2AbsolutePath(id);
// do not delete the id, delete the complete namespace
// patrent folder is the namespace folder
path = path.getParent();
FileUtils.forceDelete(path);
}
use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class WriterUtils method storeDefinitions.
// FIXME this used to be targeting Definitions, check whether that was necessary
public static void storeDefinitions(IRepository repository, TDefinitions definitions, boolean overwrite, Path dir) {
Path path = null;
try {
path = Files.createTempDirectory("winery");
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug("Store definition: {}", definitions.getId());
saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
TDefinitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
CsarImporter csarImporter = new CsarImporter(repository);
List<Exception> exceptions = new ArrayList<>();
cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
csarImporter.setNamespace(entry, namespace);
String id = ModelUtilities.getId(entry);
Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
if (repository.exists(wid)) {
if (overwrite) {
try {
repository.forceDelete(wid);
} catch (IOException e) {
exceptions.add(e);
}
} else {
return;
}
}
if (entry instanceof TArtifactTemplate) {
List<TArtifactReference> artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
artifactReferences.stream().filter(Objects::nonNull).forEach(ref -> {
String reference = ref.getReference();
URI refURI;
try {
refURI = new URI(reference);
} catch (URISyntaxException e) {
LOGGER.error("Invalid URI {}", reference);
return;
}
if (refURI.isAbsolute()) {
return;
}
Path artifactPath = dir.resolve(reference);
if (!Files.exists(artifactPath)) {
LOGGER.error("File not found {}", artifactPath);
return;
}
ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
try (InputStream is = Files.newInputStream(artifactPath);
BufferedInputStream bis = new BufferedInputStream(is)) {
MediaType mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
repository.putContentToFile(aFile, bis, mediaType);
} catch (IOException e) {
LOGGER.error("Could not read artifact template file: {}", artifactPath);
}
});
}
final TDefinitions part = BackendUtils.createWrapperDefinitions(wid, repository);
part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
String content = BackendUtils.getXMLAsString(part, true, repository);
try {
repository.putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (Exception e) {
exceptions.add(e);
}
});
}
use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId 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.DefinitionsChildId in project winery by eclipse.
the class CsarExporterTest method testPutCsarInBlockchainAndImmutableStorage.
@Test
@Disabled
public // todo check how to enable
void testPutCsarInBlockchainAndImmutableStorage() throws Exception {
setRevisionTo("origin/plain");
CsarExporter exporter = new CsarExporter(repository);
DefinitionsChildId id = new ServiceTemplateId("http://plain.winery.opentosca.org/servicetemplates", "ServiceTemplateWithAllReqCapVariants", false);
ByteArrayOutputStream os = new ByteArrayOutputStream();
CompletableFuture<String> future = exporter.writeCsarAndSaveManifestInProvenanceLayer(id, os);
String transactionHash = future.get();
assertNotNull(transactionHash);
try (InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
ZipInputStream zis = new ZipInputStream(inputStream)) {
ManifestContents manifestContents = parseManifest(zis);
assertNotNull(manifestContents);
for (String section : manifestContents.getSectionNames()) {
assertNotNull(manifestContents.getAttributesForSection(section).get(TOSCAMetaFileAttributes.IMMUTABLE_ADDRESS));
}
}
}
Aggregations