use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.
the class BackendUtils method deriveWPD.
/**
* Derives Winery's Properties Definition from an existing properties definition
*
* @param ci the entity type to try to modify the WPDs
* @param errors the list to add errors to
*/
// FIXME this is specifically for xml backends and therefore broken under the new canonical model
public static void deriveWPD(TEntityType ci, List<String> errors, IRepository repository) {
BackendUtils.LOGGER.trace("deriveWPD");
TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
if (propertiesDefinition == null) {
// if there's no properties definition, there's nothing to derive because we're in YAML mode
return;
}
if (!(propertiesDefinition instanceof TEntityType.XmlElementDefinition)) {
BackendUtils.LOGGER.debug("only works for an element definition, not for types");
return;
}
final QName element = ((TEntityType.XmlElementDefinition) propertiesDefinition).getElement();
BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
// fetch the XSD defining the element
final XsdImportManager xsdImportManager = repository.getXsdImportManager();
Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
if (ref == null) {
String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref, repository);
if (!xsModelOptional.isPresent()) {
LOGGER.error("no XSModel found");
}
XSModel xsModel = xsModelOptional.get();
XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
if (elementDeclaration == null) {
String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
// go through the XSD definition and
XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
if (!(typeDefinition instanceof XSComplexTypeDefinition)) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
return;
}
XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
XSParticle particle = cTypeDefinition.getParticle();
if (particle == null) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
return;
}
XSTerm term = particle.getTerm();
if (!(term instanceof XSModelGroup)) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
return;
}
XSModelGroup modelGroup = (XSModelGroup) term;
if (modelGroup.getCompositor() != XSModelGroup.COMPOSITOR_SEQUENCE) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
return;
}
XSObjectList particles = modelGroup.getParticles();
int len = particles.getLength();
boolean everyThingIsASimpleType = true;
List<PropertyDefinitionKV> list = new ArrayList<>();
if (len != 0) {
for (int i = 0; i < len; i++) {
XSParticle innerParticle = (XSParticle) particles.item(i);
XSTerm innerTerm = innerParticle.getTerm();
if (innerTerm instanceof XSElementDeclaration) {
XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
String name = innerElementDeclaration.getName();
XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
if (innerTypeDefinition instanceof XSSimpleType) {
XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
String typeNS = xsSimpleType.getNamespace();
String typeName = xsSimpleType.getName();
if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
PropertyDefinitionKV def = new PropertyDefinitionKV();
def.setKey(name);
// convention at WPD: use "xsd" as prefix for XML Schema Definition
def.setType("xsd:" + typeName);
list.add(def);
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
}
}
if (!everyThingIsASimpleType) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
return;
}
// everything went alright, we can add a WPD
WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
wpd.setIsDerivedFromXSD(Boolean.TRUE);
wpd.setElementName(element.getLocalPart());
wpd.setNamespace(element.getNamespaceURI());
wpd.setPropertyDefinitions(list);
ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
BackendUtils.LOGGER.debug("Successfully generated WPD");
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.
the class ConsistencyChecker method checkServiceTemplate.
private void checkServiceTemplate(ServiceTemplateId id) {
TServiceTemplate serviceTemplate;
try {
serviceTemplate = configuration.getRepository().getElement(id);
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
}
if (serviceTemplate.getTopologyTemplate() == null) {
return;
}
@NonNull final List<TNodeTemplate> nodeTemplates = serviceTemplate.getTopologyTemplate().getNodeTemplates();
for (TNodeTemplate nodeTemplate : nodeTemplates) {
NodeTypeId nodeTypeId = new NodeTypeId(nodeTemplate.getType());
TNodeType nodeType;
try {
nodeType = configuration.getRepository().getElement(nodeTypeId);
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during reading of id {}", nodeTypeId.toReadableString(), e);
printAndAddError(nodeTypeId, "Reading error " + e.getMessage());
return;
}
final WinerysPropertiesDefinition winerysPropertiesDefinition = nodeType.getWinerysPropertiesDefinition();
if (winerysPropertiesDefinition != null) {
List<PropertyDefinitionKV> list = winerysPropertiesDefinition.getPropertyDefinitions();
if (list != null) {
// iterate on all defined properties
for (PropertyDefinitionKV propdef : list) {
String key = propdef.getKey();
if (key == null) {
printAndAddError(id, "key is null");
continue;
}
// assign value, but change "null" to "" if no property is defined
final Map<String, String> propertiesKV = ModelUtilities.getPropertiesKV(nodeTemplate);
if (propertiesKV == null) {
printAndAddError(id, "propertiesKV of node template " + nodeTemplate.getId() + " is null");
}
}
}
}
}
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.
the class PropertiesDefinitionResource method getInheritedPropertiesDefinitionResource.
/**
* Returns a list of properties definition of each parent.
*
* Only self-defined properties definition are considered.
* Properties definition of a parent's parent are not merged into the properties definition of the parent.
*/
@GET
@Path("inherited")
@Produces(MediaType.APPLICATION_JSON)
public List<InheritedPropertiesDefinitionsResourceApiData> getInheritedPropertiesDefinitionResource() {
ArrayList<TEntityType> parents = RepositoryFactory.getRepository().getParents(this.parentRes.getEntityType());
ArrayList<InheritedPropertiesDefinitionsResourceApiData> list = new ArrayList<>();
for (TEntityType parent : parents) {
// Add winerys properties definition of parent if defined to list
WinerysPropertiesDefinition winerysPropertiesDefinition = parent.getWinerysPropertiesDefinition();
if (winerysPropertiesDefinition != null) {
// Add derived from information
List<PropertyDefinitionKV> propertyDefinitions = winerysPropertiesDefinition.getPropertyDefinitions();
if (propertyDefinitions != null) {
for (PropertyDefinitionKV propertyDefinition : propertyDefinitions) {
propertyDefinition.setDerivedFromType(parent.getQName());
propertyDefinition.setDerivedFromStatus("SELF");
}
}
// Add winerys properties definition to list
PropertiesDefinitionResourceApiData propertiesDefinitionResourceApiData = new PropertiesDefinitionResourceApiData(parent.getProperties(), winerysPropertiesDefinition);
list.add(new InheritedPropertiesDefinitionsResourceApiData(parent.getQName(), propertiesDefinitionResourceApiData));
}
}
return list;
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.
the class PropertiesDefinitionResource method getMerged.
/**
* Merge properties definitions with inherited definitions.
*/
@GET
@Path("merged")
@Produces(MediaType.APPLICATION_JSON)
public PropertiesDefinitionResourceApiData getMerged() {
// Get complete inheritance hierarchy
List<TEntityType> hierarchy = RepositoryFactory.getRepository().getParentsAndChild(this.getEntityType());
// Merge properties definitions
List<PropertyDefinitionKV> propertyDefinitions = ModelUtilities.mergePropertiesDefinitions(hierarchy);
// Create new WPD
WinerysPropertiesDefinition winerysPropertiesDefinition = new WinerysPropertiesDefinition();
winerysPropertiesDefinition.setElementName(this.getEntityType().getName());
winerysPropertiesDefinition.setNamespace(this.getEntityType().getTargetNamespace());
winerysPropertiesDefinition.setPropertyDefinitions(propertyDefinitions);
return new PropertiesDefinitionResourceApiData(this.getEntityType().getProperties(), winerysPropertiesDefinition);
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV in project winery by eclipse.
the class PropertiesDefinitionResource method onJsonPost.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(PropertiesDefinitionResourceApiData data) {
// CASE: XML
if (data.selectedValue == PropertiesDefinitionEnum.Element || data.selectedValue == PropertiesDefinitionEnum.Type) {
// first of all, remove Winery's Properties definition (if it exists)
ModelUtilities.removeWinerysPropertiesDefinition(this.getEntityType());
// FIXME need to actually handle propertiesData properly!
if (data.propertiesDefinition == null) {
return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
}
this.getEntityType().setProperties(data.propertiesDefinition);
List<String> errors = new ArrayList<>();
BackendUtils.deriveWPD(this.getEntityType(), errors, RepositoryFactory.getRepository());
// currently the errors are just logged
for (String error : errors) {
PropertiesDefinitionResource.LOGGER.debug(error);
}
return RestUtils.persist(this.parentRes);
}
// Only definitions are stored which are not defined by any parent
if (data.selectedValue == PropertiesDefinitionEnum.Custom) {
ArrayList<TEntityType> parents = RepositoryFactory.getRepository().getParents(this.parentRes.getEntityType());
// Get all definitions defined by any parent
List<PropertyDefinitionKV> parentsPropertiesDefinitions = new ArrayList<>();
for (TEntityType parent : parents) {
WinerysPropertiesDefinition winerysPropertiesDefinition = parent.getWinerysPropertiesDefinition();
if (winerysPropertiesDefinition != null) {
parentsPropertiesDefinitions.addAll(winerysPropertiesDefinition.getPropertyDefinitions());
}
}
// Get only definitions that are not defined by any parent
List<PropertyDefinitionKV> definitions = new ArrayList<>();
for (PropertyDefinitionKV definition : data.winerysPropertiesDefinition.getPropertyDefinitions()) {
boolean exists = false;
for (PropertyDefinitionKV currentDefinition : parentsPropertiesDefinitions) {
if (definition.equals(currentDefinition)) {
exists = true;
break;
}
}
if (!exists) {
definitions.add(definition);
}
}
// Update and store definitions
data.winerysPropertiesDefinition.setPropertyDefinitions(definitions);
this.getEntityType().setProperties(data.winerysPropertiesDefinition);
return RestUtils.persist(this.parentRes);
}
// CASE: YAML
if (data.selectedValue == PropertiesDefinitionEnum.Yaml) {
TEntityType entityType = this.parentRes.getEntityType();
if (!(data.propertiesDefinition instanceof TEntityType.YamlPropertiesDefinition)) {
return Response.status(Status.BAD_REQUEST).entity("Expected YamlPropertiesDefinition element").build();
}
entityType.setProperties(data.propertiesDefinition);
return RestUtils.persist(this.parentRes);
}
// OTHERWISE: throw error
return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
}
Aggregations