Ori Liel has uploaded a new change for review.

Change subject: restapi: Display VM session infromation (WIP)
......................................................................

restapi: Display VM session infromation (WIP)

Change-Id: Icb6488ea7118826adb169c9d1388fab7ab2792d8
Signed-off-by: Ori Liel <[email protected]>
---
M 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
A 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionResource.java
A 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionsResource.java
M 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
M 
backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/AbstractBackendResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
A 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionResource.java
A 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionsResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BaseBackendResource.java
M 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/HostMapper.java
M 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/VmMapper.java
M 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/GuidUtils.java
M 
backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/VmMapperTest.java
14 files changed, 359 insertions(+), 44 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/91/25591/1

diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
index c010b9c..783c88d 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
@@ -141,4 +141,8 @@
 
     @Path("reporteddevices")
     public VmReportedDevicesResource getVmReportedDevicesResource();
+
+    @Path("sessions")
+    public VmSessionsResource getVmSessionsResource();
+
 }
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionResource.java
new file mode 100644
index 0000000..f3714c1
--- /dev/null
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionResource.java
@@ -0,0 +1,16 @@
+package org.ovirt.engine.api.resource;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Produces;
+
+import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
+import org.ovirt.engine.api.model.Session;
+
+@Produces({ ApiMediaType.APPLICATION_XML, ApiMediaType.APPLICATION_JSON, 
ApiMediaType.APPLICATION_X_YAML })
+public interface VmSessionResource {
+
+    @GET
+    @Formatted
+    public Session get();
+
+}
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionsResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionsResource.java
new file mode 100644
index 0000000..944574a
--- /dev/null
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmSessionsResource.java
@@ -0,0 +1,22 @@
+package org.ovirt.engine.api.resource;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+
+import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
+import org.ovirt.engine.api.model.Sessions;
+
+@Produces({ ApiMediaType.APPLICATION_XML, ApiMediaType.APPLICATION_JSON, 
ApiMediaType.APPLICATION_X_YAML })
+public interface VmSessionsResource {
+
+    @GET
+    @Formatted
+    public Sessions list();
+
+    @Path("{iden}")
+    public VmSessionResource getSessionSubResource(@PathParam("iden") String 
id);
+
+
+}
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
index 519d2ae..e9ff142 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
@@ -62,6 +62,7 @@
 import org.ovirt.engine.api.model.ReportedDevice;
 import org.ovirt.engine.api.model.Request;
 import org.ovirt.engine.api.model.Role;
+import org.ovirt.engine.api.model.Session;
 import org.ovirt.engine.api.model.Snapshot;
 import org.ovirt.engine.api.model.Statistic;
 import org.ovirt.engine.api.model.Step;
@@ -166,6 +167,8 @@
 import org.ovirt.engine.api.resource.VmReportedDeviceResource;
 import org.ovirt.engine.api.resource.VmReportedDevicesResource;
 import org.ovirt.engine.api.resource.VmResource;
+import org.ovirt.engine.api.resource.VmSessionResource;
+import org.ovirt.engine.api.resource.VmSessionsResource;
 import org.ovirt.engine.api.resource.VmsResource;
 import org.ovirt.engine.api.resource.VnicProfileResource;
 import org.ovirt.engine.api.resource.VnicProfilesResource;
@@ -378,6 +381,9 @@
 
         map = new ParentToCollectionMap(AffinityGroupResource.class, 
AffinityGroupsResource.class, Cluster.class);
         TYPES.put(AffinityGroup.class, map);
