use of org.eclipse.winery.model.ids.definitions.RelationshipTypeId in project winery by eclipse.
the class Splitting method getInputParamListofIncomingRelationshipTemplates.
public List<TParameter> getInputParamListofIncomingRelationshipTemplates(TTopologyTemplate topologyTemplate, List<TRelationshipTemplate> listOfIncomingRelationshipTemplates) {
List<TParameter> listOfInputs = new ArrayList<>();
IRepository repo = RepositoryFactory.getRepository();
for (TRelationshipTemplate incomingRelationshipTemplate : listOfIncomingRelationshipTemplates) {
TNodeTemplate incomingNodetemplate = ModelUtilities.getSourceNodeTemplateOfRelationshipTemplate(topologyTemplate, incomingRelationshipTemplate);
NodeTypeId incomingNodeTypeId = new NodeTypeId(incomingNodetemplate.getType());
TNodeType incomingNodeType = repo.getElement(incomingNodeTypeId);
List<TInterface> incomingNodeTypeInterfaces = incomingNodeType.getInterfaces();
RelationshipTypeId incomingRelationshipTypeId = new RelationshipTypeId(incomingRelationshipTemplate.getType());
if (!incomingNodeTypeInterfaces.isEmpty()) {
TInterface relevantInterface = null;
List<TInterface> connectionInterfaces = incomingNodeTypeInterfaces.stream().filter(tInterface -> tInterface.getIdFromIdOrNameField().contains("connection")).collect(Collectors.toList());
if (connectionInterfaces.size() > 1) {
TNodeTemplate targetNodeTemplate = ModelUtilities.getTargetNodeTemplateOfRelationshipTemplate(topologyTemplate, incomingRelationshipTemplate);
for (TInterface tInterface : connectionInterfaces) {
int separator = tInterface.getIdFromIdOrNameField().lastIndexOf("/");
String prefixRelation = tInterface.getIdFromIdOrNameField().substring(separator + 1);
if (targetNodeTemplate.getName() != null && targetNodeTemplate.getName().toLowerCase().contains(prefixRelation.toLowerCase())) {
relevantInterface = tInterface;
}
}
} else {
relevantInterface = connectionInterfaces.get(0);
}
if (relevantInterface != null) {
for (TOperation tOperation : relevantInterface.getOperations()) {
List<TParameter> inputParameters = tOperation.getInputParameters();
if (inputParameters != null) {
listOfInputs.addAll(inputParameters);
}
}
}
}
}
return listOfInputs;
}
use of org.eclipse.winery.model.ids.definitions.RelationshipTypeId in project winery by eclipse.
the class Splitting method getMatchingRelationshipType.
/**
*/
private TRelationshipType getMatchingRelationshipType(TRequirement requirement, TCapability capability) {
TRelationshipType matchingRelationshipType = null;
SortedSet<RelationshipTypeId> relTypeIds = RepositoryFactory.getRepository().getAllDefinitionsChildIds(RelationshipTypeId.class);
List<TRelationshipType> relationshipTypes = new ArrayList<>();
for (RelationshipTypeId id : relTypeIds) {
relationshipTypes.add(RepositoryFactory.getRepository().getElement(id));
}
Map<String, String> requirementProperties = ModelUtilities.getPropertiesKV(requirement);
Map<String, String> capabilityProperties = ModelUtilities.getPropertiesKV(capability);
/* If the property "requiredRelationshipType" is defined for the requirement and the capability this relationship type
has to be taken - if the specified relationship type is not available, no relationship type is chosen */
if (requirementProperties != null && capabilityProperties != null && requirementProperties.containsKey("requiredRelationshipType") && capabilityProperties.containsKey("requiredRelationshipType") && requirementProperties.get("requiredRelationshipType").equals(capabilityProperties.get("requiredRelationshipType")) && requirementProperties.get("requiredRelationshipType") != null) {
// Assumption: We work on basic KV properties here
QName referencedRelationshipType = QName.valueOf((String) requirementProperties.get("requiredRelationshipType"));
RelationshipTypeId relTypeId = new RelationshipTypeId(referencedRelationshipType);
if (relTypeIds.stream().anyMatch(rti -> rti.equals(relTypeId))) {
return RepositoryFactory.getRepository().getElement(relTypeId);
}
} else {
QName requirementTypeQName = requirement.getType();
RequirementTypeId reqTypeId = new RequirementTypeId(requirement.getType());
TRequirementType requirementType = RepositoryFactory.getRepository().getElement(reqTypeId);
QName capabilityTypeQName = capability.getType();
CapabilityTypeId capTypeId = new CapabilityTypeId(capability.getType());
TCapabilityType capabilityType = RepositoryFactory.getRepository().getElement(capTypeId);
List<TRelationshipType> availableMatchingRelationshipTypes = new ArrayList<>();
availableMatchingRelationshipTypes.clear();
while (requirementType != null && capabilityType != null) {
// relationship type with valid source origin requirement type or empty and valid target origin capability type
for (TRelationshipType rt : relationshipTypes) {
if ((rt.getValidSource() == null || rt.getValidSource().getTypeRef().equals(requirementTypeQName)) && (rt.getValidTarget() != null && rt.getValidTarget().getTypeRef().equals(capabilityTypeQName))) {
availableMatchingRelationshipTypes.add(rt);
}
}
if (!availableMatchingRelationshipTypes.isEmpty() && availableMatchingRelationshipTypes.size() == 1) {
return availableMatchingRelationshipTypes.get(0);
} else if (!availableMatchingRelationshipTypes.isEmpty() && availableMatchingRelationshipTypes.size() > 1) {
return null;
} else if (requirementType.getDerivedFrom() != null || capabilityType.getDerivedFrom() != null) {
TCapabilityType derivedFromCapabilityType = null;
TRequirementType derivedFromRequirementType = null;
availableMatchingRelationshipTypes.clear();
List<TRelationshipType> additionalMatchingRelationshipTypes = new ArrayList<>();
if (capabilityType.getDerivedFrom() != null) {
QName derivedFromCapabilityTypeRef = capabilityType.getDerivedFrom().getTypeRef();
CapabilityTypeId derivedFromCapTypeId = new CapabilityTypeId(derivedFromCapabilityTypeRef);
derivedFromCapabilityType = RepositoryFactory.getRepository().getElement(derivedFromCapTypeId);
for (TRelationshipType rt : relationshipTypes) {
if ((rt.getValidSource() == null || rt.getValidSource().getTypeRef().equals(requirementTypeQName)) && (rt.getValidTarget() != null && rt.getValidTarget().getTypeRef().equals(derivedFromCapabilityTypeRef))) {
availableMatchingRelationshipTypes.add(rt);
}
}
}
if (requirementType.getDerivedFrom() != null) {
QName derivedFromRequirementTypeRef = requirementType.getDerivedFrom().getTypeRef();
RequirementTypeId derivedFromReqTypeId = new RequirementTypeId(derivedFromRequirementTypeRef);
derivedFromRequirementType = RepositoryFactory.getRepository().getElement(derivedFromReqTypeId);
for (TRelationshipType rt : relationshipTypes) {
if ((rt.getValidSource() != null && rt.getValidSource().getTypeRef().equals(derivedFromRequirementTypeRef)) && (rt.getValidTarget() != null && rt.getValidTarget().getTypeRef().equals(capabilityTypeQName))) {
additionalMatchingRelationshipTypes.add(rt);
}
}
}
availableMatchingRelationshipTypes.addAll(additionalMatchingRelationshipTypes);
if (!availableMatchingRelationshipTypes.isEmpty() && availableMatchingRelationshipTypes.size() == 1) {
return availableMatchingRelationshipTypes.get(0);
} else if (!availableMatchingRelationshipTypes.isEmpty() && availableMatchingRelationshipTypes.size() > 1) {
return null;
}
requirementType = derivedFromRequirementType;
capabilityType = derivedFromCapabilityType;
}
}
TCapabilityType basisCapabilityType = getBasisCapabilityType(capability.getType());
for (TRelationshipType relationshipType : relationshipTypes) {
if (basisCapabilityType != null && basisCapabilityType.getName().equalsIgnoreCase("container") && relationshipType.getName().equalsIgnoreCase("hostedon") && relationshipType.getValidSource() == null && (relationshipType.getValidTarget() == null || relationshipType.getValidTarget().getTypeRef().getLocalPart().equalsIgnoreCase("container"))) {
return relationshipType;
}
if (basisCapabilityType != null && basisCapabilityType.getName().equalsIgnoreCase("endpoint") && relationshipType.getName().equalsIgnoreCase("connectsto") && relationshipType.getValidSource() == null && (relationshipType.getValidTarget() == null || relationshipType.getValidTarget().getTypeRef().getLocalPart().equalsIgnoreCase("endpoint"))) {
return relationshipType;
}
}
}
return matchingRelationshipType;
}
use of org.eclipse.winery.model.ids.definitions.RelationshipTypeId in project winery by eclipse.
the class GenericArtifactsResource method findInterface.
private Optional<TInterface> findInterface(EntityTypeId id, String interfaceName) {
TInterface i;
List<TInterface> interfaces = new ArrayList<>();
IRepository repository = RepositoryFactory.getRepository();
if (this.res instanceof NodeTypeImplementationResource || this.res instanceof NodeTypeImplementationsResource) {
TNodeType nodeType = repository.getElement((NodeTypeId) id);
if (nodeType.getInterfaces() != null) {
interfaces.addAll(nodeType.getInterfaces());
}
} else if (this.res instanceof RelationshipTypeImplementationResource || this.res instanceof RelationshipTypeImplementationsResource) {
TRelationshipType relationshipType = repository.getElement((RelationshipTypeId) id);
if (relationshipType.getSourceInterfaces() != null) {
interfaces.addAll(relationshipType.getSourceInterfaces());
}
if (relationshipType.getTargetInterfaces() != null) {
interfaces.addAll(relationshipType.getTargetInterfaces());
}
if (relationshipType.getInterfaces() != null) {
interfaces.addAll(relationshipType.getInterfaces());
}
}
Iterator<TInterface> it = interfaces.iterator();
do {
i = it.next();
if (i.getName().equals(interfaceName)) {
return Optional.of(i);
}
} while (it.hasNext());
return Optional.empty();
}
use of org.eclipse.winery.model.ids.definitions.RelationshipTypeId in project winery by eclipse.
the class DataFlowResource method parseDataFlowToServiceTemplate.
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response parseDataFlowToServiceTemplate(DataFlowModel dataFlowModel) {
if (Objects.isNull(dataFlowModel)) {
return Response.status(Response.Status.BAD_REQUEST).entity("Passed data flow model is null!").build();
}
if (Objects.isNull(dataFlowModel.getId().getNamespaceURI())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Namespace must be defined for the data flow " + "model ID!").build();
}
IRepository repo = RepositoryFactory.getRepository();
ServiceTemplateId templateId = new ServiceTemplateId(dataFlowModel.getId());
if (repo.exists(templateId)) {
return Response.status(Response.Status.CONFLICT).entity("ServiceTemplate with name of the data flow model already exists!").build();
}
TDefinitions definitions = BackendUtils.createWrapperDefinitionsAndInitialEmptyElement(repo, templateId);
TServiceTemplate serviceTemplate = definitions.getServiceTemplates().stream().filter(template -> template.getId().equals(templateId.getQName().getLocalPart()) && templateId.getQName().getNamespaceURI().equals(template.getTargetNamespace())).findFirst().orElse(null);
if (Objects.isNull(serviceTemplate)) {
return Response.serverError().entity("Unable to create ServiceTemplate for the given data flow model!").build();
}
TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
if (Objects.isNull(topology)) {
topology = new TTopologyTemplate.Builder().build();
}
// iterate over all filters of the data flow and create corresponding NodeTemplates
for (DataFlowModel.Filter filter : dataFlowModel.getFilters()) {
if (Objects.isNull(filter.getType())) {
return Response.serverError().entity("Type is missing for a filter!").build();
}
NodeTypeId nodeTypeId = BackendUtils.getDefinitionsChildId(NodeTypeId.class, filter.getType());
if (!repo.exists(nodeTypeId)) {
TNodeType newNodeType = new TNodeType.Builder(nodeTypeId.getQName().getLocalPart()).setTargetNamespace(nodeTypeId.getQName().getNamespaceURI()).build();
try {
BackendUtils.persist(repo, nodeTypeId, newNodeType);
} catch (IOException e) {
return Response.serverError().entity("Unable to create NodeType " + filter.getType() + " which is not contained in the repository!").build();
}
}
topology = handleFilter(topology, nodeTypeId, filter.getId(), filter.getProperties(), filter.getArtifacts(), filter.getLocation(), filter.getProvider());
if (Objects.isNull(topology)) {
return Response.serverError().entity("Unable to handle filter with name: " + filter.getId()).build();
}
}
// without available connectsTo RelationshipType the transformation can not be done
RelationshipTypeId relationTypeId = BackendUtils.getDefinitionsChildId(RelationshipTypeId.class, ToscaBaseTypes.connectsToRelationshipType);
if (!repo.exists(relationTypeId)) {
return Response.serverError().entity("Unable to parse data flow model without available connectsTo " + "RelationshipType!").build();
}
// create connectsTo RelationshipTemplates between NodeTemplates corresponding to connected filters
for (DataFlowModel.Pipes pipe : dataFlowModel.getPipes()) {
if (Objects.isNull(pipe.getSource()) || Objects.isNull(pipe.getTarget())) {
return Response.serverError().entity("Unable to create RelationshipTemplate for pipe with source or " + "target equal to null!").build();
}
TNodeTemplate source = topology.getNodeTemplate(pipe.getSource());
TNodeTemplate target = topology.getNodeTemplate(pipe.getTarget());
if (Objects.isNull(source) || Objects.isNull(target)) {
return Response.serverError().entity("Unable to find NodeTemplates for relation with source: " + pipe.getSource() + " and target: " + pipe.getTarget()).build();
}
TRelationshipTemplate relationshipTemplate = createRelationshipTemplate(relationTypeId, source, target, pipe.getDataTransferType());
if (Objects.isNull(relationshipTemplate)) {
return Response.serverError().entity("Unable to create RelationshipTemplate between " + source.getId() + " and " + target.getId()).build();
}
topology.addRelationshipTemplate(relationshipTemplate);
}
serviceTemplate.setTopologyTemplate(topology);
try {
BackendUtils.persist(repo, templateId, definitions);
return Response.created(new URI(RestUtils.getAbsoluteURL(templateId))).build();
} catch (IOException e) {
return Response.serverError().entity("IOException while persisting ServiceTemplate for data flow model!").build();
} catch (URISyntaxException e) {
return Response.serverError().entity("Unable to parse URI for created ServiceTemplate!").build();
}
}
use of org.eclipse.winery.model.ids.definitions.RelationshipTypeId in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId id) {
// We have to use a HashSet to ensure that no duplicate ids are added<
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
TServiceTemplate serviceTemplate = this.getElement(id);
// add included things to export queue
TBoundaryDefinitions boundaryDefs;
if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
List<TPolicy> policies = boundaryDefs.getPolicies();
if (policies != null) {
for (TPolicy policy : policies) {
PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
ids.add(policyTypeId);
PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
ids.add(policyTemplateId);
}
}
// reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
}
final TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
if (topology != null) {
if (Objects.nonNull(topology.getPolicies())) {
topology.getPolicies().stream().filter(Objects::nonNull).forEach(p -> {
QName type = p.getPolicyType();
PolicyTypeId policyTypeIdId = new PolicyTypeId(type);
ids.add(policyTypeIdId);
});
}
for (TEntityTemplate entityTemplate : topology.getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through policies
List<TPolicy> policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
QName template = pol.getPolicyRef();
if (template != null) {
PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
ids.add(policyTemplateId);
}
}
}
// Crawl RequirementTypes and Capabilities for their references
getReferencedRequirementTypeIds(ids, n);
getCapabilitiesReferences(ids, n);
// TODO: this information is collected differently for YAML and XML modes
// crawl through deployment artifacts
List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
if (da.getArtifactType() != null) {
// This is considered Nullable, because the test case ConsistencyCheckerTest#hasError
// expects an empty artifactType and thus it may be null.
ids.add(new ArtifactTypeId(da.getArtifactType()));
}
if (da.getArtifactRef() != null) {
ids.add(new ArtifactTemplateId(da.getArtifactRef()));
}
}
}
// Store all referenced artifact types
List<TArtifact> artifacts = n.getArtifacts();
if (Objects.nonNull(artifacts)) {
artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
}
TNodeType nodeType = this.getElement(new NodeTypeId(qname));
if (Objects.nonNull(nodeType.getInterfaceDefinitions())) {
nodeType.getInterfaceDefinitions().stream().filter(Objects::nonNull).forEach(iDef -> {
if (Objects.nonNull(iDef.getType())) {
ids.add(new InterfaceTypeId(iDef.getType()));
}
});
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
Aggregations