Search in sources :

Example 1 with Definitions

use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.

the class BackendUtils method getTArtifactTemplate.

/**
 * @param directoryId DirectoryID of the TArtifactTemplate that should be returned.
 * @return The TArtifactTemplate corresponding to the directoryId.
 */
public static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId) {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
    try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
        Unmarshaller u = JAXBSupport.createUnmarshaller();
        Definitions defs = ((Definitions) u.unmarshal(is));
        for (TExtensibleElements elem : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
            if (elem instanceof TArtifactTemplate) {
                return (TArtifactTemplate) elem;
            }
        }
    } catch (IOException e) {
        LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
    } catch (JAXBException e) {
        LOGGER.error("Error in XML in " + ref.getFileName(), e);
    }
    return null;
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) JAXBException(javax.xml.bind.JAXBException) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with Definitions

use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.

the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.

public static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
    final Definitions definitions = createWrapperDefinitions(id);
    HasIdInIdOrNameField element;
    if (id instanceof RelationshipTypeImplementationId) {
        element = new TRelationshipTypeImplementation();
    } else if (id instanceof NodeTypeImplementationId) {
        element = new TNodeTypeImplementation();
    } else if (id instanceof RequirementTypeId) {
        element = new TRequirementType();
    } else if (id instanceof NodeTypeId) {
        element = new TNodeType();
    } else if (id instanceof RelationshipTypeId) {
        element = new TRelationshipType();
    } else if (id instanceof CapabilityTypeId) {
        element = new TCapabilityType();
    } else if (id instanceof ArtifactTypeId) {
        element = new TArtifactType();
    } else if (id instanceof PolicyTypeId) {
        element = new TPolicyType();
    } else if (id instanceof PolicyTemplateId) {
        element = new TPolicyTemplate();
    } else if (id instanceof ServiceTemplateId) {
        element = new TServiceTemplate();
    } else if (id instanceof ArtifactTemplateId) {
        element = new TArtifactTemplate();
    } else if (id instanceof XSDImportId) {
        // TImport has no id; thus directly generating it without setting an id
        TImport tImport = new TImport();
        definitions.setElement(tImport);
        return definitions;
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
    copyIdToFields(element, id);
    definitions.setElement((TExtensibleElements) element);
    return definitions;
}
Also used : ArtifactTypeId(org.eclipse.winery.common.ids.definitions.ArtifactTypeId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TImport(org.eclipse.winery.model.tosca.TImport) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) NodeTypeImplementationId(org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId) XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) RelationshipTypeImplementationId(org.eclipse.winery.common.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PolicyTypeId(org.eclipse.winery.common.ids.definitions.PolicyTypeId) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId)

Example 3 with Definitions

use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.

the class IGenericRepository method setElement.

/**
 * Updates the element belonging to the given DefinitionsChildId Regenerates wrapper definitions; thus all
 * extensions at the wrapper definitions are lost
 *
 * @param id      the DefinitionsChildId to update
 * @param element the element to set
 * @throws IOException if persisting went wrong
 */
default void setElement(DefinitionsChildId id, TExtensibleElements element) throws IOException {
    // default implementation on the server side
    // the client side has to use the REST method
    Definitions definitions = BackendUtils.createWrapperDefinitions(id);
    definitions.setElement(element);
    BackendUtils.persist(id, definitions);
}
Also used : TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions)

Example 4 with Definitions

use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.

the class ImportUtils method getTheImport.

/**
 * SIDE EFFECT: persists the import when something is changed
 */
public static Optional<TImport> getTheImport(GenericImportId id) {
    Objects.requireNonNull(id);
    TImport theImport;
    boolean needsPersistence = false;
    final IRepository repository = RepositoryFactory.getRepository();
    final Definitions definitions = repository.getDefinitions(id);
    if (!repository.exists(id)) {
        return Optional.empty();
    }
    if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
        // definitions exist
        // we have to manually assign our import right
        theImport = definitions.getImport().get(0);
    } else {
        // someone created a new import
        // store it locally
        theImport = (TImport) definitions.getElement();
        // undo the side effect of adding it at the wrong place at TDefinitions
        definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
        // add import at the right place
        definitions.getImport().add(theImport);
        // Super class has persisted the definitions
        // We have to persist the new variant
        needsPersistence = true;
    }
    if (theImport.getLocation() == null) {
        // invalid import -- try to synchronize with storage
        SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(id);
        // we are only interested in the non-.definitions
        for (RepositoryFileReference ref : containedFiles) {
            if (!ref.getFileName().endsWith(".definitions")) {
                // associated file found
                // set the filename of the import to the found xsd
                // TODO: no more validity checks are done currently. In the case of XSD: targetNamespace matches, not more than one xsd
                theImport.setLocation(ref.getFileName());
                needsPersistence = true;
                break;
            }
        }
    }
    if (needsPersistence) {
        try {
            BackendUtils.persist(id, definitions);
        } catch (IOException e) {
            LOGGER.error("Could not persist changes", e);
        }
    }
    return Optional.of(theImport);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Definitions(org.eclipse.winery.model.tosca.Definitions) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException)

Example 5 with Definitions

use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.

the class FilebasedRepository method rename.

@Override
public void rename(DefinitionsChildId oldId, DefinitionsChildId newId) throws IOException {
    Objects.requireNonNull(oldId);
    Objects.requireNonNull(newId);
    if (oldId.equals(newId)) {
        // we do not do anything - even not throwing an error
        return;
    }
    Definitions definitions = this.getDefinitions(oldId);
    RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
    RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
    // oldRef points to the definitions file,
    // getParent() returns the directory
    // we need toFile(), because we rely on FileUtils.moveDirectoryToDirectory
    File oldDir = this.id2AbsolutePath(oldRef.getParent()).toFile();
    File newDir = this.id2AbsolutePath(newRef.getParent()).toFile();
    org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);
    // Update definitions and store it
    // This also updates the definitions of componentInstanceResource
    BackendUtils.updateWrapperDefinitions(newId, definitions);
    // This works, because the definitions object here is the same as the definitions object treated at copyIdToFields
    // newId has to be passed, because the id is final at AbstractComponentInstanceResource
    BackendUtils.copyIdToFields((HasIdInIdOrNameField) definitions.getElement(), newId);
    try {
        BackendUtils.persist(definitions, newRef, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
    } catch (InvalidPathException e) {
        LOGGER.debug("Invalid path during write", e);
    // QUICK FIX
    // Somewhere, the first letter is deleted --> /odetypes/http%3A%2F%2Fwww.example.org%2F05/
    // We just ignore it for now
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Definitions(org.eclipse.winery.model.tosca.Definitions) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException)

Aggregations

Definitions (org.eclipse.winery.model.tosca.Definitions)21 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)7 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)7 IOException (java.io.IOException)6 JAXBException (javax.xml.bind.JAXBException)5 QName (javax.xml.namespace.QName)5 Test (org.junit.Test)5 Unmarshaller (javax.xml.bind.Unmarshaller)4 TServiceTemplate (org.eclipse.winery.model.tosca.yaml.TServiceTemplate)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)3 PolicyTemplateId (org.eclipse.winery.common.ids.definitions.PolicyTemplateId)3 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)3 TEntityType (org.eclipse.winery.model.tosca.TEntityType)3 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)3 TImport (org.eclipse.winery.model.tosca.TImport)3 TPolicyTemplate (org.eclipse.winery.model.tosca.TPolicyTemplate)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2