use of org.eclipse.winery.model.tosca.TNodeTypeImplementation in project winery by eclipse.
the class SelfContainmentPackager method createSelfContainedVersion.
public DefinitionsChildId createSelfContainedVersion(DefinitionsChildId entryId) throws IOException {
ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(VersionSupport.getSelfContainedVersion(entryId));
if (!repository.exists(newServiceTemplateId)) {
repository.duplicate(entryId, newServiceTemplateId);
TServiceTemplate serviceTemplate = repository.getElement(newServiceTemplateId);
Collection<DefinitionsChildId> referencedElements = repository.getReferencedDefinitionsChildIds(newServiceTemplateId);
for (DefinitionsChildId elementId : referencedElements) {
if (elementId instanceof NodeTypeId) {
Collection<NodeTypeImplementationId> nodeTypeImplementationIds = repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, elementId.getQName());
if (nodeTypeImplementationIds.stream().noneMatch(DefinitionsChildId::isSelfContained)) {
// self-contained element does not exist yet!
List<TNodeTypeImplementation> nodeTypeImplementations = nodeTypeImplementationIds.stream().map(repository::getElement).filter(element -> element.getImplementationArtifacts() != null).collect(Collectors.toList());
for (TNodeTypeImplementation impl : nodeTypeImplementations) {
Optional<SelfContainmentPlugin> nodeTypeBasedPlugin = this.selfContainmentPlugins.stream().filter(plugin -> plugin.canHandleNodeType(elementId.getQName(), repository)).findFirst();
if (nodeTypeBasedPlugin.isPresent()) {
NodeTypeImplementationId selfContainedNodeTypeImpl = getSelfContainedNodeTypeImplId(impl);
try {
repository.duplicate(new NodeTypeImplementationId(impl.getTargetNamespace(), impl.getIdFromIdOrNameField(), false), selfContainedNodeTypeImpl);
TNodeTypeImplementation selfContained = this.repository.getElement(selfContainedNodeTypeImpl);
nodeTypeBasedPlugin.get().downloadDependenciesBasedOnNodeType(selfContained, this.repository);
repository.setElement(selfContainedNodeTypeImpl, selfContained);
} catch (IOException e) {
logger.error("While creating self-contained Node Type Implementation", e);
}
} else if (impl.getImplementationArtifacts() != null) {
createSelfContainedNodeTypeImplementation(impl);
}
}
}
} else if (elementId instanceof ArtifactTemplateId) {
if (serviceTemplate.getTopologyTemplate() != null) {
TArtifactTemplate artifactTemplate = repository.getElement(elementId);
SelfContainmentPlugin.GeneratedArtifacts generatedArtifacts = this.downloadArtifacts(elementId.getQName(), artifactTemplate.getType());
if (generatedArtifacts != null && generatedArtifacts.selfContainedArtifactQName != null) {
// first, we need to identify the element that is referencing the artifact
serviceTemplate.getTopologyTemplate().getNodeTemplates().stream().map(TNodeTemplate::getDeploymentArtifacts).filter(Objects::nonNull).filter(daList -> daList.stream().anyMatch(da -> da.getArtifactRef() != null && da.getArtifactRef().equals(elementId.getQName()))).flatMap(Collection::stream).forEach(da -> da.setArtifactRef(generatedArtifacts.selfContainedArtifactQName));
}
}
}
}
repository.setElement(newServiceTemplateId, serviceTemplate);
} else {
logger.info("Self-contained version already exists! '{}'", newServiceTemplateId.getQName());
}
return newServiceTemplateId;
}
use of org.eclipse.winery.model.tosca.TNodeTypeImplementation in project winery by eclipse.
the class IGenericRepository method getReferenceCount.
default int getReferenceCount(ArtifactTemplateId id) {
// We do not use a database, therefore, we have to go through all possibilities pointing to the artifact template
// DAs and IAs point to an artifact template
// DAs are contained in Node Type Implementations and Node Templates
// IAs are contained in Node Type Implementations and Relationship Type Implementations
int count = 0;
Collection<TDeploymentArtifact> allDAs = new HashSet<>();
Collection<TImplementationArtifact> allIAs = new HashSet<>();
// handle Node Type Implementation, which contains DAs and IAs
SortedSet<NodeTypeImplementationId> nodeTypeImplementations = this.getAllDefinitionsChildIds(NodeTypeImplementationId.class);
for (NodeTypeImplementationId ntiId : nodeTypeImplementations) {
final TNodeTypeImplementation nodeTypeImplementation = this.getElement(ntiId);
TDeploymentArtifacts deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
allDAs.addAll(deploymentArtifacts.getDeploymentArtifact());
}
TImplementationArtifacts implementationArtifacts = nodeTypeImplementation.getImplementationArtifacts();
if (implementationArtifacts != null) {
allIAs.addAll(implementationArtifacts.getImplementationArtifact());
}
}
// check all Relationshiptype Implementations for IAs
SortedSet<RelationshipTypeImplementationId> relationshipTypeImplementations = this.getAllDefinitionsChildIds(RelationshipTypeImplementationId.class);
for (RelationshipTypeImplementationId rtiId : relationshipTypeImplementations) {
TImplementationArtifacts implementationArtifacts = this.getElement(rtiId).getImplementationArtifacts();
if (implementationArtifacts != null) {
allIAs.addAll(implementationArtifacts.getImplementationArtifact());
}
}
// check all node templates for DAs
SortedSet<ServiceTemplateId> serviceTemplates = this.getAllDefinitionsChildIds(ServiceTemplateId.class);
for (ServiceTemplateId sid : serviceTemplates) {
TTopologyTemplate topologyTemplate = this.getElement(sid).getTopologyTemplate();
if (topologyTemplate != null) {
List<TEntityTemplate> nodeTemplateOrRelationshipTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
for (TEntityTemplate template : nodeTemplateOrRelationshipTemplate) {
if (template instanceof TNodeTemplate) {
TNodeTemplate nodeTemplate = (TNodeTemplate) template;
TDeploymentArtifacts deploymentArtifacts = nodeTemplate.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
allDAs.addAll(deploymentArtifacts.getDeploymentArtifact());
}
}
}
}
}
// now we have all DAs and IAs
QName ourQName = id.getQName();
// check DAs for artifact templates
for (TDeploymentArtifact da : allDAs) {
QName artifactRef = da.getArtifactRef();
if (ourQName.equals(artifactRef)) {
count++;
}
}
// check IAs for artifact templates
for (TImplementationArtifact ia : allIAs) {
QName artifactRef = ia.getArtifactRef();
if (ourQName.equals(artifactRef)) {
count++;
}
}
return count;
}
use of org.eclipse.winery.model.tosca.TNodeTypeImplementation in project winery by eclipse.
the class IRepository method getReferenceCount.
default int getReferenceCount(ArtifactTemplateId id) {
// We do not use a database, therefore, we have to go through all possibilities pointing to the artifact template
// DAs and IAs point to an artifact template
// DAs are contained in Node Type Implementations and Node Templates
// IAs are contained in Node Type Implementations and Relationship Type Implementations
int count = 0;
Collection<TDeploymentArtifact> allDAs = new HashSet<>();
Collection<TImplementationArtifact> allIAs = new HashSet<>();
// handle Node Type Implementation, which contains DAs and IAs
SortedSet<NodeTypeImplementationId> nodeTypeImplementations = this.getAllDefinitionsChildIds(NodeTypeImplementationId.class);
for (NodeTypeImplementationId ntiId : nodeTypeImplementations) {
final TNodeTypeImplementation nodeTypeImplementation = this.getElement(ntiId);
List<TDeploymentArtifact> deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
allDAs.addAll(deploymentArtifacts);
}
List<TImplementationArtifact> implementationArtifacts = nodeTypeImplementation.getImplementationArtifacts();
if (implementationArtifacts != null) {
allIAs.addAll(implementationArtifacts);
}
}
// check all RelationshipTypeImplementations for IAs
SortedSet<RelationshipTypeImplementationId> relationshipTypeImplementations = this.getAllDefinitionsChildIds(RelationshipTypeImplementationId.class);
for (RelationshipTypeImplementationId rtiId : relationshipTypeImplementations) {
List<TImplementationArtifact> implementationArtifacts = this.getElement(rtiId).getImplementationArtifacts();
if (implementationArtifacts != null) {
allIAs.addAll(implementationArtifacts);
}
}
// check all node templates for DAs
SortedSet<ServiceTemplateId> serviceTemplates = this.getAllDefinitionsChildIds(ServiceTemplateId.class);
for (ServiceTemplateId sid : serviceTemplates) {
TTopologyTemplate topologyTemplate = this.getElement(sid).getTopologyTemplate();
if (topologyTemplate != null) {
List<TEntityTemplate> nodeTemplateOrRelationshipTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
for (TEntityTemplate template : nodeTemplateOrRelationshipTemplate) {
if (template instanceof TNodeTemplate) {
TNodeTemplate nodeTemplate = (TNodeTemplate) template;
List<TDeploymentArtifact> deploymentArtifacts = nodeTemplate.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
allDAs.addAll(deploymentArtifacts);
}
}
}
}
}
// now we have all DAs and IAs
QName ourQName = id.getQName();
// check DAs for artifact templates
for (TDeploymentArtifact da : allDAs) {
QName artifactRef = da.getArtifactRef();
if (ourQName.equals(artifactRef)) {
count++;
}
}
// check IAs for artifact templates
for (TImplementationArtifact ia : allIAs) {
QName artifactRef = ia.getArtifactRef();
if (ourQName.equals(artifactRef)) {
count++;
}
}
return count;
}
use of org.eclipse.winery.model.tosca.TNodeTypeImplementation in project winery by eclipse.
the class SelfContainmentPackager method createSelfContainedNodeTypeImplementation.
private void createSelfContainedNodeTypeImplementation(TNodeTypeImplementation impl) {
if (impl.getImplementationArtifacts() != null) {
List<SelfContainmentPlugin.GeneratedArtifacts> generatedArtifacts = impl.getImplementationArtifacts().stream().filter(ia -> Objects.nonNull(ia.getArtifactRef())).filter(ia -> !new ArtifactTemplateId(ia.getArtifactRef()).isSelfContained()).map(ia -> this.downloadArtifacts(ia.getArtifactRef(), ia.getArtifactType())).filter(Objects::nonNull).filter(SelfContainmentPlugin.GeneratedArtifacts::containsNewElements).collect(Collectors.toList());
if (!generatedArtifacts.isEmpty()) {
NodeTypeImplementationId nodeTypeImplementationId = getSelfContainedNodeTypeImplId(impl);
try {
this.repository.duplicate(new NodeTypeImplementationId(impl.getQName()), nodeTypeImplementationId);
TNodeTypeImplementation implementation = this.repository.getElement(nodeTypeImplementationId);
generatedArtifacts.forEach(generatedArtifact -> {
if (implementation.getImplementationArtifacts() != null) {
implementation.getImplementationArtifacts().forEach(ia -> {
if (generatedArtifact.artifactToReplaceQName.equals(ia.getArtifactRef())) {
ia.setArtifactRef(generatedArtifact.selfContainedArtifactQName);
TArtifactTemplate artifactTemplate = generatedArtifact.selfContainedArtifactTemplate;
if (artifactTemplate == null) {
artifactTemplate = repository.getElement(new ArtifactTemplateId(generatedArtifact.selfContainedArtifactQName));
}
if (artifactTemplate.getType() == null) {
logger.error("Artifact Template does not have a type assigned! {}", generatedArtifact.selfContainedArtifactQName);
}
ia.setArtifactType(artifactTemplate.getType());
}
});
List<TDeploymentArtifact> deploymentArtifacts = implementation.getDeploymentArtifacts();
if (deploymentArtifacts == null) {
deploymentArtifacts = new ArrayList<>();
implementation.setDeploymentArtifacts(deploymentArtifacts);
} else {
deploymentArtifacts.removeIf(da -> da.getArtifactRef() != null && generatedArtifact.deploymentArtifactsToRemove.contains(da.getArtifactRef()));
}
for (QName artifactTemplate : generatedArtifact.deploymentArtifactsToAdd) {
TArtifactTemplate generatedAT = repository.getElement(new ArtifactTemplateId(artifactTemplate));
deploymentArtifacts.add(new TDeploymentArtifact.Builder(artifactTemplate.getLocalPart(), generatedAT.getType()).setArtifactRef(artifactTemplate).build());
}
}
});
repository.setElement(nodeTypeImplementationId, implementation);
} catch (IOException e) {
logger.error("Error while creating new self-contained NodeTypeImplementation of {}", impl, e);
}
}
} else {
logger.info("No processable IAs found in Node Type Implementation {}", impl.getQName());
}
}
use of org.eclipse.winery.model.tosca.TNodeTypeImplementation in project winery by eclipse.
the class RestUtils method getEdmmEntityGraph.
public static EntityGraph getEdmmEntityGraph(TServiceTemplate element, boolean useAbsolutPaths) {
IRepository repository = RepositoryFactory.getRepository();
Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
Map<QName, TRelationshipType> relationshipTypes = repository.getQNameToElementMapping(RelationshipTypeId.class);
Map<QName, TNodeTypeImplementation> nodeTypeImplementations = repository.getQNameToElementMapping(NodeTypeImplementationId.class);
Map<QName, TRelationshipTypeImplementation> relationshipTypeImplementations = repository.getQNameToElementMapping(RelationshipTypeImplementationId.class);
Map<QName, TArtifactTemplate> artifactTemplates = repository.getQNameToElementMapping(ArtifactTemplateId.class);
EdmmManager edmmManager = EdmmManager.forRepository(repository);
Map<QName, EdmmType> oneToOneMappings = edmmManager.getOneToOneMap();
Map<QName, EdmmType> typeMappings = edmmManager.getTypeMap();
if (nodeTypes.isEmpty()) {
throw new IllegalStateException("No Node Types defined!");
} else if (relationshipTypes.isEmpty()) {
throw new IllegalStateException("No Relationship Types defined!");
}
EdmmConverter edmmConverter = new EdmmConverter(nodeTypes, relationshipTypes, nodeTypeImplementations, relationshipTypeImplementations, artifactTemplates, typeMappings, oneToOneMappings, useAbsolutPaths);
return edmmConverter.transform(element);
}
Aggregations