+
+        map = new ParentToCollectionMap(VmSessionResource.class, 
VmSessionsResource.class, VM.class);
+        TYPES.put(Session.class, map);
     }
 
     /**
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
index da83e79..281c12b 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
@@ -2790,7 +2790,6 @@
     </xs:sequence>
   </xs:complexType>
 
-
   <xs:element name="pm_proxy_types" type="PmProxyTypes"/>
 
   <xs:complexType name="PmProxyTypes">
@@ -2805,6 +2804,37 @@
     </xs:sequence>
   </xs:complexType>
 
+  <xs:complexType name="Session">
+    <xs:complexContent>
+      <xs:extension base="BaseResource">
+        <xs:sequence>
+          <xs:element name="protocol" type="xs:string" minOccurs="0" 
maxOccurs="1"/>
+          <xs:element ref="ip" minOccurs="0" maxOccurs="1"/>
+          <xs:element ref="user" minOccurs="0" maxOccurs="1"/>
+          <xs:element name="console_user" type="xs:boolean" minOccurs="0" 
maxOccurs="1"/>    
+        </xs:sequence>
+      </xs:extension>
+    </xs:complexContent>
+  </xs:complexType>
+
+<xs:element name="session" type="Session"/>
+
+  <xs:complexType name="Sessions">
+    <xs:complexContent>
+      <xs:extension base="BaseResources">
+        <xs:sequence>
+          <xs:annotation>
+            <xs:appinfo>
+                <jaxb:property name="Sessions"/>
+            </xs:appinfo>
+          </xs:annotation>
+          <xs:element ref="session" minOccurs="0" maxOccurs="unbounded"/>
+        </xs:sequence>
+      </xs:extension>
+    </xs:complexContent>
+  </xs:complexType>
+
+<xs:element name="sessions" type="Sessions"/>
 
   <!-- Virtual Machine Pools -->
 
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/AbstractBackendResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/AbstractBackendResource.java
index fdfdabd..f03cadd 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/AbstractBackendResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/AbstractBackendResource.java
@@ -13,7 +13,6 @@
 import org.ovirt.engine.api.model.Host;
 import org.ovirt.engine.api.model.Link;
 import org.ovirt.engine.api.restapi.types.Mapper;
-import org.ovirt.engine.api.restapi.types.MappingLocator;
 import org.ovirt.engine.api.utils.LinkHelper;
 import org.ovirt.engine.core.common.action.VdcReturnValueBase;
 import org.ovirt.engine.core.common.businessentities.AsyncTaskStatus;
@@ -36,7 +35,6 @@
     protected static final javax.ws.rs.core.Response.Status ACCEPTED_STATUS =
         javax.ws.rs.core.Response.Status.ACCEPTED;
 
-    protected MappingLocator mappingLocator;
     protected Class<R> modelType;
     protected Class<Q> entityType;
     protected String[] subCollections;
@@ -53,26 +51,6 @@
     protected AbstractBackendResource(Class<R> modelType, Class<Q> entityType, 
String... subCollections) {
         this(modelType, entityType);
         this.subCollections = subCollections;
-    }
-
-    public void setMappingLocator(MappingLocator mappingLocator) {
-        this.mappingLocator = mappingLocator;
-    }
-
-    public MappingLocator getMappingLocator() {
-        return mappingLocator;
-    }
-
-    //protected <S extends AbstractBackendResource<B extends BaseResource, A>> 
S inject(S resource) {
-    protected <S extends AbstractBackendResource<?, ?>> S inject(S resource) {
-        resource.setBackend(backend);
-        resource.setMappingLocator(mappingLocator);
-        resource.setValidatorLocator(validatorLocator);
-        resource.setSessionHelper(sessionHelper);
-        resource.setMessageBundle(messageBundle);
-        resource.setUriInfo(uriInfo);
-        resource.setHttpHeaders(httpHeaders);
-        return resource;
     }
 
     protected <F, T> Mapper<F, T> getMapper(Class<F> from, Class<T> to) {
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
index 63b3456..6d5c3e9 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
@@ -30,6 +30,7 @@
 import org.ovirt.engine.api.resource.AssignedTagsResource;
 import org.ovirt.engine.api.resource.CreationResource;
 import org.ovirt.engine.api.resource.DevicesResource;
+import org.ovirt.engine.api.resource.VmSessionsResource;
 import org.ovirt.engine.api.resource.SnapshotsResource;
 import org.ovirt.engine.api.resource.StatisticsResource;
 import org.ovirt.engine.api.resource.VmApplicationsResource;
@@ -479,4 +480,9 @@
     public VmReportedDevicesResource getVmReportedDevicesResource() {
         return inject(new BackendVmReportedDevicesResource(guid));
     }
+
+    @Override
+    public VmSessionsResource getVmSessionsResource() {
+        return inject(new BackendVmSessionsResource(guid));
+    }
 }
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionResource.java
new file mode 100644
index 0000000..4901c44
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionResource.java
@@ -0,0 +1,42 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.ovirt.engine.api.model.Session;
+import org.ovirt.engine.api.model.Sessions;
+import org.ovirt.engine.api.resource.VmSessionResource;
+import org.ovirt.engine.api.resource.VmSessionsResource;
+
+public class BackendVmSessionResource extends AbstractBackendResource<Session, 
Object> implements VmSessionResource {
+
+    private VmSessionsResource parent;
+    private String id;
+
+    public BackendVmSessionResource(VmSessionsResource parent, String id) {
+        super(Session.class, Object.class);
+        this.parent = parent;
+        this.id = id;
+    }
+
+    @Override
+    public Session get() {
+        // Get all sessions and search for the session with this ID. Must be 
done this way because
+        // there's no way to get session by ID from the engine.
+        Sessions sessions = parent.list();
+        if (sessions.isSetSessions()) {
+            for (Session session : sessions.getSessions()) {
+                if (session.getId().equals(id)) {
+                    return session;
+                }
+            }
+        }
+        throw new WebApplicationException(Response.Status.NOT_FOUND); // 
shouldn't happen.
+    }
+
+    @Override
+    protected Session doPopulate(Session model, Object entity) {
+        return model;
+    }
+
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionsResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionsResource.java
new file mode 100644
index 0000000..6d33a2f
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmSessionsResource.java
@@ -0,0 +1,100 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.ovirt.engine.api.model.Session;
+import org.ovirt.engine.api.model.Sessions;
+import org.ovirt.engine.api.model.User;
+import org.ovirt.engine.api.resource.UsersResource;
+import org.ovirt.engine.api.resource.VmSessionResource;
+import org.ovirt.engine.api.resource.VmSessionsResource;
+import org.ovirt.engine.api.restapi.types.VmMapper;
+import org.ovirt.engine.api.restapi.utils.GuidUtils;
+import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.core.common.queries.IdQueryParameters;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+import org.ovirt.engine.core.compat.Guid;
+
+public class BackendVmSessionsResource extends 
AbstractBackendCollectionResource<Session, Object> implements 
VmSessionsResource {
+
+    private Guid vmId;
+
+    public BackendVmSessionsResource(Guid vmId) {
+        super(Session.class, Object.class);
+        this.vmId = vmId;
+    }
+
+    @Override
+    public Sessions list() {
+        Object vm =
+                getEntity(entityType, VdcQueryType.GetVmByVmId, new 
IdQueryParameters(vmId), vmId.toString(), true);
+        Sessions sessions = VmMapper.map((VM) vm, new Sessions());
+        UsersResource usersResource = getUsersResource();
+        if (sessions.isSetSessions()) {
+            for (Session session : sessions.getSessions()) {
+                setSessionId(session);
+                setSessionUser(usersResource, session);
+                addLinks(session);
+            }
+        }
+        return sessions;
+    }
+
+    /**
+     * A session is not a business-entity in the engine and does not have an 
ID. This method generates an ID for the
+     * session object, based on its attributes.
+     *
+     * @param session
+     */
+    private void setSessionId(Session session) {
+        String idString = session.getUser().getName();
+        if (session.isSetIp() && session.getIp().isSetAddress()) {
+            idString += session.getIp().getAddress();
+        }
+        if (session.isSetProtocol()) {
+            idString += session.getProtocol();
+        }
+        session.setId(GuidUtils.generateGuidUsingMd5(idString).toString());
+        addLinks(session);
+    }
+
+    private void setSessionUser(UsersResource usersResource, Session session) {
+        if (session.isSetConsoleUser() && session.isConsoleUser()) { // (only 
the console user is assumed to be an ovirt
+                                                                     // user).
+            // Engine doesn't supply a query to get a user by name, so we must 
fetch
+            // all users and search.
+            for (User user : usersResource.list().getUsers()) {
+                if (user.getName().equals(session.getUser().getName())) {
+                    session.setUser(user);
+                    return;
+                }
+            }
+            // Console user is an ovirt user and must be found.
+            throw new WebApplicationException(Response.Status.NOT_FOUND);
+        }
+    }
+
+    private BackendUsersResource getUsersResource() {
+        return inject(new BackendUsersResource());
+    }
+
+    @Override
+    @Path("{iden}")
+    public VmSessionResource getSessionSubResource(@PathParam("iden") String 
id) {
+        return inject(new BackendVmSessionResource(this, id));
+    }
+
+    @Override
+    protected Response performRemove(String id) {
+        throw new UnsupportedOperationException("Remove of sessions not 
currently possible");
+    }
+
+    @Override
+    protected Session doPopulate(Session model, Object entity) {
+        return model;
+    }
+
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BaseBackendResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BaseBackendResource.java
index 3957709..3740f86 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BaseBackendResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BaseBackendResource.java
@@ -21,6 +21,7 @@
 import org.ovirt.engine.api.restapi.logging.MessageBundle;
 import org.ovirt.engine.api.restapi.logging.Messages;
 import org.ovirt.engine.api.restapi.resource.validation.ValidatorLocator;
+import org.ovirt.engine.api.restapi.types.MappingLocator;
 import org.ovirt.engine.api.restapi.util.SessionHelper;
 import org.ovirt.engine.api.restapi.utils.MalformedIdException;
 import org.ovirt.engine.core.common.action.VdcActionParametersBase;
@@ -42,6 +43,27 @@
     protected UriInfo uriInfo;
     protected HttpHeaders httpHeaders;
     protected ValidatorLocator validatorLocator;
+    protected MappingLocator mappingLocator;
+
+    // protected <S extends AbstractBackendResource<B extends BaseResource, 
A>> S inject(S resource) {
+    protected <S extends BaseBackendResource> S inject(S resource) {
+        resource.setBackend(backend);
+        resource.setMappingLocator(mappingLocator);
+        resource.setValidatorLocator(validatorLocator);
+        resource.setSessionHelper(sessionHelper);
+        resource.setMessageBundle(messageBundle);
+        resource.setUriInfo(uriInfo);
+        resource.setHttpHeaders(httpHeaders);
+        return resource;
+    }
+
+    public void setMappingLocator(MappingLocator mappingLocator) {
+        this.mappingLocator = mappingLocator;
+    }
+
+    public MappingLocator getMappingLocator() {
+        return mappingLocator;
+    }
 
     public void setBackend(BackendLocal backend) {
         this.backend = backend;
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/HostMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/HostMapper.java
index e3be06e..158ff35 100644
--- 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/HostMapper.java
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/HostMapper.java
@@ -1,8 +1,6 @@
 package org.ovirt.engine.api.restapi.types;
 
 import java.math.BigDecimal;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -52,7 +50,6 @@
     // REVISIT retrieve from configuration
     private static final int DEFAULT_VDSM_PORT = 54321;
     private static final String MD5_FILE_SIGNATURE = "md5";
-    private static final String MD5_SECURITY_ALGORITHM = "MD5";
 
     private static final String HOST_OS_DELEIMITER = " - ";
 
@@ -674,18 +671,7 @@
     }
 
     private static void setHookId(Hook hook, String hookName, String 
eventName, String md5) {
-        Guid guid = generateHookId(eventName, hookName, md5);
+        Guid guid = GuidUtils.generateGuidUsingMd5(eventName, hookName, md5);
         hook.setId(guid.toString());
-    }
-
-    public static Guid generateHookId(String eventName, String hookName, 
String md5) {
-        String idString = eventName + hookName + md5;
-        try {
-            byte[] hash = 
MessageDigest.getInstance(MD5_SECURITY_ALGORITHM).digest(idString.getBytes());
-            Guid guid = new Guid(hash, true);
-            return guid;
-        } catch (NoSuchAlgorithmException e) {
-            throw new IllegalStateException(e); // never happens, MD5 
algorithm exists
-        }
     }
 }
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/VmMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/VmMapper.java
index a6cf6c6..1013940 100644
--- 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/VmMapper.java
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/VmMapper.java
@@ -43,6 +43,8 @@
 import org.ovirt.engine.api.model.OsType;
 import org.ovirt.engine.api.model.Payload;
 import org.ovirt.engine.api.model.Quota;
+import org.ovirt.engine.api.model.Session;
+import org.ovirt.engine.api.model.Sessions;
 import org.ovirt.engine.api.model.Template;
 import org.ovirt.engine.api.model.Usb;
 import org.ovirt.engine.api.model.UsbType;
@@ -1348,4 +1350,78 @@
             return null; // Should never get here
         }
     }
