use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.
the class ModelUtilities method instantiateNodeTemplate.
/**
* This method instantiates a {@link TNodeTemplate} for a given {@link TNodeType}.
*
* @param nodeType the {@link TNodeType} used for the {@link TNodeTemplate} instantiation.
* @return the instantiated {@link TNodeTemplate}
*/
public static TNodeTemplate instantiateNodeTemplate(TNodeType nodeType) {
String nodeTemplateId = nodeType.getIdFromIdOrNameField() + Math.random();
TNodeTemplate.Builder builder = new TNodeTemplate.Builder(nodeTemplateId, nodeType.getQName());
builder.setName(nodeType.getName());
// add capabilities to the NodeTemplate
if (nodeType.getCapabilityDefinitions() != null) {
for (TCapabilityDefinition cd : nodeType.getCapabilityDefinitions()) {
TCapability capability = new TCapability.Builder(cd.getName() + nodeTemplateId, cd.getCapabilityType(), cd.getName()).build();
builder.addCapability(capability);
}
}
// add requirements
if (nodeType.getRequirementDefinitions() != null) {
nodeType.getRequirementDefinitions().forEach(tRequirementDefinition -> {
TRequirement requirement = new TRequirement.Builder(tRequirementDefinition.getName() + nodeTemplateId, tRequirementDefinition.getName(), tRequirementDefinition.getRequirementType()).build();
builder.addRequirement(requirement);
});
}
// add properties
WinerysPropertiesDefinition propDef = nodeType.getWinerysPropertiesDefinition();
if (propDef != null && propDef.getPropertyDefinitions() != null) {
Map<String, String> properties = new HashMap<>();
propDef.getPropertyDefinitions().forEach(propertyDefinition -> properties.put(propertyDefinition.getKey(), propertyDefinition.getDefaultValue()));
TEntityTemplate.WineryKVProperties tProps = new TEntityTemplate.WineryKVProperties();
tProps.setKVProperties(new LinkedHashMap<>(properties));
builder.setProperties(tProps);
}
return builder.build();
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.
the class ModelUtilities method mergePropertiesDefinitions.
/**
* Merge properties definitions.
* Only winery properties definitions are considered.
* The first element in the list is the lowest in the inheritance hierarchy.
*/
public static <T extends TEntityType> List<PropertyDefinitionKV> mergePropertiesDefinitions(List<T> entityTypes) {
List<PropertyDefinitionKV> propertyDefinitions = new ArrayList<>();
for (int i = 0; i < entityTypes.size(); i++) {
TEntityType entityType = entityTypes.get(i);
WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
// Continue if current entity type does not have any properties definitions
if (winerysPropertiesDefinition == null) {
continue;
}
// Continue if current entity type does not have any properties definitions
List<PropertyDefinitionKV> winerysPropertiesDefinitions = winerysPropertiesDefinition.getPropertyDefinitions();
if (winerysPropertiesDefinitions == null) {
continue;
}
// Add property definition to list if not already added by a previous entity type
for (PropertyDefinitionKV entityTypePropertyDefinition : winerysPropertiesDefinitions) {
boolean exists = false;
for (PropertyDefinitionKV propertyDefinition : propertyDefinitions) {
if (Objects.equals(propertyDefinition.getKey(), entityTypePropertyDefinition.getKey())) {
if (i == 1) {
propertyDefinition.setDerivedFromStatus("OVERRIDE");
}
exists = true;
break;
}
}
if (!exists) {
entityTypePropertyDefinition.setDerivedFromType(entityType.getQName());
if (i == 0) {
entityTypePropertyDefinition.setDerivedFromStatus("SELF");
} else {
entityTypePropertyDefinition.setDerivedFromStatus("INHERITED");
}
propertyDefinitions.add(entityTypePropertyDefinition);
}
}
}
return propertyDefinitions;
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.
the class ServiceTemplateResource method generatePlaceholdersWithCapability.
@POST
@Path("placeholder/generator")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response generatePlaceholdersWithCapability() {
Splitting splitting = new Splitting();
TTopologyTemplate topologyTemplate = this.getServiceTemplate().getTopologyTemplate();
if (topologyTemplate == null) {
return Response.notModified().build();
}
try {
// get all open requirements and the respective node templates with open requirements
Map<TRequirement, TNodeTemplate> requirementsAndItsNodeTemplates = splitting.getOpenRequirementsAndItsNodeTemplate(topologyTemplate);
IRepository repo = RepositoryFactory.getRepository();
// iterate over all open requirements
for (Map.Entry<TRequirement, TNodeTemplate> entry : requirementsAndItsNodeTemplates.entrySet()) {
List<PropertyDefinitionKV> propertyDefinitionKVList = new ArrayList<>();
LinkedHashMap<String, String> placeholderNodeTemplateProperties = new LinkedHashMap<>();
// current node template with open requirements
TNodeTemplate nodeTemplateWithOpenReq = entry.getValue();
// get type of node template with open requirements
NodeTypeId id = new NodeTypeId(nodeTemplateWithOpenReq.getType());
TNodeType sourceNodeType = repo.getElement(id);
List<TInterface> sourceNodeTypeInterfaces = sourceNodeType.getInterfaces();
if (sourceNodeTypeInterfaces != null) {
for (TInterface tInterface : sourceNodeTypeInterfaces) {
// TODO: make this more safe
for (TOperation tOperation : tInterface.getOperations()) {
List<TParameter> inputParameters = tOperation.getInputParameters();
if (inputParameters != null) {
for (TParameter inputParameter : inputParameters) {
generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
}
}
}
}
}
List<TRelationshipTemplate> incomingRelationshipTemplates = ModelUtilities.getIncomingRelationshipTemplates(topologyTemplate, nodeTemplateWithOpenReq);
List<TParameter> inputParameters = splitting.getInputParamListofIncomingRelationshipTemplates(topologyTemplate, incomingRelationshipTemplates);
for (TParameter inputParameter : inputParameters) {
String prefixTARGET = "TARGET_";
String prefixSOURCE = "SOURCE_";
String inputParamName = inputParameter.getName();
if (inputParamName.contains(prefixTARGET)) {
inputParamName = inputParamName.replaceAll(prefixTARGET, "");
}
if (inputParamName.contains(prefixSOURCE)) {
inputParamName = inputParamName.replaceAll(prefixSOURCE, "");
}
inputParameter.setName(inputParamName);
generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
}
// get required capability type of open requirement
QName capabilityType = splitting.getRequiredCapabilityTypeQNameOfRequirement(entry.getKey());
// create new placeholder node type
TNodeType placeholderNodeType = splitting.createPlaceholderNodeType(nodeTemplateWithOpenReq.getName());
QName placeholderQName = new QName(placeholderNodeType.getTargetNamespace(), placeholderNodeType.getName());
WinerysPropertiesDefinition winerysPropertiesDefinition = sourceNodeType.getWinerysPropertiesDefinition();
// add properties definition
placeholderNodeType.setProperties(null);
if (winerysPropertiesDefinition != null) {
winerysPropertiesDefinition.setPropertyDefinitions(propertyDefinitionKVList);
placeholderNodeType.setProperties(winerysPropertiesDefinition);
String namespace = placeholderNodeType.getWinerysPropertiesDefinition().getNamespace();
NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
if (!namespaceManager.hasPermanentProperties(namespace)) {
namespaceManager.addPermanentNamespace(namespace);
}
}
NodeTypeId placeholderId = new NodeTypeId(placeholderQName);
// check if placeholder node type exists
if (repo.exists(placeholderId)) {
// delete and create new
RestUtils.delete(placeholderId);
}
repo.setElement(placeholderId, placeholderNodeType);
// create placeholder node template
TNodeTemplate placeholderNodeTemplate = splitting.createPlaceholderNodeTemplate(topologyTemplate, nodeTemplateWithOpenReq.getName(), placeholderQName);
// create capability of placeholder node template
TCapability capa = splitting.createPlaceholderCapability(topologyTemplate, capabilityType);
ModelUtilities.setPropertiesKV(placeholderNodeTemplate, placeholderNodeTemplateProperties);
if (placeholderNodeTemplate.getCapabilities() == null) {
placeholderNodeTemplate.setCapabilities(new ArrayList<>());
}
placeholderNodeTemplate.getCapabilities().add(capa);
for (Map.Entry<QName, String> targetLocation : nodeTemplateWithOpenReq.getOtherAttributes().entrySet()) {
placeholderNodeTemplate.getOtherAttributes().put(targetLocation.getKey(), targetLocation.getValue());
}
// add placeholder to node template and connect with source node template with open requirements
topologyTemplate.addNodeTemplate(placeholderNodeTemplate);
ModelUtilities.createRelationshipTemplateAndAddToTopology(nodeTemplateWithOpenReq, placeholderNodeTemplate, ToscaBaseTypes.hostedOnRelationshipType, topologyTemplate);
}
LOGGER.debug("PERSISTING");
RestUtils.persist(this);
LOGGER.debug("PERSISTED");
String responseId = this.getServiceTemplate().getId();
return Response.ok().entity(responseId).build();
} catch (Exception e) {
LOGGER.error("Could not fetch requirements and capabilities", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
}
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.
the class CsarImporter method adjustEntityType.
/**
* All EntityTypes may contain properties definition. In case a winery properties definition is found, the TOSCA
* conforming properties definition is removed
*
* @param ci the entity type
* @param wid the Winery id of the entityType
* @param newDefs the definitions, the entity type is contained in. The imports might be adjusted here
* @param errors Used to collect the errors
*/
private void adjustEntityType(TEntityType ci, EntityTypeId wid, TDefinitions newDefs, final List<String> errors) {
TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
if (propertiesDefinition != null) {
WinerysPropertiesDefinition winerysPropertiesDefinition = ci.getWinerysPropertiesDefinition();
boolean deriveWPD;
if (winerysPropertiesDefinition == null) {
deriveWPD = true;
} else {
if (winerysPropertiesDefinition.getIsDerivedFromXSD() == null) {
// no derivation from properties required as the properties are generated by Winery
deriveWPD = false;
// we have to remove the import, too
// Determine the location
String elementName = winerysPropertiesDefinition.getElementName();
String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD(wid, null, elementName);
// remove the import matching that location
List<TImport> imports = newDefs.getImport();
boolean found = false;
if (imports != null) {
Iterator<TImport> iterator = imports.iterator();
TImport imp;
while (iterator.hasNext()) {
imp = iterator.next();
// TODO: add check for QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE instead of import location. The current routine, however, works, too.
if (imp.getLocation().equals(loc)) {
found = true;
break;
}
}
// noinspection StatementWithEmptyBody
if (found) {
// imp with Winery's k/v location found
iterator.remove();
// the XSD has been imported in importOtherImport
// it was too difficult to do the location check there, therefore we just remove the XSD from the repository here
XSDImportId importId = new XSDImportId(winerysPropertiesDefinition.getNamespace(), elementName, false);
try {
this.targetRepository.forceDelete(importId);
} catch (IOException e) {
CsarImporter.LOGGER.debug("Could not delete Winery's generated XSD definition", e);
errors.add("Could not delete Winery's generated XSD definition");
}
} else {
// K/V properties definition was incomplete
}
}
} else {
// winery's properties are derived from an XSD
// The export does NOT add an imports statement: only the wpd exists
// We remove that as
ModelUtilities.removeWinerysPropertiesDefinition(ci);
// derive the WPDs again from the properties definition
deriveWPD = true;
}
}
if (deriveWPD) {
BackendUtils.deriveWPD(ci, errors, targetRepository);
}
}
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.
the class PropertiesResource method getProperties.
/**
* Gets the defined properties.
* Inheritance is not considered, see {@link #getEffectiveProperties()} instead.
*
* If no properties are defined, an empty JSON object is returned.
* If k/v properties are defined, then a JSON object is returned.
* If xml properties are defined, then an XML object is returned.
* Otherwise, an empty JSON is returned.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@NonNull
public Response getProperties() {
TEntityTemplate.Properties properties = this.template.getProperties();
WinerysPropertiesDefinition propertiesDefinition = RepositoryFactory.getRepository().getTypeForTemplate(this.template).getWinerysPropertiesDefinition();
// CASE "WKV": Return WineryKV properties as JSON if existed.
if (properties instanceof TEntityTemplate.WineryKVProperties || (properties == null && propertiesDefinition != null)) {
return Response.ok().entity(ModelUtilities.getPropertiesKV(template)).type(MediaType.APPLICATION_JSON).build();
}
// CASE "YAML": Return YAML properties as JSON if existed.
if (properties instanceof TEntityTemplate.YamlProperties) {
// hurrah for yaml properties
return Response.ok().entity(((TEntityTemplate.YamlProperties) properties).getProperties()).type(MediaType.APPLICATION_JSON).build();
}
// CASE "XML": Return XML properties as XML if existed.
if (properties instanceof TEntityTemplate.XmlProperties) {
@Nullable final Object any = ((TEntityTemplate.XmlProperties) properties).getAny();
if (any == null) {
LOGGER.debug("XML properties expected, but none found. Returning empty JSON.");
return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
}
try {
@ADR(6) String // String xmlAsString = BackendUtils.getXMLAsString(TEntityTemplate.XmlProperties.class, (TEntityTemplate.XmlProperties)props, true, requestRepository);
xmlAsString = BackendUtils.getXMLAsString(properties, requestRepository);
return Response.ok().entity(xmlAsString).type(MediaType.TEXT_XML).build();
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
// CASE "NULL": Return empty JSON if no properties existed.
if (properties == null) {
return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
}
// OTHERWISE: Throw error if properties could not be handled. Should never happen.
LOGGER.error("Property definition for Entity Template {} was not handled", template.getId());
return Response.serverError().build();
}
Aggregations