Search in sources :

Example 1 with TRequirementDefinition

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

the class IGenericRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(NodeTypeId id) {
    Collection<DefinitionsChildId> ids = new ArrayList<>();
    Collection<NodeTypeImplementationId> allNodeTypeImplementations = this.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, id.getQName());
    for (NodeTypeImplementationId ntiId : allNodeTypeImplementations) {
        ids.add(ntiId);
    }
    final TNodeType nodeType = this.getElement(id);
    // add all referenced requirement types
    TNodeType.RequirementDefinitions reqDefsContainer = nodeType.getRequirementDefinitions();
    if (reqDefsContainer != null) {
        List<TRequirementDefinition> reqDefs = reqDefsContainer.getRequirementDefinition();
        for (TRequirementDefinition reqDef : reqDefs) {
            RequirementTypeId reqTypeId = new RequirementTypeId(reqDef.getRequirementType());
            ids.add(reqTypeId);
        }
    }
    // add all referenced capability types
    TNodeType.CapabilityDefinitions capDefsContainer = nodeType.getCapabilityDefinitions();
    if (capDefsContainer != null) {
        List<TCapabilityDefinition> capDefs = capDefsContainer.getCapabilityDefinition();
        for (TCapabilityDefinition capDef : capDefs) {
            CapabilityTypeId capTypeId = new CapabilityTypeId(capDef.getCapabilityType());
            ids.add(capTypeId);
        }
    }
    return ids;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) ArrayList(java.util.ArrayList) TNodeType(org.eclipse.winery.model.tosca.TNodeType) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TCapabilityDefinition(org.eclipse.winery.model.tosca.TCapabilityDefinition)

Example 2 with TRequirementDefinition

use of org.eclipse.winery.model.tosca.TRequirementDefinition 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 3 with TRequirementDefinition

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

the class IRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(NodeTypeId id) {
    Collection<NodeTypeImplementationId> allNodeTypeImplementations = this.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, id.getQName());
    Collection<DefinitionsChildId> ids = new HashSet<>(allNodeTypeImplementations);
    final TNodeType nodeType = this.getElement(id);
    // Add all referenced requirement types, but only in XML mode.
    // For YAML mode add referenced RelationshipType and CapabilityType, if present
    List<TRequirementDefinition> reqDefs = nodeType.getRequirementDefinitions();
    if (reqDefs != null) {
        for (TRequirementDefinition reqDef : reqDefs) {
            // if either of these is set, we're dealing with a type defined in YAML
            if (Objects.nonNull(reqDef.getRelationship()) || Objects.nonNull(reqDef.getCapability()) || Objects.nonNull(reqDef.getNode())) {
                if (Objects.nonNull(reqDef.getRelationship())) {
                    ids.add(new RelationshipTypeId(reqDef.getRelationship()));
                }
                if (Objects.nonNull(reqDef.getCapability())) {
                    ids.add(new CapabilityTypeId(reqDef.getCapability()));
                }
                if (Objects.nonNull(reqDef.getNode())) {
                    ids.add(new NodeTypeId(reqDef.getNode()));
                }
            } else {
                RequirementTypeId reqTypeId = new RequirementTypeId(reqDef.getRequirementType());
                ids.add(reqTypeId);
            }
        }
    }
    // add all referenced capability types
    List<TCapabilityDefinition> capabilityDefinitions = nodeType.getCapabilityDefinitions();
    if (capabilityDefinitions != null) {
        for (TCapabilityDefinition capDef : capabilityDefinitions) {
            CapabilityTypeId capTypeId = new CapabilityTypeId(capDef.getCapabilityType());
            ids.add(capTypeId);
            // Add all types referenced in valid source types
            if (Objects.nonNull(capDef.getValidSourceTypes())) {
                capDef.getValidSourceTypes().forEach(sourceType -> ids.add(new NodeTypeId(sourceType)));
            }
        }
    }
    List<TInterfaceDefinition> interfaceDefinitions = nodeType.getInterfaceDefinitions();
    if (Objects.nonNull(interfaceDefinitions) && !interfaceDefinitions.isEmpty()) {
        for (TInterfaceDefinition intDef : interfaceDefinitions) {
            InterfaceTypeId interfaceTypeId = new InterfaceTypeId(intDef.getType());
            ids.add(interfaceTypeId);
        }
    }
    // Store all referenced artifact types
    List<TArtifact> artifacts = nodeType.getArtifacts();
    if (Objects.nonNull(artifacts)) {
        artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
    }
    getReferencedDefinitionsOfProperties(ids, nodeType.getProperties());
    return ids;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) InterfaceTypeId(org.eclipse.winery.model.ids.definitions.InterfaceTypeId) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) TArtifact(org.eclipse.winery.model.tosca.TArtifact) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TCapabilityDefinition(org.eclipse.winery.model.tosca.TCapabilityDefinition) TInterfaceDefinition(org.eclipse.winery.model.tosca.TInterfaceDefinition) HashSet(java.util.HashSet)

