Search in sources :

Example 81 with ServiceTemplateId

use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.

the class Allocation method save.

private List<ServiceTemplateId> save(ServiceTemplateId originalId, List<TopologyWrapper> allocatedTopologies, String suffix) throws IOException, AllocationException {
    if (allocatedTopologies.isEmpty()) {
        throw new AllocationException("No topologies were created");
    }
    List<ServiceTemplateId> allocatedIds = new ArrayList<>();
    for (int i = 0; i < allocatedTopologies.size(); i++) {
        // generate id
        ServiceTemplateId allocatedId = new ServiceTemplateId(originalId.getNamespace().getDecoded(), originalId.getXmlId().getDecoded() + "-" + suffix + (i + 1), false);
        repository.forceDelete(allocatedId);
        repository.flagAsExisting(allocatedId);
        // generate and save service template
        TServiceTemplate allocated = createServiceTemplate(allocatedTopologies.get(i));
        repository.setElement(allocatedId, allocated);
        allocatedIds.add(allocatedId);
    }
    LOGGER.debug("Saved " + allocatedIds.size() + " allocated topologies");
    return allocatedIds;
}
Also used : ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) AllocationException(org.eclipse.winery.repository.exceptions.AllocationException) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 82 with ServiceTemplateId

use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.

the class Splitting method composeServiceTemplates.

/**
 */
public ServiceTemplateId composeServiceTemplates(String composedSolutionServiceTemplateID, List<ServiceTemplateId> serviceTemplateIds) throws IOException, SplittingException {
    IRepository repository = RepositoryFactory.getRepository();
    String solutionNamespace = "http://www.opentosca.org/solutions/";
    // create composed service template
    ServiceTemplateId composedServiceTemplateId = new ServiceTemplateId(solutionNamespace, composedSolutionServiceTemplateID, false);
    repository.forceDelete(composedServiceTemplateId);
    repository.flagAsExisting(composedServiceTemplateId);
    TServiceTemplate composedServiceTemplate = new TServiceTemplate();
    composedServiceTemplate.setName(composedServiceTemplateId.getXmlId().getDecoded());
    composedServiceTemplate.setId(composedServiceTemplate.getName());
    composedServiceTemplate.setTargetNamespace(solutionNamespace);
    TTopologyTemplate composedTopologyTemplate = new TTopologyTemplate();
    composedServiceTemplate.setTopologyTemplate(composedTopologyTemplate);
    repository.setElement(composedServiceTemplateId, composedServiceTemplate);
    // add all node and relationship templates from the solution fragements to the composed topology template
    for (ServiceTemplateId id : serviceTemplateIds) {
        BackendUtils.mergeTopologyTemplateAinTopologyTemplateB(id, composedServiceTemplateId, repository);
    }
    composedServiceTemplate = repository.getElement(composedServiceTemplateId);
    composedTopologyTemplate = composedServiceTemplate.getTopologyTemplate();
    List<TRequirement> openRequirements = getOpenRequirements(composedTopologyTemplate);
    for (TRequirement requirement : openRequirements) {
        QName requiredCapabilityTypeQName = getRequiredCapabilityTypeQNameOfRequirement(requirement);
        TNodeTemplate nodeWithOpenCapability = composedTopologyTemplate.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).filter(nt -> nt.getCapabilities().stream().anyMatch(c -> c.getType().equals(requiredCapabilityTypeQName))).findFirst().orElse(null);
        if (nodeWithOpenCapability != null) {
            TCapability matchingCapability = nodeWithOpenCapability.getCapabilities().stream().filter(c -> c.getType().equals(requiredCapabilityTypeQName)).findFirst().get();
            TRelationshipType matchingRelationshipType = getMatchingRelationshipType(requirement, matchingCapability);
            if (matchingRelationshipType != null) {
                addMatchingRelationshipTemplateToTopologyTemplate(composedTopologyTemplate, matchingRelationshipType, requirement, matchingCapability);
            } else {
                throw new SplittingException("No suitable relationship type found for matching");
            }
        }
    }
    LOGGER.debug("Persisting...");
    repository.setElement(composedServiceTemplateId, composedServiceTemplate);
    LOGGER.debug("Persisted.");
    return composedServiceTemplateId;
}
Also used : SortedSet(java.util.SortedSet) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) TParameter(org.eclipse.winery.model.tosca.TParameter) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) VersionSupport(org.eclipse.winery.model.version.VersionSupport) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Map(java.util.Map) TRequirement(org.eclipse.winery.model.tosca.TRequirement) Util(org.eclipse.winery.repository.common.Util) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ListUtils.listIsNotNullOrEmpty(org.eclipse.winery.common.ListUtils.listIsNotNullOrEmpty) Set(java.util.Set) IOException(java.io.IOException) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Collectors(java.util.stream.Collectors) TInterface(org.eclipse.winery.model.tosca.TInterface) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) Objects(java.util.Objects) List(java.util.List) DASpecification(org.eclipse.winery.repository.driverspecificationandinjection.DASpecification) TCapability(org.eclipse.winery.model.tosca.TCapability) IRepository(org.eclipse.winery.repository.backend.IRepository) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) Optional(java.util.Optional) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TOperation(org.eclipse.winery.model.tosca.TOperation) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) DriverInjection(org.eclipse.winery.repository.driverspecificationandinjection.DriverInjection) QName(javax.xml.namespace.QName) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) QName(javax.xml.namespace.QName) TCapability(org.eclipse.winery.model.tosca.TCapability) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 83 with ServiceTemplateId

