Fixing update properties

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134816 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-25 14:44:21 +00:00
parent eb2c8022ba
commit e4cbaf19d4
2 changed files with 132 additions and 76 deletions

View File

@ -52,6 +52,7 @@ import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
@ -62,6 +63,52 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertex;
*/
public class EntityManagementImpl implements EntityManagement {
public static final Set<String> RELATION_IGNORE_KEYS;
public static final Set<String> RELATION_IGNORE_START_WITH_KEYS;
public static final Set<String> ENTITY_IGNORE_KEYS;
public static final Set<String> ENTITY_IGNORE_START_WITH_KEYS;
public static final Set<String> EMBEDDED_IGNORE_KEYS;
public static final Set<String> EMBEDDED_IGNORE_START_WITH_KEYS;
public static final String AT = "@";
public static final String UNDERSCORE = "_";
static {
RELATION_IGNORE_KEYS = new HashSet<String>();
RELATION_IGNORE_KEYS.add(Relation.HEADER_PROPERTY);
RELATION_IGNORE_KEYS.add(Relation.TARGET_PROPERTY);
RELATION_IGNORE_KEYS.add(Relation.SOURCE_PROPERTY);
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_IN.toLowerCase());
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_OUT.toLowerCase());
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_IN.toUpperCase());
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase());
RELATION_IGNORE_START_WITH_KEYS = new HashSet<String>();
RELATION_IGNORE_START_WITH_KEYS.add(AT);
RELATION_IGNORE_START_WITH_KEYS.add(UNDERSCORE);
ENTITY_IGNORE_KEYS = new HashSet<String>();
ENTITY_IGNORE_KEYS.add(Entity.HEADER_PROPERTY);
ENTITY_IGNORE_START_WITH_KEYS = new HashSet<String>();
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_IN_PREFIX.toLowerCase());
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_OUT_PREFIX.toLowerCase());
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_IN_PREFIX.toUpperCase());
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_OUT_PREFIX.toUpperCase());
ENTITY_IGNORE_START_WITH_KEYS.add(AT);
ENTITY_IGNORE_START_WITH_KEYS.add(UNDERSCORE);
EMBEDDED_IGNORE_KEYS = new HashSet<String>();
EMBEDDED_IGNORE_START_WITH_KEYS = new HashSet<String>();
ENTITY_IGNORE_START_WITH_KEYS.add(AT);
ENTITY_IGNORE_START_WITH_KEYS.add(UNDERSCORE);
}
private static Logger logger = LoggerFactory
.getLogger(EntityManagementImpl.class);
@ -210,7 +257,7 @@ public class EntityManagementImpl implements EntityManagement {
throw new ResourceRegistryException(
"An embedded object cannot have an Header");
}
ODocument oDocument = new ODocument(type);
return oDocument.fromJSON(jsonNode.toString());
@ -277,29 +324,36 @@ public class EntityManagementImpl implements EntityManagement {
}
public static Map<String, Object> getPropertyMap(JsonNode jsonNode,
Set<String> ignoreKeys) throws JsonProcessingException, IOException {
Set<String> ignoreKeys, Set<String> ignoreStartWith) throws JsonProcessingException, IOException {
Map<String, Object> map = new HashMap<>();
if (ignoreKeys == null) {
ignoreKeys = new HashSet<>();
}
if (ignoreStartWith == null) {
ignoreStartWith = new HashSet<>();
}
Iterator<Entry<String, JsonNode>> fields = jsonNode.fields();
OUTER_WHILE:
while (fields.hasNext()) {
Entry<String, JsonNode> entry = fields.next();
String key = entry.getKey();
if (key.compareTo(Entity.HEADER_PROPERTY) == 0) {
continue;
}
if (key.startsWith("@") || key.startsWith("_")) {
continue;
}
if (ignoreKeys.contains(key)) {
continue;
}
for(String prefix : ignoreStartWith){
if(key.startsWith(prefix)){
break OUTER_WHILE;
}
}
JsonNode value = entry.getValue();
Object object = null;
try {
@ -318,10 +372,9 @@ public class EntityManagementImpl implements EntityManagement {
private Map<String, Object> getVertexProperties(JsonNode node)
throws ResourceRegistryException {
Set<String> ignoreKeys = new HashSet<>();
Map<String, Object> vertexProperties = null;
try {
vertexProperties = getPropertyMap(node, ignoreKeys);
vertexProperties = getPropertyMap(node, ENTITY_IGNORE_KEYS, ENTITY_IGNORE_START_WITH_KEYS);
} catch (Exception e) {
String error = "Error while parsing json to get Relation properties";
logger.error(error, e);
@ -332,14 +385,9 @@ public class EntityManagementImpl implements EntityManagement {
private Map<String, Object> getEdgeProperties(JsonNode node)
throws ResourceRegistryException {
Set<String> ignoreKeys = new HashSet<>();
ignoreKeys.add(Relation.TARGET_PROPERTY);
ignoreKeys.add(Relation.SOURCE_PROPERTY);
ignoreKeys.add(Relation.HEADER_PROPERTY);
Map<String, Object> edgeProperties = null;
try {
edgeProperties = getPropertyMap(node, ignoreKeys);
edgeProperties = getPropertyMap(node, RELATION_IGNORE_KEYS, RELATION_IGNORE_START_WITH_KEYS);
} catch (Exception e) {
String error = "Error while parsing json to get Relation properties";
logger.error(error, e);
@ -348,6 +396,62 @@ public class EntityManagementImpl implements EntityManagement {
return edgeProperties;
}
private Element updateProperties(Element element, JsonNode jsonNode)
throws ResourceRegistryException {
Set<String> ignoreKeys = null;
Set<String> ignoreStartWithKeys = null;
Set<String> oldKeys = element.getPropertyKeys();
Map<String, Object> properties;
if (element instanceof Vertex) {
properties = getVertexProperties(jsonNode);
ignoreKeys = EMBEDDED_IGNORE_KEYS;
ignoreStartWithKeys = EMBEDDED_IGNORE_START_WITH_KEYS;
} else if (element instanceof Edge) {
properties = getEdgeProperties(jsonNode);
ignoreKeys = RELATION_IGNORE_KEYS;
ignoreStartWithKeys = RELATION_IGNORE_START_WITH_KEYS;
} else {
String error = String.format("Error while updating {} properties",
element.toString());
throw new ResourceRegistryException(error);
}
oldKeys.removeAll(properties.keySet());
for (String key : properties.keySet()) {
try {
element.setProperty(key, properties.get(key));
} catch (Exception e) {
String error = String.format(
"Error while setting property %s : %s", key, properties
.get(key).toString());
logger.error(error);
throw new ResourceRegistryException(error, e);
}
}
OUTER_FOR:
for (String key : oldKeys) {
if (ignoreKeys.contains(key)) {
continue;
}
for(String prefix : ignoreStartWithKeys){
if(key.startsWith(prefix)){
break OUTER_FOR;
}
}
element.removeProperty(key);
}
return element;
}
private Vertex getOrCreateTargetVertex(OrientGraph orientGraph,
@SuppressWarnings("rawtypes") Class<? extends Relation> relation,
JsonNode target) throws ResourceRegistryException {
@ -745,48 +849,6 @@ public class EntityManagementImpl implements EntityManagement {
}
}
private Element updateProperties(Element element, JsonNode jsonNode)
throws ResourceRegistryException {
Set<String> oldKeys = element.getPropertyKeys();
Map<String, Object> properties;
if (element instanceof Vertex) {
properties = getVertexProperties(jsonNode);
} else if (element instanceof Edge) {
properties = getEdgeProperties(jsonNode);
} else {
String error = String.format("Error while updating {} properties",
element.toString());
throw new ResourceRegistryException(error);
}
oldKeys.removeAll(properties.keySet());
for (String key : properties.keySet()) {
try {
element.setProperty(key, properties.get(key));
} catch (Exception e) {
String error = String.format(
"Error while setting property %s : %s", key, properties
.get(key).toString());
logger.error(error);
throw new ResourceRegistryException(error, e);
}
}
for (String key : oldKeys) {
if (key.startsWith("@") || key.startsWith("_")) {
continue;
}
if (key.compareTo(Facet.HEADER_PROPERTY) == 0) {
continue;
}
element.removeProperty(key);
}
return element;
}
protected Vertex updateFacet(OrientGraph orientGraph, UUID uuid,
JsonNode jsonNode) throws ResourceRegistryException {
logger.debug("Trying to update {} with UUID {} usign {}", Facet.NAME,

View File

@ -9,6 +9,8 @@ import java.io.FileReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Paths;
@ -25,8 +27,13 @@ import org.gcube.informationsystem.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.ContainerStateFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.MemoryFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.NetworkingFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SimplePropertyFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl;
import org.gcube.informationsystem.impl.entity.resource.HostingNodeImpl;
import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl;
import org.gcube.informationsystem.impl.relation.consistsof.HasPersistentMemoryImpl;
import org.gcube.informationsystem.impl.relation.consistsof.HasVolatileMemoryImpl;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Facet;
@ -35,10 +42,14 @@ import org.gcube.informationsystem.model.entity.facet.CPUFacet;
import org.gcube.informationsystem.model.entity.facet.ContainerStateFacet;
import org.gcube.informationsystem.model.entity.facet.MemoryFacet;
import org.gcube.informationsystem.model.entity.facet.MemoryFacet.MemoryUnit;
import org.gcube.informationsystem.model.entity.facet.NetworkingFacet;
import org.gcube.informationsystem.model.entity.facet.SimplePropertyFacet;
import org.gcube.informationsystem.model.entity.facet.SoftwareFacet;
import org.gcube.informationsystem.model.entity.resource.EService;
import org.gcube.informationsystem.model.entity.resource.HostingNode;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.model.relation.consistsof.HasPersistentMemory;
import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.junit.Test;
@ -107,7 +118,6 @@ public class SmartgearResourcesTest {
Header header = new HeaderImpl(uuid);
hostingNode.setHeader(header);
/*
NetworkingFacet networkingFacet = new NetworkingFacetImpl();
try {
networkingFacet.setIPAddress(InetAddress.getLocalHost().getHostAddress());
@ -138,37 +148,28 @@ public class SmartgearResourcesTest {
SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables();
hostingNode.addFacet(simplePropertyFacet);
*/
ContainerStateFacet containerStateFacet = getContainerStateFacet(null);
hostingNode.addFacet(containerStateFacet);
/*
MemoryFacet ramFacet = getRamInfo(null);
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileRAMMemory =
new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
hostingNode, ramFacet, null);
hasVolatileRAMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_RAM);
hostingNode.addFacet(hasVolatileRAMMemory);
*/
/*
MemoryFacet jvmMemoryFacet = getJVMMemoryInfo(null);
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileJVMMemory =
new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
hostingNode, jvmMemoryFacet, null);
hasVolatileJVMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_JVM);
hostingNode.addFacet(hasVolatileJVMMemory);
*/
/*
MemoryFacet disk = getDiskSpace(null);
HasPersistentMemory<HostingNode, MemoryFacet> hasPersistentMemory =
new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(hostingNode, disk, null);
hostingNode.addFacet(hasPersistentMemory);
*/
String json = entityManagementImpl.createResource(HostingNode.NAME, Entities.marshal(hostingNode));
@ -179,40 +180,33 @@ public class SmartgearResourcesTest {
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = hostingNodeToUpdate.getConsistsOf();
for(ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList){
/*
if(c.getTarget() instanceof ContainerStateFacet){
containerStateFacet = (ContainerStateFacet) c.getTarget();
containerStateFacet = getContainerStateFacet(containerStateFacet);
continue;
}
*/
if(c instanceof HasVolatileMemory){
/*
String memoryType = (String) c.getAdditionalProperty(MEMORY_TYPE);
if(memoryType.compareTo(MEMORY_TYPE_RAM)==0){
ramFacet = (MemoryFacet) c.getTarget();
ramFacet = getRamInfo(ramFacet);
continue;
}
*/
/*
if(memoryType.compareTo(MEMORY_TYPE_JVM)==0){
jvmMemoryFacet = (MemoryFacet) c.getTarget();
jvmMemoryFacet = getJVMMemoryInfo(jvmMemoryFacet);
continue;
}
*/
}
/*
if(c instanceof HasPersistentMemory){
disk = (MemoryFacet) c.getTarget();
disk = getDiskSpace(disk);
continue;
}
*/
consistsOfToRemove.add(c);