+
+    /**
+     * This method maps the VM's open sessions with users. Engine currently 
does not regard sessions as business
+     * entities, and therefore a session doesn't have an ID. Generating IDs 
for sessions is outside the scope of this
+     * method and should be done by the method's invoker.
+     *
+     * The session involves a user. Sometimes this is an ovirt-user, and 
sometimes not. Engine provides only the user
+     * name, and this method maps it by placing it inside a 'User' object in 
the session. If invokers want to identify
+     * the ovirt user and provide a link to it, it's their responsibility to 
do so; this is out of the scope of this
+     * method.
+     *
+     * @param vm
+     * @param sessions
+     * @return
+     */
+    public static Sessions 
map(org.ovirt.engine.core.common.businessentities.VM vm, Sessions sessions) {
+        if (sessions == null) {
+            sessions = new Sessions();
+        }
+        mapConsoleSession(vm, sessions);
+        mapGusetSessions(vm, sessions);
+        return sessions;
+    }
+
+    /**
+     * This method maps the session of the 'console user', if exists. This is 
the ovirt user who opened a session
+     * through the user-console; the one who is said to be 'logged in' (or 
'have the ticket') to this VM. Currently
+     * engine makes available only the name and IP of this user. In the future 
it may make available also the connection
+     * protocol used in the session (spice/vnc).
+     *
+     * @param vm
+     * @param sessions
+     */
+    private static Sessions 
mapConsoleSession(org.ovirt.engine.core.common.businessentities.VM vm, Sessions 
sessions) {
+        String consoleUserName = vm.getConsoleCurentUserName();
+        if (consoleUserName != null && !consoleUserName.isEmpty()) {
+            Session consoleSession = new Session();
+            User consoleUser = new User();
+            consoleUser.setName(consoleUserName);
+            consoleSession.setUser(consoleUser);
+            if (vm.getClientIp()!=null && !vm.getClientIp().isEmpty()) {
+                IP ip = new IP();
+                ip.setAddress(vm.getClientIp());
+                consoleSession.setIp(ip);
+            }
+            consoleSession.setConsoleUser(true);
+            // TODO: in the future, map the connection protocol as well
+            sessions.getSessions().add(consoleSession);
+        }
+        return sessions;
+    }
+
+    /**
+     * This method maps the sessions of users who are connected to the VM, but 
are not the 'logged-in'/'console' user.
+     * Currently the information that engine supplies about these users is 
only a string, which contains the name of
+     * only one such user, if exists (the user is not necessarily an ovirt 
user). In the future the engine may pass
+     * multiple 'guest' users, along with their IPs and perhaps also the 
connection protocols that they are using (SSH,
+     * RDP...)
+     *
+     * @param vm
+     * @param sessions
+     */
+    private static Sessions 
mapGusetSessions(org.ovirt.engine.core.common.businessentities.VM vm, Sessions 
sessions) {
+        String guestUserName = vm.getGuestCurentUserName();
+        if (guestUserName != null && !guestUserName.isEmpty()) {
+            Session guestSession = new Session();
+            User user = new User();
+            user.setName(guestUserName);
+            guestSession.setUser(user);
+            // TODO: in the future, map the user-IP and connection protocol as 
well
+            sessions.getSessions().add(guestSession);
+        }
+        return sessions;
+    }
 }
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/GuidUtils.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/GuidUtils.java
index 8ea6525..5c5d240 100644
--- 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/GuidUtils.java
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/GuidUtils.java
@@ -1,8 +1,12 @@
 package org.ovirt.engine.api.restapi.utils;
 
 import org.ovirt.engine.core.compat.Guid;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 public class GuidUtils {
+
+    private static final String MD5_SECURITY_ALGORITHM = "MD5";
 
     public static Guid asGuid(String id) {
         try {
@@ -12,4 +16,27 @@
         }
     }
 
+    /**
+     * There are some business entities in the API, which are not regarded as 
business entities in the engine, and
+     * therefore they don't have IDs. The API generates uniqute GUIDs for 
them, according to their attributes. This
+     * method accepts one or more string attributes, concatenates them, and 
using Md5 hash to generate a unique Guid for
+     * them.
+     *
+     * @param args
+     *            one or more strings, guid will be generated from them
+     * @return unique Guid generated from the given strings.
+     */
+    public static Guid generateGuidUsingMd5(String... args) {
+        String id = "";
+        for (String arg : args) {
+            id = id + arg;
+        }
+        byte[] hash;
+        try {
+            hash = 
MessageDigest.getInstance(MD5_SECURITY_ALGORITHM).digest(id.getBytes());
+        } catch (NoSuchAlgorithmException e) {
+            throw new IllegalStateException(e); // never happens, MD5 
algorithm exists
+        }
+        return new Guid(hash, true);
+    }
 }