use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.

the class RestUtils method cloneServiceTemplate.

public static ServiceTemplateId cloneServiceTemplate(ServiceTemplateId serviceTemplate, String newName, String artifactName) throws JAXBException, IllegalArgumentException, IOException {
    ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(serviceTemplate.getNamespace().getDecoded(), newName, false);
    RepositoryFileReference fileRef = new RepositoryFileReference(newServiceTemplateId, "ServiceTemplate.tosca");
    TDefinitions defs = new ServiceTemplateResource(serviceTemplate).getDefinitions();
    defs.setId(newName + "Definitions");
    defs.setName(newName + "Definitions generated from Artifact " + artifactName);
    TServiceTemplate oldSTModel = null;
    for (TExtensibleElements el : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
        if (el instanceof TServiceTemplate) {
            oldSTModel = (TServiceTemplate) el;
        }
    }
    if (oldSTModel != null) {
        oldSTModel.setId(newName);
        oldSTModel.setName(newName + " generated from Artifact " + artifactName);
        // remove xaaspackager tags
        Collection<TTag> toRemove = new ArrayList<>();
        for (TTag tag : oldSTModel.getTags()) {
            switch(tag.getName()) {
                case "xaasPackageNode":
                case "xaasPackageArtifactType":
                case "xaasPackageDeploymentArtifact":
                    toRemove.add(tag);
                    break;
                default:
                    break;
            }
        }
        oldSTModel.getTags().removeAll(toRemove);
    }
    JAXBContext context = JAXBContext.newInstance(TDefinitions.class);
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    m.marshal(defs, sw);
    String xmlString = sw.toString();
    RepositoryFactory.getRepository().putContentToFile(fileRef, xmlString, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
    return newServiceTemplateId;
}
Also used : TTag(org.eclipse.winery.model.tosca.TTag) Marshaller(javax.xml.bind.Marshaller) ArrayList(java.util.ArrayList) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) JAXBContext(javax.xml.bind.JAXBContext) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) StringWriter(java.io.StringWriter) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 84 with ServiceTemplateId

use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.

the class RestUtils method create.

/**
 * Generates given TOSCA element and returns appropriate response code <br  />
 * <p>
 * In the case of an existing resource, the other possible return code is 302. This code has no Status constant,
 * therefore we use Status.CONFLICT, which is also possible.
 *
 * @return <ul> <li> <ul> <li>Status.CREATED (201) if the resource has been created,</li> <li>Status.CONFLICT if the
 * resource already exists,</li> <li>Status.INTERNAL_SERVER_ERROR (500) if something went wrong</li> </ul> </li>
 * <li>URI: the absolute URI of the newly created resource</li> </ul>
 */
