use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition 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.WinerysPropertiesDefinition 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.WinerysPropertiesDefinition 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.WinerysPropertiesDefinition 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();
}
use of org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition in project winery by eclipse.
the class PropertiesDefinitionDeserializer method deserialize.
@Override
public TEntityType.PropertiesDefinition deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
// read as ObjectNode to enable removing empty properties
ObjectNode node = parser.getCodec().readTree(parser);
final JavaType targetType;
if (node.hasNonNull("propertyDefinitionKVList")) {
// deserialize as WinerysPropertiesDefinition
targetType = context.constructType(WinerysPropertiesDefinition.class);
} else if (node.hasNonNull("element")) {
targetType = context.constructType(TEntityType.XmlElementDefinition.class);
// remove unused properties to avoid tripping up the Json Parsing
node.remove("type");
node.remove("properties");
} else if (node.hasNonNull("type")) {
targetType = context.constructType(TEntityType.XmlTypeDefinition.class);
// remove unused properties to avoid tripping up the Json Parsing
node.remove("element");
node.remove("properties");
} else if (node.hasNonNull("properties")) {
targetType = context.constructType(TEntityType.YamlPropertiesDefinition.class);
// remove unused properties to avoid tripping up the Json Parsing
node.remove("type");
node.remove("element");
} else {
// must not throw an exception because PropertiesDefinitionAPIData can contain a null element
return null;
}
JsonDeserializer<Object> deserializer = context.findNonContextualValueDeserializer(targetType);
// create a new JsonParser for the delegate deserializer to account for consumed input in original parser.
JsonParser objectParser = node.traverse();
// advance the parser by one token because the parser initialized by node.traverse() stays at "before start"
objectParser.nextToken();
return (TEntityType.PropertiesDefinition) deserializer.deserialize(objectParser, context);
}
Aggregations