diff --git 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/VmMapperTest.java
 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/VmMapperTest.java
index 5ec55ad..1d14aa8 100644
--- 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/VmMapperTest.java
+++ 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/VmMapperTest.java
@@ -183,7 +183,7 @@
         vmDynamic.setStatus(VMStatus.Up);
         vmDynamic.setVmIp("2.2.2.2");
         vm.setDynamicData(vmDynamic);
-        VM map = VmMapper.map(vm, null);
+        VM map = VmMapper.map(vm, (VM) null);
         assertNotNull(map.getGuestInfo().getIps().getIPs().get(0));
         assertEquals(map.getGuestInfo().getIps().getIPs().get(0).getAddress(), 
"2.2.2.2");
     }
@@ -195,7 +195,7 @@
         vmDynamic.setStatus(VMStatus.Up);
         vmDynamic.setVmFQDN("localhost.localdomain");
         vm.setDynamicData(vmDynamic);
-        VM map = VmMapper.map(vm, null);
+        VM map = VmMapper.map(vm, (VM) null);
         assertNotNull(map.getGuestInfo().getFqdn());
         assertEquals(map.getGuestInfo().getFqdn(), "localhost.localdomain");
     }
@@ -206,7 +206,7 @@
         vmDynamic.setStatus(VMStatus.Up);
         vmDynamic.setVmIp("2.2.2.2 2.2.2.3 2.2.2.4");
         vm.setDynamicData(vmDynamic);
