Search in sources :

Example 1 with TInterface

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

the class InterfacesResource method onPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onPost(List<TInterface> interfaceApiData) {
    if (!interfaceApiData.isEmpty()) {
        for (TInterface tInt : interfaceApiData) {
            if (!tInt.getOperation().isEmpty()) {
                for (TOperation tOp : tInt.getOperation()) {
                    if (tOp.getInputParameters() == null || tOp.getInputParameters().getInputParameter().isEmpty()) {
                        tOp.setInputParameters(null);
                    }
                    if (tOp.getOutputParameters() == null || tOp.getOutputParameters().getOutputParameter().isEmpty()) {
                        tOp.setOutputParameters(null);
                    }
                }
            } else {
                return Response.status(Response.Status.BAD_REQUEST).entity("No operation provided!").build();
            }
        }
    } else {
        return Response.status(Response.Status.BAD_REQUEST).entity("No interface provided!").build();
    }
    if (this.res instanceof RelationshipTypeResource) {
        TRelationshipType relationshipType = (TRelationshipType) this.res.getElement();
        switch(this.interfaceType) {
            case "source":
                TRelationshipType.SourceInterfaces sourceInterfaces = new TRelationshipType.SourceInterfaces();
                sourceInterfaces.getInterface().clear();
                sourceInterfaces.getInterface().addAll(interfaceApiData);
                relationshipType.setSourceInterfaces(sourceInterfaces);
                break;
            default:
                // it will be target
                TRelationshipType.TargetInterfaces targetInterfaces = new TRelationshipType.TargetInterfaces();
                targetInterfaces.getInterface().clear();
                targetInterfaces.getInterface().addAll(interfaceApiData);
                relationshipType.setTargetInterfaces(targetInterfaces);
                break;
        }
    } else if (this.res instanceof NodeTypeResource) {
        TNodeType nodeType = (TNodeType) this.res.getElement();
        TNodeType.Interfaces interfaces = new TNodeType.Interfaces();
        interfaces.getInterface().clear();
        interfaces.getInterface().addAll(interfaceApiData);
        nodeType.setInterfaces(interfaces);
    } else {
        throw new IllegalStateException("Interfaces are not supported for this element type!");
    }
    return RestUtils.persist(this.res);
}
Also used : TInterface(org.eclipse.winery.model.tosca.TInterface) RelationshipTypeResource(org.eclipse.winery.repository.rest.resources.entitytypes.relationshiptypes.RelationshipTypeResource) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TOperation(org.eclipse.winery.model.tosca.TOperation) NodeTypeResource(org.eclipse.winery.repository.rest.resources.entitytypes.nodetypes.NodeTypeResource)

Example 2 with TInterface

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

the class GeneratorTest method testMultipleOperationsInOrOut.

@Test
public void testMultipleOperationsInOrOut() throws Exception {
    TInterface tinterface = new TInterface();
    tinterface.setName("TestInOrOut");
    TOperation opIn = new TOperation();
    opIn.setName("OpIn");
    tinterface.getOperation().add(opIn);
    TOperation.InputParameters op1InputParameters = new TOperation.InputParameters();
    TParameter op1ip1 = new TParameter();
    op1ip1.setName("op1ip1");
    op1ip1.setType("xs:string");
    op1InputParameters.getInputParameter().add(op1ip1);
    TParameter op1ip2 = new TParameter();
    op1ip2.setName("op1ip2");
    op1ip2.setType("xs:string");
    op1InputParameters.getInputParameter().add(op1ip2);
    opIn.setInputParameters(op1InputParameters);
    TOperation opOut = new TOperation();
    opOut.setName("OpOut");
    tinterface.getOperation().add(opOut);
    TOperation.OutputParameters op1OutputParameters = new TOperation.OutputParameters();
    TParameter op1op1 = new TParameter();
    op1op1.setName("op1op1");
    op1op1.setType("xs:string");
    op1OutputParameters.getOutputParameter().add(op1op1);
    TParameter op1op2 = new TParameter();
    op1op2.setName("op1op2");
    op1op1.setType("xs:string");
    op1OutputParameters.getOutputParameter().add(op1op2);
    opOut.setOutputParameters(op1OutputParameters);
    TNodeType nodeType = new TNodeType();
    nodeType.setName("test");
    nodeType.setTargetNamespace("http://asd.com");
    Generator gen = new Generator(tinterface, "org.opentosca.ia", new URL("http://asd.com"), "testname", WORKING_DIR.toFile());
    Path generateProject = gen.generateProject();
    System.out.println(generateProject);
}
Also used : Path(java.nio.file.Path) TInterface(org.eclipse.winery.model.tosca.TInterface) TOperation(org.eclipse.winery.model.tosca.TOperation) TParameter(org.eclipse.winery.model.tosca.TParameter) URL(java.net.URL) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Test(org.junit.Test)

