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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations