Search in sources :

Example 1 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class Showcase method zipTypeTest.

@Test
public void zipTypeTest() throws Exception {
    String name = "Showcase";
    String path = this.path + File.separator + name + ".csar";
    // Read DriverInjectionTest and import the csar into the repository
    InputStream fis = new FileInputStream(path);
    IRepository repository = RepositoryFactory.getRepository(Utils.getTmpDir(Paths.get("repository")));
    CsarImporter csarImporter = new CsarImporter();
    csarImporter.readCSAR(fis, true, true);
    // Read the csar again and convert it to yaml the resulting yaml service templates
    // are written to a temporary dir and converted to a input stream of a zip file
    Converter converter = new Converter(repository);
    fis = new FileInputStream(path);
    InputStream zip = converter.convertX2Y(fis);
    // Write the zip file to the output path and rename it
    File zipFile = new File(outPath + File.separator + name + ".csar");
    if (!zipFile.getParentFile().exists()) {
        zipFile.getParentFile().mkdirs();
    }
    Files.copy(zip, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    // clean temporary repository
    Utils.deleteTmpDir(Paths.get("repository"));
    Assert.assertTrue(zipFile.exists());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) Converter(org.eclipse.winery.yaml.converter.Converter) IRepository(org.eclipse.winery.repository.backend.IRepository) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class ModelUtilitiesTest method instantiateNodeTemplate.

// endregion
@Test
public void instantiateNodeTemplate() throws Exception {
    this.setRevisionTo("origin/plain");
    IRepository repository = RepositoryFactory.getRepository();
    TNodeType nodeType = repository.getElement(new NodeTypeId(QName.valueOf("{http://opentosca.org/add/management/to/instances/nodetypes}Ubuntu_16.04-w1")));
    TNodeTemplate nodeTemplate = ModelUtilities.instantiateNodeTemplate(nodeType);
    assertNotNull(nodeTemplate);
    assertNotNull(nodeTemplate.getProperties());
    assertEquals(8, ((TEntityTemplate.WineryKVProperties) nodeTemplate.getProperties()).getKVProperties().size());
}
Also used : 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) Test(org.junit.jupiter.api.Test)

Example 3 with IRepository

use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.

the class EnhancementUtils method getAvailableFeaturesForTopology.

// region ******************** Add Management Features ********************
/**
 * Gathers all feature NodeTypes available for the given topology.
 *
 * If the underlying implementation of the feature does not matter, use <code>null</code>.
 *
 * <p>
 * Note: If feature NodeTypes are used in the topology, they cannot be enhanced with more features.
 * </p>
 *
 * @param topology               The topology to update.
 * @param deploymentTechnologies Deployment technology descriptors contained in the service template
 */
public static Map<String, Map<QName, String>> getAvailableFeaturesForTopology(TTopologyTemplate topology, List<DeploymentTechnologyDescriptor> deploymentTechnologies) {
    IRepository repository = RepositoryFactory.getRepository();
    Map<String, Map<QName, String>> availableFeatures = new HashMap<>();
    Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
    topology.getNodeTemplates().forEach(node -> {
        List<String> nodeDeploymentTechnologies = deploymentTechnologies.stream().filter(deploymentTechnologyDescriptor -> deploymentTechnologyDescriptor.getManagedIds().contains(node.getId())).map(DeploymentTechnologyDescriptor::getTechnologyId).collect(Collectors.toList());
        Map<TNodeType, String> featureChildren = ModelUtilities.getAvailableFeaturesOfType(node.getType(), nodeTypes, nodeDeploymentTechnologies);
        Map<QName, String> applicableFeatures = new HashMap<>();
        // Check requirements
        featureChildren.forEach((featureType, value) -> {
            if (listIsNotNullOrEmpty(featureType.getRequirementDefinitions())) {
                List<TRequirementDefinition> requirements = featureType.getRequirementDefinitions().stream().filter(req -> req.getRequirementType().equals(OpenToscaBaseTypes.managementFeatureRequirement)).collect(Collectors.toList());
                requirements.forEach(req -> {
                    boolean applicable = ModelUtilities.getHostedOnSuccessors(topology, node).stream().anyMatch(hosts -> {
                        WineryVersion reqVersion = VersionUtils.getVersion(req.getName());
                        String reqName = VersionUtils.getNameWithoutVersion(req.getName());
                        String type = hosts.getType().getLocalPart();
                        if (VersionUtils.getNameWithoutVersion(type).equals(reqName)) {
                            return reqVersion.getComponentVersion().isEmpty() || reqVersion.getComponentVersion().equals(VersionUtils.getVersion(type).getComponentVersion());
                        }
                        return false;
                    });
                    if (applicable) {
                        applicableFeatures.put(featureType.getQName(), value);
                    }
                });
            } else {
                applicableFeatures.put(featureType.getQName(), value);
            }
        });
        if (featureChildren.size() > 0) {
            availableFeatures.put(node.getId(), applicableFeatures);
        }
    });
    return availableFeatures;
}
Also used : TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) OpenToscaInterfaces(org.eclipse.winery.model.tosca.constants.OpenToscaInterfaces) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) OpenToscaBaseTypes(org.eclipse.winery.model.tosca.constants.OpenToscaBaseTypes) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) NamespaceProperties(org.eclipse.winery.repository.backend.filebased.NamespaceProperties) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WineryVersion(org.eclipse.winery.common.version.WineryVersion) Map(java.util.Map) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) Logger(org.slf4j.Logger) ListUtils.listIsNotNullOrEmpty(org.eclipse.winery.common.ListUtils.listIsNotNullOrEmpty) Collection(java.util.Collection) IOException(java.io.IOException) DeploymentTechnologyDescriptor(org.eclipse.winery.model.tosca.DeploymentTechnologyDescriptor) TNodeType(org.eclipse.winery.model.tosca.TNodeType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) 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) Objects(java.util.Objects) List(java.util.List) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) VersionUtils(org.eclipse.winery.common.version.VersionUtils) IRepository(org.eclipse.winery.repository.backend.IRepository) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) QName(javax.xml.namespace.QName) ToscaBaseTypes(org.eclipse.winery.model.tosca.constants.ToscaBaseTypes) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QName(javax.xml.namespace.QName) TNodeType(org.eclipse.winery.model.tosca.TNodeType) IRepository(org.eclipse.winery.repository.backend.IRepository) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Example 4 with IRepository

use of org.eclipse.winery.repository.backend.IRepository 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 IRepository

use of org.eclipse.winery.repository.backend.IRepository 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)

Aggregations

IRepository (org.eclipse.winery.repository.backend.IRepository)75 TNodeType (org.eclipse.winery.model.tosca.TNodeType)29 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)28 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)27 IOException (java.io.IOException)24 QName (javax.xml.namespace.QName)24 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)22 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)21 ArrayList (java.util.ArrayList)20 Map (java.util.Map)20 HashMap (java.util.HashMap)15 LoggerFactory (org.slf4j.LoggerFactory)15 LinkedHashMap (java.util.LinkedHashMap)14 List (java.util.List)14 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)14 RepositoryFactory (org.eclipse.winery.repository.backend.RepositoryFactory)14 Collectors (java.util.stream.Collectors)13 POST (javax.ws.rs.POST)13 Path (javax.ws.rs.Path)13 Produces (javax.ws.rs.Produces)13