use of org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId 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;
}
use of org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId in project winery by eclipse.
the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.
public static TDefinitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
final TDefinitions definitions = createWrapperDefinitions(id, repository);
HasIdInIdOrNameField element;
if (id instanceof RelationshipTypeImplementationId) {
element = new TRelationshipTypeImplementation();
} else if (id instanceof NodeTypeImplementationId) {
element = new TNodeTypeImplementation();
} else if (id instanceof RequirementTypeId) {
element = new TRequirementType();
} else if (id instanceof NodeTypeId) {
element = new TNodeType();
} else if (id instanceof RelationshipTypeId) {
element = new TRelationshipType();
} else if (id instanceof CapabilityTypeId) {
element = new TCapabilityType();
} else if (id instanceof DataTypeId) {
element = new TDataType();
} else if (id instanceof ArtifactTypeId) {
element = new TArtifactType();
} else if (id instanceof PolicyTypeId) {
element = new TPolicyType();
} else if (id instanceof PolicyTemplateId) {
element = new TPolicyTemplate();
} else if (id instanceof ServiceTemplateId) {
element = new TServiceTemplate();
} else if (id instanceof ArtifactTemplateId) {
element = new TArtifactTemplate();
} else if (id instanceof ComplianceRuleId) {
element = new OTComplianceRule(new OTComplianceRule.Builder(id.getXmlId().getDecoded()));
} else if (id instanceof PatternRefinementModelId) {
element = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
} else if (id instanceof TopologyFragmentRefinementModelId) {
element = new OTTopologyFragmentRefinementModel(new OTPatternRefinementModel.Builder());
} else if (id instanceof TestRefinementModelId) {
element = new OTTestRefinementModel(new OTTestRefinementModel.Builder());
} else if (id instanceof InterfaceTypeId) {
element = new TInterfaceType();
} else if (id instanceof XSDImportId) {
// TImport has no id; thus directly generating it without setting an id
TImport tImport = new TImport();
definitions.setElement(tImport);
return definitions;
} else {
throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
}
copyIdToFields(element, id);
definitions.setElement((TExtensibleElements) element);
return definitions;
}
use of org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId 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.ids.definitions.NodeTypeImplementationId in project winery by eclipse.
the class YamlRepository method convertToYamlModel.
private YTServiceTemplate convertToYamlModel(RepositoryFileReference existing, TDefinitions definitions) throws IOException, MultiException {
FromCanonical converter = new FromCanonical(this);
YTServiceTemplate serviceTemplate;
if (existing.getParent() instanceof NodeTypeImplementationId) {
serviceTemplate = readServiceTemplate(existing);
serviceTemplate = converter.convertNodeTypeImplementation(serviceTemplate, definitions.getNodeTypeImplementations().get(0));
} else if (existing.getParent() instanceof RelationshipTypeImplementationId) {
serviceTemplate = readServiceTemplate(existing);
serviceTemplate = converter.convertRelationshipTypeImplementation(serviceTemplate, definitions.getRelationshipTypeImplementations().get(0));
} else if (existing.getParent() instanceof NodeTypeId) {
serviceTemplate = converter.convert(definitions);
if (exists(existing)) {
YTServiceTemplate oldServiceTemplate = readServiceTemplate(existing);
serviceTemplate = replaceOldWithNewData(serviceTemplate, oldServiceTemplate);
}
} else if (existing.getParent() instanceof RelationshipTypeId) {
serviceTemplate = converter.convert(definitions);
if (exists(existing)) {
YTServiceTemplate oldServiceTemplate = readServiceTemplate(existing);
serviceTemplate = replaceOldRelationshipTypeWithNewData(serviceTemplate, oldServiceTemplate);
}
} else if (existing.getParent() instanceof ArtifactTemplateId) {
ArtifactTemplateId id = (ArtifactTemplateId) existing.getParent();
TArtifactTemplate artifactTemplate = definitions.getArtifactTemplates().get(0);
YTArtifactDefinition artifact = converter.convertArtifactTemplate(artifactTemplate);
List<YTMapImportDefinition> imports = converter.convertImports();
Path targetPath = ref2AbsolutePath(existing);
if (Files.exists(targetPath)) {
serviceTemplate = readServiceTemplate(targetPath);
if (serviceTemplate == null) {
serviceTemplate = createNewCacheNodeTypeWithArtifact(existing, artifactTemplate, artifact, imports);
} else if (getTypeFromArtifactName(id.getQName().getLocalPart()).equalsIgnoreCase("nodetypes")) {
YTNodeType nodeType = serviceTemplate.getNodeTypes().entrySet().iterator().next().getValue();
Map<String, YTArtifactDefinition> artifacts = nodeType.getArtifacts();
if (artifacts.containsKey(artifactTemplate.getIdFromIdOrNameField())) {
artifacts.replace(artifactTemplate.getIdFromIdOrNameField(), artifact);
} else {
artifacts.put(artifactTemplate.getIdFromIdOrNameField(), artifact);
}
} else if (existing.getParent() instanceof PolicyTypeId || existing.getParent() instanceof CapabilityTypeId) {
// we simply take the new definition as is
serviceTemplate = converter.convert(definitions);
} else {
serviceTemplate = converter.convert(definitions);
if (exists(existing)) {
YTServiceTemplate existingServiceTemplate = readServiceTemplate(existing);
serviceTemplate = replaceTopologyTemplate(serviceTemplate, existingServiceTemplate);
}
}
} else {
serviceTemplate = createNewCacheNodeTypeWithArtifact(existing, artifactTemplate, artifact, imports);
}
} else {
serviceTemplate = converter.convert(definitions);
}
return serviceTemplate;
}
use of org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId in project winery by eclipse.
the class ConsistencyCheckerTest method openToscaNamespaceIsNotLowerCaseErrorRaisedAtWrongNodeTypeImplementationNamespace.
@Test
public void openToscaNamespaceIsNotLowerCaseErrorRaisedAtWrongNodeTypeImplementationNamespace() {
QName qname = new QName("http://www.opentosca.org/NodeTypeImplementations/fruits", "baobab_impl");
NodeTypeImplementationId id = new NodeTypeImplementationId(qname);
consistencyChecker.checkNamespaceUri(id);
Map<QName, ElementErrorList> expected = new HashMap<>();
ElementErrorList elementErrorList;
elementErrorList = new ElementErrorList("NodeTypeImplementation");
elementErrorList.addError("opentosca URI is not lowercase");
expected.put(qname, elementErrorList);
assertEquals(expected, consistencyChecker.getErrorCollector().getErrorList());
}
Aggregations