public static ResourceResult create(GenericId id, String name) {
    ResourceResult res = new ResourceResult();
    if (RepositoryFactory.getRepository().exists(id)) {
        // res.setStatus(302);
        res.setStatus(Status.CONFLICT);
    } else {
        if (RepositoryFactory.getRepository().flagAsExisting(id)) {
            res.setStatus(Status.CREATED);
            // @formatter:off
            // This method is a generic method
            // We cannot return an "absolute" URL as the URL is always
            // relative to the caller
            // Does not work: String path = Environment.getUrlConfiguration().getRepositoryApiUrl()
            // + "/" +
            // Utils.getUrlPathForPathInsideRepo(id.getPathInsideRepo());
            // We distinguish between two cases: DefinitionsChildId and
            // TOSCAelementId
            // @formatter:on
            String path;
            if (id instanceof DefinitionsChildId) {
                // here, we return namespace + id, as it is only possible to
                // post on the definition child*s* resource to create an
                // instance of a definition child
                DefinitionsChildId tcId = (DefinitionsChildId) id;
                path = tcId.getNamespace().getEncoded() + "/" + tcId.getXmlId().getEncoded() + "/";
                // in case the resource additionally supports a name attribute, we set the original name
                if ((tcId instanceof ServiceTemplateId) || (tcId instanceof ArtifactTemplateId) || (tcId instanceof PolicyTemplateId)) {
                    // these three types have an additional name (instead of a pure id)
                    // we store the name
                    IHasName resource = (IHasName) AbstractComponentsResource.getComponentInstanceResource(tcId);
                    resource.setName(name);
                }
            } else {
                assert (id instanceof ToscaElementId);
                // We just return the id as we assume that only the parent
                // of this id may create sub elements
                path = id.getXmlId().getEncoded() + "/";
            }
            // we have to encode it twice to get correct URIs
            path = Util.getUrlPath(path);
            URI uri = URI.create(path);
            res.setUri(uri);
            res.setId(id);
        } else {
            res.setStatus(Status.INTERNAL_SERVER_ERROR);
        }
    }
    return res;
}
Also used : ToscaElementId(org.eclipse.winery.model.ids.elements.ToscaElementId) ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) IHasName(org.eclipse.winery.repository.rest.resources._support.IHasName) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) URI(java.net.URI) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)

Example 85 with ServiceTemplateId

use of org.eclipse.winery.model.ids.definitions.ServiceTemplateId in project winery by eclipse.

the class APIResource method getAllArtifactTemplatesOfContainedDeploymentArtifacts.

@GET
@Path("getallartifacttemplatesofcontaineddeploymentartifacts")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllArtifactTemplatesOfContainedDeploymentArtifacts(@QueryParam("servicetemplate") String serviceTemplateQNameString, @QueryParam("nodetemplateid") String nodeTemplateId) {
    if (StringUtils.isEmpty(serviceTemplateQNameString)) {
        return Response.status(Status.BAD_REQUEST).entity("servicetemplate has be given as query parameter").build();
    }
    QName serviceTemplateQName = QName.valueOf(serviceTemplateQNameString);
    ServiceTemplateId serviceTemplateId = new ServiceTemplateId(serviceTemplateQName);
    final IRepository repository = RepositoryFactory.getRepository();
    if (!repository.exists(serviceTemplateId)) {
        return Response.status(Status.BAD_REQUEST).entity("service template does not exist").build();
    }
    ServiceTemplateResource serviceTemplateResource = new ServiceTemplateResource(serviceTemplateId);
    Collection<QName> artifactTemplates = new ArrayList<>();
    List<TNodeTemplate> allNestedNodeTemplates = BackendUtils.getAllNestedNodeTemplates(serviceTemplateResource.getServiceTemplate());
    for (TNodeTemplate nodeTemplate : allNestedNodeTemplates) {
        if (StringUtils.isEmpty(nodeTemplateId) || nodeTemplate.getId().equals(nodeTemplateId)) {
            Collection<QName> ats = BackendUtils.getArtifactTemplatesOfReferencedDeploymentArtifacts(nodeTemplate, repository);
            artifactTemplates.addAll(ats);
        }
    }
    // convert QName list to select2 data
    Select2DataWithOptGroups res = new Select2DataWithOptGroups();
    for (QName qName : artifactTemplates) {
        res.add(qName.getNamespaceURI(), qName.toString(), qName.getLocalPart());
    }
    return Response.ok().entity(res.asSortedSet()).build();
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) Select2DataWithOptGroups(org.eclipse.winery.repository.rest.datatypes.select2.Select2DataWithOptGroups) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)126 Test (org.junit.jupiter.api.Test)73 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)44 ArrayList (java.util.ArrayList)41 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)40 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)35 QName (javax.xml.namespace.QName)25 IOException (java.io.IOException)24 IRepository (org.eclipse.winery.repository.backend.IRepository)19 List (java.util.List)17 POST (javax.ws.rs.POST)17 Path (javax.ws.rs.Path)16 Produces (javax.ws.rs.Produces)16 HashMap (java.util.HashMap)15 TopologyWrapper (org.eclipse.winery.repository.targetallocation.util.TopologyWrapper)15 Map (java.util.Map)14 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)14 URI (java.net.URI)13 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)13 TNodeType (org.eclipse.winery.model.tosca.TNodeType)13