Example 4 with TRequirementDefinition

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

the class RequirementDefinitionsResource method performPost.

@Override
public Response performPost(RequirementOrCapabilityDefinitionPostData postData) {
    // if we are in XML mode, we delegate to the parent
    if (!RepositoryUtils.isYamlRepository(requestRepository)) {
        return super.performPost(postData);
    }
    // otherwise, we do it the YAML way!!
    if (StringUtils.isEmpty(postData.name)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Name has to be provided").build();
    }
    if (StringUtils.isEmpty(postData.capability)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Capability Type has to be provided").build();
    }
    TRequirementDefinition def = super.createBasicReqOrCapDef(postData);
    def.setCapability(QName.valueOf(postData.capability));
    if (!StringUtils.isEmpty(postData.node)) {
        def.setNode(QName.valueOf(postData.node));
    }
    if (!StringUtils.isEmpty(postData.relationship)) {
        def.setRelationship(QName.valueOf(postData.relationship));
    }
    return super.persistDef(def, postData);
}
Also used : TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition)

Example 5 with TRequirementDefinition

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

the class RequirementOrCapabilityDefinitionsResource method onPost.

@POST
// As there is no supertype of TCapabilityType and TRequirementType containing the common attributes, we have to rely on unchecked casts
@SuppressWarnings("unchecked")
@Consumes(MediaType.APPLICATION_JSON)
public Response onPost(CapabilityDefinitionPostData postData) {
    if (StringUtils.isEmpty(postData.name)) {
        return Response.status(Status.BAD_REQUEST).entity("Name has to be provided").build();
    }
    if (StringUtils.isEmpty(postData.type)) {
        return Response.status(Status.BAD_REQUEST).entity("Type has to be provided").build();
    }
    int lbound = 1;
    if (!StringUtils.isEmpty(postData.lowerBound)) {
        try {
            lbound = Integer.parseInt(postData.lowerBound);
        } catch (NumberFormatException e) {
            return Response.status(Status.BAD_REQUEST).entity("Bad format of lowerbound: " + e.getMessage()).build();
        }
    }
    String ubound = "1";
    if (!StringUtils.isEmpty(postData.upperBound)) {
        ubound = postData.upperBound;
    }
    // we also support replacement of existing requirements
    // therefore, we loop through the existing requirements
    int idx = -1;
    boolean found = false;
    for (ReqDefOrCapDef d : this.list) {
        idx++;
        if (this.getId(d).equals(postData.name)) {
            found = true;
            break;
        }
    }
    QName typeQName = QName.valueOf(postData.type);
    // Create object and put type in it
    ReqDefOrCapDef def;
    if (this instanceof CapabilityDefinitionsResource) {
        def = (ReqDefOrCapDef) new TCapabilityDefinition();
        ((TCapabilityDefinition) def).setCapabilityType(typeQName);
    } else {
        assert (this instanceof RequirementDefinitionsResource);
        def = (ReqDefOrCapDef) new TRequirementDefinition();
        ((TRequirementDefinition) def).setRequirementType(typeQName);
    }
    // copy all other data into object
    AbstractReqOrCapDefResource.invokeSetter(def, "setName", postData.name);
    AbstractReqOrCapDefResource.invokeSetter(def, "setLowerBound", lbound);
    AbstractReqOrCapDefResource.invokeSetter(def, "setUpperBound", ubound);
    if (found) {
        // replace element
        this.list.set(idx, def);
    } else {
        // add new element
        this.list.add(def);
    }
    return RestUtils.persist(this.res);
}
Also used : QName(javax.xml.namespace.QName) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TCapabilityDefinition(org.eclipse.winery.model.tosca.TCapabilityDefinition) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

TRequirementDefinition (org.eclipse.winery.model.tosca.TRequirementDefinition)6 TNodeType (org.eclipse.winery.model.tosca.TNodeType)4 QName (javax.xml.namespace.QName)3 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)3 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)3 TCapabilityDefinition (org.eclipse.winery.model.tosca.TCapabilityDefinition)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 NodeTypeImplementationId (org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId)2 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)2 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)2 TRelationshipType (org.eclipse.winery.model.tosca.TRelationshipType)2