From 00bf76b9b85e8ec9286f94ed7e5467cb14c26dca Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 28 Nov 2016 11:49:27 +0000 Subject: [PATCH] Added missing updateResource API to version 1.1.0-SNAPSHOT git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@134954 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 2 +- .../proxy/ResourceRegistryPublisher.java | 4 +- .../proxy/ResourceRegistryPublisherImpl.java | 29 ++ .../publisher/SmartgearResourcesTest.java | 379 ++++++++++++++++++ 4 files changed, 412 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b1d05b5..f36629b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.gcube.information-system resource-registry-publisher - 1.2.0-SNAPSHOT + 1.1.0-SNAPSHOT Resource Registry Publisher Contains Non Idempotent API for Resource Registry diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java index 3ce8420..b50a42e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java @@ -24,7 +24,9 @@ public interface ResourceRegistryPublisher { public R createResource(Class resourceClass, R resource); - + + public R updateResource(Class resourceClass, R resource); + public boolean deleteResource(R resource); public > C createConsistsOf( diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java index a51064a..c120706 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java @@ -396,6 +396,35 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher throw new ServiceException(e); } } + + @Override + public R updateResource(Class resourceClass, R resource) { + try { + logger.info("Going to update: {}", resource); + StringWriter stringWriter = new StringWriter(); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.ENTITY_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.RESOURCE_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(resource.getHeader().getUUID().toString()); + + String body = Entities.marshal(resource); + + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPMETHOD.POST, null, body); + + ResourceRegistryCall call = new ResourceRegistryCall<>( + resourceClass, httpInputs); + + R r = delegate.make(call); + logger.info("{} update created", r); + return r; + } catch (Exception e) { + logger.error("Error Creating {}", resource, e); + throw new ServiceException(e); + } + } @Override public boolean deleteResource(R resource) { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java index 8c5311a..4ee6ddf 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java @@ -3,11 +3,54 @@ */ package org.gcube.informationsystem.resourceregistry.publisher; +import java.io.BufferedReader; +import java.io.File; +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; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.gcube.common.scope.api.ScopeProvider; +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; +import org.gcube.informationsystem.model.entity.Resource; +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.NetworkingFacet; +import org.gcube.informationsystem.model.entity.facet.SimplePropertyFacet; +import org.gcube.informationsystem.model.entity.facet.SoftwareFacet; +import org.gcube.informationsystem.model.entity.facet.MemoryFacet.MemoryUnit; 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.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisher; import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisherFactory; @@ -60,4 +103,340 @@ public class SmartgearResourcesTest { logger.debug("Created : {}", eService); } + + public static final String MEMORY_TYPE = "memoryType"; + public static final String MEMORY_TYPE_RAM = "RAM"; + public static final String MEMORY_TYPE_JVM = "JVM"; + public static final String JVM_MAX_MEMORY = "jvmMaxMemory"; + + @Test + public void testHostingNodeOperations() throws ResourceRegistryException, IOException{ + ScopeProvider.instance.set("/gcube/devNext"); + + UUID uuid = UUID.randomUUID(); + + HostingNode hostingNode = new HostingNodeImpl(); + Header header = new HeaderImpl(uuid); + hostingNode.setHeader(header); + + NetworkingFacet networkingFacet = new NetworkingFacetImpl(); + try { + networkingFacet.setIPAddress(InetAddress.getLocalHost().getHostAddress()); + } catch (UnknownHostException e) { + logger.warn("unable to detect the IP address of the host"); + } + networkingFacet.setHostName("pc-frosini.isti.cnr.it"); + networkingFacet.setDomainName(getDomain(networkingFacet.getHostName())); + + networkingFacet.setAdditionalProperty("Port", 8080); + IsIdentifiedBy isIdentifiedBy = + new IsIdentifiedByImpl<>(hostingNode, networkingFacet, null); + hostingNode.addFacet(isIdentifiedBy); + + List cpuFacets = getCPUFacets(); + for (CPUFacet cpuFacet: cpuFacets) { + hostingNode.addFacet(cpuFacet); + } + + + SoftwareFacet softwareFacet = new SoftwareFacetImpl(); + OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean(); + softwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name")); + softwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch")); + softwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version")); + hostingNode.addFacet(softwareFacet); + + + SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables(); + hostingNode.addFacet(simplePropertyFacet); + + ContainerStateFacet containerStateFacet = getContainerStateFacet(null); + hostingNode.addFacet(containerStateFacet); + + + MemoryFacet ramFacet = getRamInfo(null); + HasVolatileMemory hasVolatileRAMMemory = + new HasVolatileMemoryImpl( + hostingNode, ramFacet, null); + hasVolatileRAMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_RAM); + hostingNode.addFacet(hasVolatileRAMMemory); + + MemoryFacet jvmMemoryFacet = getJVMMemoryInfo(null); + HasVolatileMemory hasVolatileJVMMemory = + new HasVolatileMemoryImpl( + hostingNode, jvmMemoryFacet, null); + hasVolatileJVMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_JVM); + hostingNode.addFacet(hasVolatileJVMMemory); + + MemoryFacet disk = getDiskSpace(null); + HasPersistentMemory hasPersistentMemory = + new HasPersistentMemoryImpl(hostingNode, disk, null); + hostingNode.addFacet(hasPersistentMemory); + + HostingNode hostingNodeToUpdate = resourceRegistryPublisher.createResource(HostingNode.class, hostingNode); + + List> consistsOfToRemove = new ArrayList<>(); + + List> consistsOfList = hostingNodeToUpdate.getConsistsOf(); + for(ConsistsOf 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); + + } + + consistsOfList.removeAll(consistsOfToRemove); + + HostingNode hostingNodeToUpdated = resourceRegistryPublisher.updateResource(HostingNode.class, hostingNodeToUpdate); + logger.debug("Upodate Hosting Node {}", hostingNodeToUpdated); + + } + + + + private ContainerStateFacet getContainerStateFacet(ContainerStateFacet containerStateFacet){ + if(containerStateFacet == null){ + containerStateFacet = new ContainerStateFacetImpl(); + } + containerStateFacet.setValue("ready"); + return containerStateFacet; + } + + public static final String MESSAGE = "message"; + + private MemoryFacet getDiskSpace(MemoryFacet memoryFacet){ + if(memoryFacet == null){ + memoryFacet = new MemoryFacetImpl(); + } + + long free = 0; + long total = 0; + try { + FileStore fileStore = Files.getFileStore(Paths.get("./")); + free = fileStore.getUsableSpace() / 1048576; // 1048576 = 1024*1024 user to convert bytes in MByte + total = fileStore.getTotalSpace() / 1048576; // 1048576 = 1024*1024 user to convert bytes in MByte + } catch (IOException ioe) { + logger.warn("Unable to detect disk space information", ioe); + memoryFacet.setAdditionalProperty(MESSAGE, "Unable to detect disk space information."); + } + + memoryFacet.setUnit(MemoryUnit.MB); + memoryFacet.setSize(total); + memoryFacet.setUsed(total-free); + + return memoryFacet; + } + + @SuppressWarnings("restriction") + private MemoryFacet getRamInfo(MemoryFacet memoryFacet) { + if(memoryFacet == null){ + memoryFacet = new MemoryFacetImpl(); + } + + OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean(); + com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean; + long freeMemory = sunmxbean.getFreePhysicalMemorySize() / 1048576; // in MB + long totalMemory = sunmxbean.getTotalPhysicalMemorySize() / 1048576; // in MB + + memoryFacet.setUnit(MemoryUnit.MB); + memoryFacet.setSize(totalMemory); + memoryFacet.setUsed(totalMemory-freeMemory); + + return memoryFacet; + } + + + private MemoryFacet getJVMMemoryInfo(MemoryFacet memoryFacet) { + if(memoryFacet == null){ + memoryFacet = new MemoryFacetImpl(); + } + + long jvmFreeMemory = Runtime.getRuntime().freeMemory() / 1048576; // 1048576 = 1024*1024 user to convert bytes in MByte + long jvmTotalMemory = Runtime.getRuntime().totalMemory() / 1048576; // 1048576 = 1024*1024 user to convert bytes in MByte + long jvmMaxMemory = Runtime.getRuntime().maxMemory() / 1048576; // 1048576 = 1024*1024 user to convert bytes in MByte + + memoryFacet.setUnit(MemoryUnit.MB); + memoryFacet.setSize(jvmTotalMemory); + memoryFacet.setUsed(jvmTotalMemory-jvmFreeMemory); + memoryFacet.setAdditionalProperty(JVM_MAX_MEMORY, jvmMaxMemory); + + return memoryFacet; + } + + + private static String sanitizeKey(String key){ + return key.trim().replace(" ", "_"); + } + + private SimplePropertyFacet addEnvironmentVariables() { + + + Map map = new HashMap(); + map.putAll(System.getenv()); + + SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); + simplePropertyFacet.setName("ENVIRONMENT_VARIABLES"); + simplePropertyFacet.setValue(""); + + for (Map.Entry entry : map.entrySet()) { + String varname = entry.getKey(); + if ((varname.compareToIgnoreCase("CLASSPATH") == 0) || + (varname.compareToIgnoreCase("PATH") == 0) || + (varname.contains("SSH")) || + (varname.contains("MAIL")) || + (varname.compareToIgnoreCase("LS_COLORS") == 0)) { + continue; + } + + simplePropertyFacet.setAdditionalProperty(sanitizeKey(entry.getKey()), entry.getValue()); + + } + + + simplePropertyFacet.setAdditionalProperty("Java", System.getProperty("java.version")); + + return simplePropertyFacet; + } + + + private static String getDomain(String hostname) { + try { + Pattern pattern = Pattern.compile("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})"); + Matcher regexMatcher = pattern.matcher(hostname); + if (regexMatcher.matches()) { //it's an IP address, nothing to trim + return hostname; + } + return hostname.substring(hostname.indexOf(".")+1); + }catch(Exception e){ + logger.warn("Error while getting domain from hostname"); + return hostname; + } + } + + + public static final String CPU_PROCESSOR = "processor"; + public static final String CPU_VENDOR_ID = "vendor_id"; + public static final String CPU_MODEL_NAME = "model name"; + public static final String CPU_CPU_MHZ = "cpu MHz"; + public static final String CPU_MODEL_T = "model\t"; + public static final String CPU_MODEL_B = "model\b"; + public static final String CPU_MODEL_NUMBER = "modelNumber"; + + public static List getCPUFacets() { + + List cpuFacets = new ArrayList<>(); + + File file = new File("/proc/cpuinfo"); + + if (!file.exists()) { + logger.warn("cannot acquire CPU info (no /proc/cpuinfo)"); + return cpuFacets; + } + + BufferedReader input = null; + + try { + input = new BufferedReader(new FileReader(file)); + + String line = null; + + CPUFacet cpuFacet = null; + + while ((line = input.readLine()) != null) { + + if ((line.startsWith(CPU_PROCESSOR))) { // add the current processor to the map + cpuFacet = new CPUFacetImpl(); + cpuFacets.add(cpuFacet); + } + + try { + if (line.contains(CPU_VENDOR_ID)) { + cpuFacet.setVendor(line.split(":")[1].trim()); + continue; + } + } catch (Exception e) { + continue; + } + + + try { + if (line.contains(CPU_MODEL_NAME)){ + cpuFacet.setModel(line.split(":")[1].trim()); + continue; + } + } catch (Exception e){ + continue; + } + + try { + if (line.contains(CPU_CPU_MHZ)) { + cpuFacet.setClockSpeed(line.split(":")[1].trim()); + continue; + } + } catch (Exception e) { + continue; + } + + + try { + if ((line.contains(CPU_MODEL_T)) || (line.contains(CPU_MODEL_B))){ + cpuFacet.setAdditionalProperty(CPU_MODEL_NUMBER, line.split(":")[1].trim()); + continue; + } + } catch (Exception e) { + continue; + } + + + + try { + String[] nameValue = line.split(":"); + cpuFacet.setAdditionalProperty(sanitizeKey(nameValue[0]), line.split(":")[1].trim()); + } catch (Exception e) { + + } + + } + } catch (Exception e) { + logger.warn("unable to acquire CPU info", e); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + logger.warn("unable to close stream", e); + } + } + } + return cpuFacets; + } + }