Commit 9b7a79cb authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Updated openNebula Plugin

parent 088131dc
package org.fogbowcloud.green.server.core.plugins.opennebula;
import org.apache.log4j.Logger;
import org.opennebula.client.Client;
import org.opennebula.client.ClientConfigurationException;
import org.opennebula.client.host.HostPool;
public class ClientFactory {
private static final Logger LOGGER = Logger
.getLogger(ClientFactory.class);
private String userPasswordTuple;
private String endPoint;
public ClientFactory(String user, String password, String endPoint) {
this.endPoint = endPoint;
this.userPasswordTuple = user + ":" + password;
}
private Client createOneClient() throws ClientConfigurationException {
return new Client(userPasswordTuple, endPoint);
}
public HostPool initializeHostPool() {
try {
HostPool hostPool = new HostPool(createOneClient());
return hostPool;
} catch (ClientConfigurationException e) {
LOGGER.fatal("Authentication failed", e);
}
return null;
}
}
......@@ -3,29 +3,21 @@ package org.fogbowcloud.green.server.core.plugins.opennebula;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.fogbowcloud.green.server.core.greenStrategy.DateWrapper;
import org.fogbowcloud.green.server.core.greenStrategy.Host;
import org.fogbowcloud.green.server.core.plugins.CloudInfoPlugin;
import org.opennebula.client.Client;
import org.opennebula.client.ClientConfigurationException;
import org.opennebula.client.host.HostPool;
public class OpenNebulaInfoPlugin implements CloudInfoPlugin {
private String userPasswordTuple;
private String endPoint;
private static final Logger LOGGER = Logger
.getLogger(OpenNebulaInfoPlugin.class);
private ClientFactory clientFactory;
public OpenNebulaInfoPlugin(String user, String password, String endPoint) {
this.endPoint = endPoint;
this.userPasswordTuple = user + ":" + password;
clientFactory = new ClientFactory(user, password, endPoint);
}
protected Client createOneClient() throws ClientConfigurationException {
return new Client(userPasswordTuple, endPoint);
protected void setClientFactory(ClientFactory clientFactory) {
this.clientFactory = clientFactory;
}
protected Host openNebulaHostToGreenSitterHost(
......@@ -51,22 +43,10 @@ public class OpenNebulaInfoPlugin implements CloudInfoPlugin {
datew.getTime(), availableCPU, availableRAM);
}
protected HostPool initializeHostPool() {
Client oneClient;
try {
oneClient = createOneClient();
HostPool hostPool = new HostPool(oneClient);
return hostPool;
} catch (ClientConfigurationException e) {
LOGGER.fatal("Authentication failed", e);
}
return null;
}
@Override
public List<? extends Host> getHostInformation() {
List<Host> greenSitterHosts = new LinkedList<Host>();
HostPool hostPool = initializeHostPool();
HostPool hostPool = clientFactory.initializeHostPool();
if (hostPool != null) {
hostPool.info();
for (int i = 0; i < hostPool.getLength(); i++) {
......
......@@ -4,52 +4,69 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.mockito.Mockito;
import org.opennebula.client.OneResponse;
import org.opennebula.client.host.Host;
import org.opennebula.client.host.HostPool;
public class TestOpenNebulaPlugin {
private Host createONHostMock(String name, String runningVM, int state,
String freeCPU, String freeRAM) {
Host host = Mockito.mock(Host.class);
Mockito.when(host.getName()).thenReturn(name);
Mockito.when(host.xpath("HOST_SHARE/RUNNING_VMS")).thenReturn(runningVM);
Mockito.when(host.state()).thenReturn(state);
Mockito.when(host.xpath("HOST_SHARE/FREE_CPU")).thenReturn(freeCPU);
Mockito.when(host.xpath("HOST_SHARE/FREE_MEM")).thenReturn(freeRAM);
private Host createONHostMock(String name, String runningVM, int state,
String freeCPU, String freeRAM) {
Host host = Mockito.mock(Host.class);
Mockito.when(host.getName()).thenReturn(name);
Mockito.when(host.xpath("HOST_SHARE/RUNNING_VMS"))
.thenReturn(runningVM);
Mockito.when(host.state()).thenReturn(state);
Mockito.when(host.xpath("HOST_SHARE/FREE_CPU")).thenReturn(freeCPU);
Mockito.when(host.xpath("HOST_SHARE/FREE_MEM")).thenReturn(freeRAM);
return host;
}
}
private HostPool createHostPoolMock(int lengthHostPool) {
HostPool hostPool = Mockito.mock(HostPool.class);
OneResponse response = new OneResponse(true, "");
Mockito.when(hostPool.info()).thenReturn(response);
Mockito.when(hostPool.getLength()).thenReturn(lengthHostPool);
for (int i = 0; i < lengthHostPool; i ++) {
Host hostON = createONHostMock("host" + i, "1", 1, "2", "1024");
Mockito.when(hostPool.item(i)).thenReturn(hostON);
}
return hostPool;
}
private ClientFactory createClientFactoryMock(int lengthHostPool) {
ClientFactory cf = Mockito.mock(ClientFactory.class);
HostPool hp = createHostPoolMock(lengthHostPool);
Mockito.when(cf.initializeHostPool()).thenReturn(hp);
return cf;
}
@Test
public void testNoHosts() {
OpenNebulaInfoPlugin onip =
new OpenNebulaInfoPlugin("user", "password", "localhost/");
OpenNebulaInfoPlugin onip = new OpenNebulaInfoPlugin("user",
"password", "localhost/");
assertEquals(0, onip.getHostInformation().size());
}
@Test
public void testConvertingHost() {
OpenNebulaInfoPlugin onip =
new OpenNebulaInfoPlugin("user", "password", "localhost/");
OpenNebulaInfoPlugin onip = new OpenNebulaInfoPlugin("user",
"password", "localhost/");
Host hostON = createONHostMock("host1", "1", 1, "2", "1024");
org.fogbowcloud.green.server.core.greenStrategy.Host hostGS =
onip.openNebulaHostToGreenSitterHost(hostON);
org.fogbowcloud.green.server.core.greenStrategy.Host hostGS = onip
.openNebulaHostToGreenSitterHost(hostON);
assertEquals("host1", hostGS.getName());
assertEquals(1, hostGS.getRunningVM());
assertEquals(2, hostGS.getAvailableCPU());
assertEquals(1024, hostGS.getAvailableRAM());
}
@Test
public void testHostsInThePool() {
OpenNebulaInfoPlugin onip =
new OpenNebulaInfoPlugin("user", "password", "localhost/");
Host hostON = createONHostMock("host1", "1", 1, "2", "1024");
org.fogbowcloud.green.server.core.greenStrategy.Host hostGS =
onip.openNebulaHostToGreenSitterHost(hostON);
assertEquals("host1", hostGS.getName());
assertEquals(1, hostGS.getRunningVM());
assertEquals(2, hostGS.getAvailableCPU());
assertEquals(1024, hostGS.getAvailableRAM());
OpenNebulaInfoPlugin onip = new OpenNebulaInfoPlugin("user", "password", "localhost");
onip.setClientFactory(createClientFactoryMock(2));
assertEquals(2, onip.getHostInformation().size());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment