Search in sources :

Example 1 with NodeTypeId

use of org.eclipse.winery.model.ids.definitions.NodeTypeId 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 2 with NodeTypeId

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

the class SpringWebAppRefinementPlugin method createSpringWebAppDetector.

private TTopologyTemplate createSpringWebAppDetector(IRepository repository) {
    TNodeType springWebAppType = repository.getElement(new NodeTypeId(springWebApp));
    TNodeTemplate springApp = ModelUtilities.instantiateNodeTemplate(springWebAppType);
    return new TTopologyTemplate.Builder().addNodeTemplate(springApp).build();
}
Also used : NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Example 3 with NodeTypeId

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

the class DockerImageRefinementPlugin method getDetectorGraphs.

@Override
protected List<TTopologyTemplate> getDetectorGraphs() {
    IRepository repository = RepositoryFactory.getRepository();
    TNodeType computeType = repository.getElement(new NodeTypeId(QNAME_DOCKER_CONTAINER));
    TNodeTemplate compute = ModelUtilities.instantiateNodeTemplate(computeType);
    LinkedHashMap<String, String> computeKvProperties = new LinkedHashMap<>();
    String detectorPropertyRegex = refinementHandlerByImage.keySet().stream().collect(Collectors.joining("|", "(", ")"));
    computeKvProperties.put(PROPERTY_IMAGE_ID, detectorPropertyRegex);
    TEntityTemplate.WineryKVProperties computeProperties = new TEntityTemplate.WineryKVProperties();
    computeProperties.setKVProperties(computeKvProperties);
    compute.setProperties(computeProperties);
    return Collections.singletonList(new TTopologyTemplate.Builder().addNodeTemplate(compute).build());
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with NodeTypeId

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

the class MongoDbHandler method handleNode.

@Override
public Set<String> handleNode(TNodeTemplate dockerContainer, TTopologyTemplate topologyTemplate, String imageId) {
    Set<String> discoveredNodeIds = new HashSet<>();
    IRepository repository = RepositoryFactory.getRepository();
    QName type;
    switch(imageId) {
        case IMAGE_ID_WEAVE_USER_DB:
        case IMAGE_ID_MONGO:
            type = QNAME_MONGO_DOCKER_CONTAINER;
            break;
        default:
            type = null;
    }
    if (type != null) {
        dockerContainer.setType(type);
    }
    TNodeType mongoType = repository.getElement(new NodeTypeId(QNAME_MONGO_DB));
    TNodeTemplate mongo = ModelUtilities.instantiateNodeTemplate(mongoType);
    topologyTemplate.addNodeTemplate(mongo);
    ModelUtilities.createRelationshipTemplateAndAddToTopology(mongo, dockerContainer, ToscaBaseTypes.hostedOnRelationshipType, topologyTemplate);
    discoveredNodeIds.add(dockerContainer.getId());
    discoveredNodeIds.add(mongo.getId());
    return discoveredNodeIds;
}
Also used : QName(javax.xml.namespace.QName) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Example 5 with NodeTypeId

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

the class PetClinicRefinementPlugin method getDetectorGraphs.

@Override
protected List<TTopologyTemplate> getDetectorGraphs() {
    IRepository repository = RepositoryFactory.getRepository();
    TNodeType petClinicType = repository.getElement(new NodeTypeId(petClinic));
    TNodeTemplate petClinic = ModelUtilities.instantiateNodeTemplate(petClinicType);
    if (petClinic.getProperties() instanceof TEntityTemplate.WineryKVProperties) {
        TEntityTemplate.WineryKVProperties properties = (TEntityTemplate.WineryKVProperties) petClinic.getProperties();
        properties.getKVProperties().put("context", "*");
    }
    return Collections.singletonList(new TTopologyTemplate.Builder().addNodeTemplate(petClinic).build());
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Aggregations

NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)67 TNodeType (org.eclipse.winery.model.tosca.TNodeType)36 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)28 Test (org.junit.jupiter.api.Test)27 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)23 QName (javax.xml.namespace.QName)21 IRepository (org.eclipse.winery.repository.backend.IRepository)20 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)15 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)15 WineryVersion (org.eclipse.winery.common.version.WineryVersion)14 Response (javax.ws.rs.core.Response)13 IOException (java.io.IOException)12 LinkedHashMap (java.util.LinkedHashMap)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)11 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)11 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)10 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)10 ArrayList (java.util.ArrayList)9 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)9 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)9