Example 3 with TInterface

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

the class GeneratorTest method testNoParams.

@Test
public void testNoParams() throws Exception {
    TInterface tinterface = new TInterface();
    tinterface.setName("TestNoParams");
    TOperation opIn = new TOperation();
    opIn.setName("OpNoParams");
    tinterface.getOperation().add(opIn);
    TNodeType nodeType = new TNodeType();
    nodeType.setName("test");
    nodeType.setTargetNamespace("http://asd.com");
    Generator gen = new Generator(tinterface, "org.opentosca.ia", new URL("http://asd.com"), "testname", WORKING_DIR.toFile());
    Path generateProject = gen.generateProject();
    System.out.println(generateProject);
}
Also used : Path(java.nio.file.Path) TInterface(org.eclipse.winery.model.tosca.TInterface) TOperation(org.eclipse.winery.model.tosca.TOperation) URL(java.net.URL) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Test(org.junit.Test)

Example 4 with TInterface

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

the class EnhancementUtils method createFeatureNodeType.

/**
 * This method merges the Basic-NodeType of the given nodeTemplate with the selected Feature-NodeTypes and generates
 * respective implementations.
 *
 * @param nodeTemplate The NodeTemplate that is updated with the selected features.
 * @param featureTypes The list of selected features as generated by {@link #getAvailableFeaturesForTopology(TTopologyTemplate,
 *                     List}.
 * @return The mapping of the generated merged NodeType and the QName of the NodeType it replaces.
 */
