Search in sources :

Example 36 with DefinitionsChildId

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;
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 37 with DefinitionsChildId

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);
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId)

Example 38 with DefinitionsChildId

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);
        }
    });
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Example 39 with DefinitionsChildId

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;
}
Also used : TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Collection(java.util.Collection) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Objects(java.util.Objects) List(java.util.List) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) VersionSupport(org.eclipse.winery.model.version.VersionSupport) IRepository(org.eclipse.winery.repository.backend.IRepository) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) Optional(java.util.Optional) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) QName(javax.xml.namespace.QName) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) IOException(java.io.IOException) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) Objects(java.util.Objects) Collection(java.util.Collection) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 40 with DefinitionsChildId

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));
        }
    }
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) ZipInputStream(java.util.zip.ZipInputStream) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)77 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)29 Test (org.junit.jupiter.api.Test)21 QName (javax.xml.namespace.QName)19 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)16 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)16 HashMap (java.util.HashMap)15 IRepository (org.eclipse.winery.repository.backend.IRepository)14 TNodeType (org.eclipse.winery.model.tosca.TNodeType)13 Path (java.nio.file.Path)12 Map (java.util.Map)12 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)12 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)12 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 InputStream (java.io.InputStream)10 HashSet (java.util.HashSet)10 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)10