Search in sources :

Example 6 with TArtifactType

use of org.eclipse.winery.model.tosca.TArtifactType 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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) WebApplicationException(javax.ws.rs.WebApplicationException) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IOException(java.io.IOException) Document(org.w3c.dom.Document) WebApplicationException(javax.ws.rs.WebApplicationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) Namespace(org.eclipse.winery.model.ids.Namespace) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NonNull(org.eclipse.jdt.annotation.NonNull) StringReader(java.io.StringReader) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) XmlId(org.eclipse.winery.model.ids.XmlId) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) IRepository(org.eclipse.winery.repository.backend.IRepository) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with TArtifactType

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

the class DASpecificationTest method getArtifactTypeOfDA.

@Test
public void getArtifactTypeOfDA() throws Exception {
    setRevisionTo("af529e513388dc9358a8f700757d8dc59aba3a55");
    ServiceTemplateId id = new ServiceTemplateId("http://winery.opentosca.org/test/servicetemplates/ponyuniverse/daspecifier", "DASpecificationTest", false);
    TTopologyTemplate topologyTemplate = this.repository.getElement(id).getTopologyTemplate();
    assertNotNull(topologyTemplate);
    TNodeTemplate nodeTemplateWithAbstractDA = topologyTemplate.getNodeTemplate("shetland_pony");
    assertNotNull(nodeTemplateWithAbstractDA);
    assertNotNull(nodeTemplateWithAbstractDA.getDeploymentArtifacts());
    TDeploymentArtifact deploymentArtifact = nodeTemplateWithAbstractDA.getDeploymentArtifacts().get(0);
    QName artifactTypeQName = deploymentArtifact.getArtifactType();
    ArtifactTypeId artifactTypeId = new ArtifactTypeId(artifactTypeQName);
    TArtifactType artifactType = this.repository.getElement(artifactTypeId);
    assertEquals(artifactType.getTargetNamespace(), DASpecification.getArtifactTypeOfDA(nodeTemplateWithAbstractDA.getDeploymentArtifacts().get(0)).getTargetNamespace());
    assertEquals(artifactType.getName(), DASpecification.getArtifactTypeOfDA(nodeTemplateWithAbstractDA.getDeploymentArtifacts().get(0)).getName());
}
Also used : ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 8 with TArtifactType

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

the class DASpecificationTest method getArtifactTypeHierarchy.

@Test
public void getArtifactTypeHierarchy() throws Exception {
    setRevisionTo("af529e513388dc9358a8f700757d8dc59aba3a55");
    ServiceTemplateId id = new ServiceTemplateId("http://winery.opentosca.org/test/servicetemplates/ponyuniverse/daspecifier", "DASpecificationTest", false);
    TTopologyTemplate topologyTemplate = this.repository.getElement(id).getTopologyTemplate();
    assertNotNull(topologyTemplate);
    TNodeTemplate nodeTemplate = topologyTemplate.getNodeTemplate("westernequipment");
    assertNotNull(nodeTemplate);
    assertNotNull(nodeTemplate.getDeploymentArtifacts());
    List<TArtifactType> artifactTypes = DASpecification.getArtifactTypeHierarchy(DASpecification.getArtifactTypeOfDA(nodeTemplate.getDeploymentArtifacts().get(0)));
    List<String> artifactTypeNames = new ArrayList<>();
    artifactTypes.forEach(at -> artifactTypeNames.add(at.getName()));
    assertEquals(2, artifactTypes.size());
    assertTrue(artifactTypeNames.contains("WesternEquipment_Pony"));
    assertTrue(artifactTypeNames.contains("PonyEquipment"));
}
Also used : TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 9 with TArtifactType

use of org.eclipse.winery.model.tosca.TArtifactType in project container by OpenTOSCA.

the class ModelUtils method getArtifactTypeHierarchy.

public static List<QName> getArtifactTypeHierarchy(final TArtifactTemplate artifactTemplate, Csar csar) {
    final List<QName> qNames = new ArrayList<>();
    final Collection<TArtifactType> artifactTypes = fetchAllArtifactTypes(csar);
    qNames.add(artifactTemplate.getType());
    TArtifactType type = findArtifactType(artifactTemplate.getType(), artifactTypes);
    TArtifactType ref = null;
    if (Objects.nonNull(type.getDerivedFrom())) {
        ref = findArtifactType(type.getDerivedFrom().getTypeRef(), artifactTypes);
    }
    while (Objects.nonNull(ref) && Objects.nonNull(ref.getDerivedFrom())) {
        qNames.add(ref.getQName());
        ref = findArtifactType(ref.getDerivedFrom().getTypeRef(), artifactTypes);
    }
    return qNames;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType)