public static TNodeType createFeatureNodeType(TNodeTemplate nodeTemplate, Map<QName, String> featureTypes) {
    IRepository repository = RepositoryFactory.getRepository();
    Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
    Map<QName, TNodeTypeImplementation> nodeTypeImplementations = repository.getQNameToElementMapping(NodeTypeImplementationId.class);
    StringBuilder featureNames = new StringBuilder();
    featureTypes.values().forEach(featureName -> {
        if (!featureNames.toString().isEmpty()) {
            featureNames.append("-");
        }
        featureNames.append(featureName.replaceAll("\\s", "_"));
    });
    // merge type
    String namespace = generateNewGeneratedNamespace(nodeTemplate.getType());
    TNodeType featureEnrichedNodeType = nodeTypes.get(nodeTemplate.getType());
    featureEnrichedNodeType.setTargetNamespace(namespace);
    featureEnrichedNodeType.setName(nodeTemplate.getType().getLocalPart() + "-" + nodeTemplate.getId() + "-" + featureNames + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1");
    // prepare Properties
    if (Objects.isNull(featureEnrichedNodeType.getWinerysPropertiesDefinition())) {
        WinerysPropertiesDefinition props = new WinerysPropertiesDefinition();
        props.setPropertyDefinitions(new ArrayList<>());
        ModelUtilities.replaceWinerysPropertiesDefinition(featureEnrichedNodeType, props);
    }
    List<PropertyDefinitionKV> baseProperties = featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions();
    // prepare Interfaces
    if (Objects.isNull(featureEnrichedNodeType.getInterfaces())) {
        featureEnrichedNodeType.setInterfaces(new ArrayList<>());
    }
    List<TInterface> baseInterfaces = featureEnrichedNodeType.getInterfaces();
    // merge impl accordingly
    TNodeTypeImplementation generatedImplementation = new TNodeTypeImplementation.Builder(featureEnrichedNodeType.getName() + "_Impl" + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1", featureEnrichedNodeType.getQName()).build();
    // ensure that the lists are initialized
    generatedImplementation.setImplementationArtifacts(new ArrayList<>());
    generatedImplementation.setDeploymentArtifacts(new ArrayList<>());
    Collection<NodeTypeImplementationId> baseTypeImplementations = repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, nodeTemplate.getType());
    if (baseTypeImplementations.size() > 0) {
        for (NodeTypeImplementationId id : baseTypeImplementations) {
            if (Objects.isNull(generatedImplementation.getTargetNamespace())) {
                generatedImplementation.setTargetNamespace(generateNewGeneratedNamespace(id.getQName()));
            }
            addAllDAsAndIAsToImplementation(generatedImplementation, nodeTypeImplementations.get(id.getQName()));
        }
    } else {
        // This should never be the case. However, we implement it as a valid fallback.
        generatedImplementation.setTargetNamespace(namespace.replace("nodetypes", "nodetypeimplementations"));
    }
    featureTypes.keySet().forEach(featureTypeQName -> {
        TNodeType nodeType = nodeTypes.get(featureTypeQName);
        // merge Properties
        if (Objects.nonNull(nodeType.getWinerysPropertiesDefinition())) {
            List<PropertyDefinitionKV> kvList = nodeType.getWinerysPropertiesDefinition().getPropertyDefinitions();
            if (Objects.nonNull(kvList) && !kvList.isEmpty()) {
                for (PropertyDefinitionKV kv : kvList) {
                    boolean listContainsProperty = baseProperties.stream().anyMatch(property -> property.getKey().equals(kv.getKey()));
                    if (!listContainsProperty) {
                        baseProperties.add(kv);
                    }
                }
            }
        }
        // merge Interfaces
        if (Objects.nonNull(nodeType.getInterfaces()) && !nodeType.getInterfaces().isEmpty()) {
            baseInterfaces.addAll(nodeType.getInterfaces());
        }
        // merge implementations
        repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, featureTypeQName).forEach(id -> addAllDAsAndIAsToImplementation(generatedImplementation, nodeTypeImplementations.get(id.getQName())));
    });
    // remove them from the type to ensure a compliant XML.
    if (Objects.nonNull(featureEnrichedNodeType.getWinerysPropertiesDefinition()) && Objects.nonNull(featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions()) && featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions().isEmpty()) {
        ModelUtilities.removeWinerysPropertiesDefinition(featureEnrichedNodeType);
    }
    try {
        repository.setElement(new NodeTypeId(featureEnrichedNodeType.getQName()), featureEnrichedNodeType);
        repository.setElement(new NodeTypeImplementationId(generatedImplementation.getQName()), generatedImplementation);
    } catch (IOException e) {
        logger.error("Error while saving generated definitions.", e);
    }
    return featureEnrichedNodeType;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TInterface(org.eclipse.winery.model.tosca.TInterface) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) IOException(java.io.IOException) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) TNodeType(org.eclipse.winery.model.tosca.TNodeType) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository)

Example 5 with TInterface

use of org.eclipse.winery.model.tosca.TInterface 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)

Aggregations

TInterface (org.eclipse.winery.model.tosca.TInterface)15 TNodeType (org.eclipse.winery.model.tosca.TNodeType)10 TOperation (org.eclipse.winery.model.tosca.TOperation)10 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)5 TParameter (org.eclipse.winery.model.tosca.TParameter)5 IRepository (org.eclipse.winery.repository.backend.IRepository)5 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 TRelationshipType (org.eclipse.winery.model.tosca.TRelationshipType)4 LinkedHashMap (java.util.LinkedHashMap)3 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)3 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)3 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)3 PropertyDefinitionKV (org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV)3 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition)3 Test (org.junit.Test)3 Map (java.util.Map)2 Produces (javax.ws.rs.Produces)2