Search in sources :

Example 36 with TTopologyTemplate

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

the class Visitor method visit.

public void visit(TServiceTemplate serviceTemplate) {
    Objects.requireNonNull(serviceTemplate);
    visit((TExtensibleElements) serviceTemplate);
    final TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
    if (topologyTemplate != null) {
        topologyTemplate.accept(this);
    }
    final List<TTag> tags = serviceTemplate.getTags();
    if (tags != null) {
        for (TTag tag : tags) {
            tag.accept(this);
        }
    }
    final List<TPlan> plans = serviceTemplate.getPlans();
    if (plans != null) {
        for (TPlan plan : plans) {
            plan.accept(this);
        }
    }
    final TBoundaryDefinitions boundaryDefinitions = serviceTemplate.getBoundaryDefinitions();
    if (boundaryDefinitions != null) {
        boundaryDefinitions.accept(this);
    }
}
Also used : TTag(org.eclipse.winery.model.tosca.TTag) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TPlan(org.eclipse.winery.model.tosca.TPlan) TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions)

Example 37 with TTopologyTemplate

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

the class ServiceTemplateResource method injectNodeTemplates.

@POST
@Path("injector/replace")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response injectNodeTemplates(InjectorReplaceData injectorReplaceData, @Context UriInfo uriInfo) throws Exception {
    if (injectorReplaceData.hostInjections != null) {
        Collection<TTopologyTemplate> hostInjectorTopologyTemplates = injectorReplaceData.hostInjections.values();
        hostInjectorTopologyTemplates.forEach(t -> {
            try {
                ModelUtilities.patchAnyAttributes(t.getNodeTemplates());
            } catch (IOException e) {
                LOGGER.error("XML was invalid", e);
            }
        });
    }
    if (injectorReplaceData.connectionInjections != null) {
        Collection<TTopologyTemplate> connectionInjectorTopologyTemplates = injectorReplaceData.connectionInjections.values();
        connectionInjectorTopologyTemplates.forEach(t -> {
            try {
                ModelUtilities.patchAnyAttributes(t.getNodeTemplates());
            } catch (IOException e) {
                LOGGER.error("XML was invalid", e);
            }
        });
    }
    Splitting splitting = new Splitting();
    TTopologyTemplate matchedHostsTopologyTemplate;
    TTopologyTemplate matchedConnectedTopologyTemplate;
    // Test Method findOpenRequirements
    Map<TRequirement, String> requirementsAndMatchingBasisCapabilityTypes = splitting.getOpenRequirementsAndMatchingBasisCapabilityTypeNames(this.getServiceTemplate().getTopologyTemplate());
    // Output check
    for (TRequirement req : requirementsAndMatchingBasisCapabilityTypes.keySet()) {
        System.out.println("open Requirement: " + req.getId());
        System.out.println("matchingbasisType: " + requirementsAndMatchingBasisCapabilityTypes.get(req));
    }
    if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Container")) {
        matchedHostsTopologyTemplate = splitting.injectNodeTemplates(this.getServiceTemplate().getTopologyTemplate(), injectorReplaceData.hostInjections, InjectRemoval.REMOVE_REPLACED_AND_SUCCESSORS);
        if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
            matchedConnectedTopologyTemplate = splitting.injectConnectionNodeTemplates(matchedHostsTopologyTemplate, injectorReplaceData.connectionInjections);
        } else {
            matchedConnectedTopologyTemplate = matchedHostsTopologyTemplate;
        }
    } else if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
        matchedConnectedTopologyTemplate = splitting.injectConnectionNodeTemplates(this.getServiceTemplate().getTopologyTemplate(), injectorReplaceData.connectionInjections);
    } else {
        throw new SplittingException("No open Requirements which can be matched");
    }
    TTopologyTemplate daSpecifiedTopology = matchedConnectedTopologyTemplate;
    // Start additional functionality Driver Injection
    if (!DASpecification.getNodeTemplatesWithAbstractDAs(matchedConnectedTopologyTemplate).isEmpty() && DASpecification.getNodeTemplatesWithAbstractDAs(matchedConnectedTopologyTemplate) != null) {
        daSpecifiedTopology = DriverInjection.injectDriver(matchedConnectedTopologyTemplate);
    }
    // End additional functionality Driver Injection
    this.getServiceTemplate().setTopologyTemplate(daSpecifiedTopology);
    LOGGER.debug("Persisting...");
    RestUtils.persist(this);
    LOGGER.debug("Persisted.");
    // No renaming of the Service Template allowed because of the plans
    URI url = uriInfo.getBaseUri().resolve(RestUtils.getAbsoluteURL(id));
    LOGGER.debug("URI of the old and new service template {}", url.toString());
    return Response.created(url).build();
}
Also used : Splitting(org.eclipse.winery.repository.splitting.Splitting) TRequirement(org.eclipse.winery.model.tosca.TRequirement) SplittingException(org.eclipse.winery.repository.splitting.SplittingException) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) IOException(java.io.IOException) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 38 with TTopologyTemplate

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