-        VM map = VmMapper.map(vm, null);
+        VM map = VmMapper.map(vm, (VM) null);
         assertNotNull(map.getGuestInfo().getIps().getIPs().get(0));
         assertEquals(map.getGuestInfo().getIps().getIPs().get(0).getAddress(), 
"2.2.2.2");
         assertEquals(map.getGuestInfo().getIps().getIPs().get(1).getAddress(), 
"2.2.2.3");
@@ -220,12 +220,12 @@
         entity.setStatus(VMStatus.Up);
         entity.setDisplay(5900);
         entity.setDisplaySecurePort(9999);
-        VM model = VmMapper.map(entity, null);
+        VM model = VmMapper.map(entity, (VM) null);
         assertTrue(model.getDisplay().getPort() == 5900);
         assertTrue(model.getDisplay().getSecurePort() == 9999);
         entity.setDisplay(-1);
         entity.setDisplaySecurePort(-1);
-        model = VmMapper.map(entity, null);
+        model = VmMapper.map(entity, (VM) null);
         assertNull(model.getDisplay().getPort());
         assertNull(model.getDisplay().getSecurePort());
     }
@@ -253,7 +253,7 @@
         entity.setStatus(VMStatus.Up);
         Guid guid = Guid.newGuid();
         entity.setRunOnVds(guid);
-        VM model = VmMapper.map(entity, null);
+        VM model = VmMapper.map(entity, (VM) null);
         assertEquals(guid.toString(), model.getHost().getId());
     }
 


-- 
To view, visit http://gerrit.ovirt.org/25591
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icb6488ea7118826adb169c9d1388fab7ab2792d8
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Ori Liel <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to