Example 10 with TArtifactType

use of org.eclipse.winery.model.tosca.TArtifactType in project container by OpenTOSCA.

the class CsarImpl method addArtifactTemplate.

public void addArtifactTemplate(InputStream inputStream, ServiceTemplateId serviceTemplateId, String nodeTemplateId) throws IOException {
    final String artifactTypeNamespace = "http://opentosca.org/artifacttypes";
    final String artifactTypeName = "State";
    // final QName artifactTypeQName = new QName (artifactTypeNamespace, artifactTypeName);
    final String artifactTemplateNamespace = "http://opentosca.org/stateartifacttemplates";
    final String artifactTemplateName = serviceTemplateId.getQName().getLocalPart() + "_" + nodeTemplateId + "_StateArtifactTemplate";
    final QName artifactTemplateQName = new QName(artifactTemplateNamespace, artifactTemplateName);
    // ArtifactType handling
    TArtifactType artifactType = new TArtifactType.Builder(artifactTypeName).setTargetNamespace(artifactTypeNamespace).build();
    ArtifactTypeId artTypeId = new ArtifactTypeId(artifactType.getQName());
    this.wineryRepo.setElement(artTypeId, artifactType);
    // ArtifactTemplate handling
    TArtifactTemplate artifactTemplate = new TArtifactTemplate.Builder(artifactTemplateName, artifactType.getQName()).setName(artifactTemplateName).build();
    ArtifactTemplateId artTemplateId = new ArtifactTemplateId(artifactTemplateQName);
    this.wineryRepo.setElement(artTemplateId, artifactTemplate);
    ArtifactTemplateFilesDirectoryId artFileId = new ArtifactTemplateFilesDirectoryId(artTemplateId);
    RepositoryFileReference fileRef = new RepositoryFileReference(artFileId, "stateArtifact.state");
    this.wineryRepo.putContentToFile(fileRef, inputStream, MediaType.parse("application/x-state"));
    BackendUtils.synchronizeReferences(this.wineryRepo, artTemplateId);
    TServiceTemplate servTemp = this.wineryRepo.getElement(serviceTemplateId);
    for (TNodeTemplate nestedNodeTemplate : BackendUtils.getAllNestedNodeTemplates(servTemp)) {
        if (nestedNodeTemplate.getId().equals(nodeTemplateId)) {
            TDeploymentArtifact deploymentArtifact = new TDeploymentArtifact.Builder(nodeTemplateId + "_StateArtifact", artifactType.getQName()).setArtifactRef(artTemplateId.getQName()).setArtifactRef(artTemplateId.getQName()).build();
            if (nestedNodeTemplate.getDeploymentArtifacts() == null) {
                nestedNodeTemplate.setDeploymentArtifacts(new ArrayList<>());
            }
            nestedNodeTemplate.getDeploymentArtifacts().add(deploymentArtifact);
            this.wineryRepo.setElement(serviceTemplateId, servTemp);
            break;
        }
    }
    // update the ArtifactTemplates list
    this.artifactTemplates.putAll(this.wineryRepo.getQNameToElementMapping(ArtifactTemplateId.class));
}
Also used : ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Aggregations

TArtifactType (org.eclipse.winery.model.tosca.TArtifactType)10 QName (javax.xml.namespace.QName)7 ArtifactTypeId (org.eclipse.winery.model.ids.definitions.ArtifactTypeId)5 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)5 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)4 TDeploymentArtifact (org.eclipse.winery.model.tosca.TDeploymentArtifact)4 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)4 ArrayList (java.util.ArrayList)3 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)3 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)3 TNodeType (org.eclipse.winery.model.tosca.TNodeType)3 NonNull (org.eclipse.jdt.annotation.NonNull)2 TCapabilityType (org.eclipse.winery.model.tosca.TCapabilityType)2 TPolicyType (org.eclipse.winery.model.tosca.TPolicyType)2 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)2 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)2 IRepository (org.eclipse.winery.repository.backend.IRepository)2 Test (org.junit.jupiter.api.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 File (java.io.File)1