the class ServiceTemplateResource method generatePlaceholdersWithCapability.

@POST
@Path("placeholder/generator")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response generatePlaceholdersWithCapability() {
    Splitting splitting = new Splitting();
    TTopologyTemplate topologyTemplate = this.getServiceTemplate().getTopologyTemplate();
    if (topologyTemplate == null) {
        return Response.notModified().build();
    }
    try {
        // get all open requirements and the respective node templates with open requirements
        Map<TRequirement, TNodeTemplate> requirementsAndItsNodeTemplates = splitting.getOpenRequirementsAndItsNodeTemplate(topologyTemplate);
        IRepository repo = RepositoryFactory.getRepository();
        // iterate over all open requirements
        for (Map.Entry<TRequirement, TNodeTemplate> entry : requirementsAndItsNodeTemplates.entrySet()) {
            List<PropertyDefinitionKV> propertyDefinitionKVList = new ArrayList<>();
            LinkedHashMap<String, String> placeholderNodeTemplateProperties = new LinkedHashMap<>();
            // current node template with open requirements
            TNodeTemplate nodeTemplateWithOpenReq = entry.getValue();
            // get type of node template with open requirements
            NodeTypeId id = new NodeTypeId(nodeTemplateWithOpenReq.getType());
            TNodeType sourceNodeType = repo.getElement(id);
            List<TInterface> sourceNodeTypeInterfaces = sourceNodeType.getInterfaces();
            if (sourceNodeTypeInterfaces != null) {
                for (TInterface tInterface : sourceNodeTypeInterfaces) {
                    // TODO: make this more safe
                    for (TOperation tOperation : tInterface.getOperations()) {
                        List<TParameter> inputParameters = tOperation.getInputParameters();
                        if (inputParameters != null) {
                            for (TParameter inputParameter : inputParameters) {
                                generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
                            }
                        }
                    }
                }
            }
            List<TRelationshipTemplate> incomingRelationshipTemplates = ModelUtilities.getIncomingRelationshipTemplates(topologyTemplate, nodeTemplateWithOpenReq);
            List<TParameter> inputParameters = splitting.getInputParamListofIncomingRelationshipTemplates(topologyTemplate, incomingRelationshipTemplates);
            for (TParameter inputParameter : inputParameters) {
                String prefixTARGET = "TARGET_";
                String prefixSOURCE = "SOURCE_";
                String inputParamName = inputParameter.getName();
                if (inputParamName.contains(prefixTARGET)) {
                    inputParamName = inputParamName.replaceAll(prefixTARGET, "");
                }
                if (inputParamName.contains(prefixSOURCE)) {
                    inputParamName = inputParamName.replaceAll(prefixSOURCE, "");
                }
                inputParameter.setName(inputParamName);
                generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
            }
            // get required capability type of open requirement
            QName capabilityType = splitting.getRequiredCapabilityTypeQNameOfRequirement(entry.getKey());
            // create new placeholder node type
            TNodeType placeholderNodeType = splitting.createPlaceholderNodeType(nodeTemplateWithOpenReq.getName());
            QName placeholderQName = new QName(placeholderNodeType.getTargetNamespace(), placeholderNodeType.getName());
            WinerysPropertiesDefinition winerysPropertiesDefinition = sourceNodeType.getWinerysPropertiesDefinition();
            // add properties definition
            placeholderNodeType.setProperties(null);
            if (winerysPropertiesDefinition != null) {
                winerysPropertiesDefinition.setPropertyDefinitions(propertyDefinitionKVList);
                placeholderNodeType.setProperties(winerysPropertiesDefinition);
                String namespace = placeholderNodeType.getWinerysPropertiesDefinition().getNamespace();
                NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
                if (!namespaceManager.hasPermanentProperties(namespace)) {
                    namespaceManager.addPermanentNamespace(namespace);
                }
            }
            NodeTypeId placeholderId = new NodeTypeId(placeholderQName);
            // check if placeholder node type exists
            if (repo.exists(placeholderId)) {
                // delete and create new
                RestUtils.delete(placeholderId);
            }
            repo.setElement(placeholderId, placeholderNodeType);
            // create placeholder node template
            TNodeTemplate placeholderNodeTemplate = splitting.createPlaceholderNodeTemplate(topologyTemplate, nodeTemplateWithOpenReq.getName(), placeholderQName);
            // create capability of placeholder node template
            TCapability capa = splitting.createPlaceholderCapability(topologyTemplate, capabilityType);
            ModelUtilities.setPropertiesKV(placeholderNodeTemplate, placeholderNodeTemplateProperties);
            if (placeholderNodeTemplate.getCapabilities() == null) {
                placeholderNodeTemplate.setCapabilities(new ArrayList<>());
            }
            placeholderNodeTemplate.getCapabilities().add(capa);
            for (Map.Entry<QName, String> targetLocation : nodeTemplateWithOpenReq.getOtherAttributes().entrySet()) {
                placeholderNodeTemplate.getOtherAttributes().put(targetLocation.getKey(), targetLocation.getValue());
            }
            // add placeholder to node template and connect with source node template with open requirements
            topologyTemplate.addNodeTemplate(placeholderNodeTemplate);
            ModelUtilities.createRelationshipTemplateAndAddToTopology(nodeTemplateWithOpenReq, placeholderNodeTemplate, ToscaBaseTypes.hostedOnRelationshipType, topologyTemplate);
        }
        LOGGER.debug("PERSISTING");
        RestUtils.persist(this);
        LOGGER.debug("PERSISTED");
        String responseId = this.getServiceTemplate().getId();
        return Response.ok().entity(responseId).build();
    } catch (Exception e) {
        LOGGER.error("Could not fetch requirements and capabilities", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
}
Also used : NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TInterface(org.eclipse.winery.model.tosca.TInterface) ArrayList(java.util.ArrayList) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) LinkedHashMap(java.util.LinkedHashMap) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TOperation(org.eclipse.winery.model.tosca.TOperation) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) QName(javax.xml.namespace.QName) TCapability(org.eclipse.winery.model.tosca.TCapability) SplittingException(org.eclipse.winery.repository.splitting.SplittingException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Splitting(org.eclipse.winery.repository.splitting.Splitting) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TParameter(org.eclipse.winery.model.tosca.TParameter) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 39 with TTopologyTemplate

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

the class ServiceTemplateResource method getInjectorOptions.

@GET
@Path("injector/options")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response getInjectorOptions() {
    Splitting splitting = new Splitting();
    TTopologyTemplate topologyTemplate = this.getServiceTemplate().getTopologyTemplate();
    Map<String, List<TTopologyTemplate>> hostMatchingOptions;
    Map<String, List<TTopologyTemplate>> connectionMatchingOptions;
    InjectorReplaceOptions injectionReplaceOptions = new InjectorReplaceOptions();
    try {
        Map<TRequirement, String> requirementsAndMatchingBasisCapabilityTypes = splitting.getOpenRequirementsAndMatchingBasisCapabilityTypeNames(this.getServiceTemplate().getTopologyTemplate());
        // Output check
        for (TRequirement req : requirementsAndMatchingBasisCapabilityTypes.keySet()) {
            System.out.println("open Requirement: " + req.getId());
            System.out.println("matchingBasisType: " + requirementsAndMatchingBasisCapabilityTypes.get(req));
        }
        if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Container")) {
            hostMatchingOptions = splitting.getHostingMatchingOptionsWithDefaultLabeling(topologyTemplate);
        } else {
            hostMatchingOptions = null;
        }
        if (requirementsAndMatchingBasisCapabilityTypes.containsValue("Endpoint")) {
            connectionMatchingOptions = splitting.getConnectionInjectionOptions(topologyTemplate);
        } else {
            connectionMatchingOptions = null;
        }
        injectionReplaceOptions.setTopologyTemplate(topologyTemplate);
        injectionReplaceOptions.setHostInjectionOptions(hostMatchingOptions);
        injectionReplaceOptions.setConnectionInjectionOptions(connectionMatchingOptions);
        if (hostMatchingOptions == null && connectionMatchingOptions == null) {
            return Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity("No need for matching").build();
        }
    } catch (SplittingException e) {
        LOGGER.error("Could not match", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
    return Response.ok().entity(injectionReplaceOptions).build();
}
Also used : InjectorReplaceOptions(org.eclipse.winery.repository.rest.resources._support.dataadapter.injectionadapter.InjectorReplaceOptions) Splitting(org.eclipse.winery.repository.splitting.Splitting) TRequirement(org.eclipse.winery.model.tosca.TRequirement) SplittingException(org.eclipse.winery.repository.splitting.SplittingException) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) List(java.util.List) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 40 with TTopologyTemplate

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

the class ServiceTemplateResource method createParticipantsVersion.

@POST()
@Path("createparticipantsversion")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Response> createParticipantsVersion() throws IOException {
    IRepository repo = RepositoryFactory.getRepository();
    // create list of responses because we create multiple resources at once
    List<Response> listOfResponses = new ArrayList<>();
    LOGGER.debug("Creating new participants version of Service Template {}...", this.getId());
    ServiceTemplateId id = (ServiceTemplateId) this.getId();
    WineryVersion version = VersionUtils.getVersion(id.getXmlId().getDecoded());
    TTopologyTemplate topologyTemplate = this.getTopology();
    List<TTag> tags = new ArrayList<>();
    Splitting splitting = new Splitting();
    if (topologyTemplate.getParticipants() != null) {
        for (OTParticipant participant : topologyTemplate.getParticipants()) {
            // check if tag with partner in service template
            WineryVersion newVersion = new WineryVersion(participant.getName() + "-" + version.toString().replace("gdm", "ldm"), 1, 1);
            List<OTParticipant> newParticipantList = new ArrayList<>(topologyTemplate.getParticipants());
            // new tag to define participant of service template
            tags.add(new TTag.Builder("participant", participant.getName()).build());
            String choreoValue = splitting.calculateChoreographyTag(this.getServiceTemplate().getTopologyTemplate().getNodeTemplates(), participant.getName());
            tags.add(new TTag.Builder("choreography", choreoValue).build());
            ServiceTemplateId newId = new ServiceTemplateId(id.getNamespace().getDecoded(), VersionUtils.getNameWithoutVersion(id.getXmlId().getDecoded()) + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + newVersion.toString(), false);
            if (repo.exists(newId)) {
                repo.forceDelete(newId);
            }
            ResourceResult response = RestUtils.duplicate(id, newId);
            if (response.getStatus() == Status.CREATED) {
                response.setUri(null);
                response.setMessage(new QNameApiData(newId));
            }
            TServiceTemplate tempServiceTempl = repo.getElement(newId);
            tempServiceTempl.setTags(tags);
            tempServiceTempl.getTopologyTemplate().setParticipants(newParticipantList);
            listOfResponses.add(response.getResponse());
            // set element to propagate changed tags
            repo.setElement(newId, tempServiceTempl);
        }
    }
    return listOfResponses;
}
Also used : TTag(org.eclipse.winery.model.tosca.TTag) QNameApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameApiData) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) OTParticipant(org.eclipse.winery.model.tosca.extensions.OTParticipant) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) Splitting(org.eclipse.winery.repository.splitting.Splitting) ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) IRepository(org.eclipse.winery.repository.backend.IRepository) WineryVersion(org.eclipse.winery.common.version.WineryVersion) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)158 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)112 ArrayList (java.util.ArrayList)85 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)62 List (java.util.List)59 QName (javax.xml.namespace.QName)55 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)55 Test (org.junit.jupiter.api.Test)51 Map (java.util.Map)48 HashMap (java.util.HashMap)47 LinkedHashMap (java.util.LinkedHashMap)42 TNodeType (org.eclipse.winery.model.tosca.TNodeType)42 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)38 IRepository (org.eclipse.winery.repository.backend.IRepository)38 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)36 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)35 Objects (java.util.Objects)34 IOException (java.io.IOException)32 HashSet (java.util.HashSet)32 Collectors (java.util.stream.Collectors)32