Commit ec9646c1 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

connected the classes =)

parent eb67aa18
......@@ -36,14 +36,10 @@ public class TurnOff {
public void hibernate() throws RuntimeException, IOException {
String hibernateCommand="";
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem)
|| "Mac OS X".equals(operatingSystem)) {
hibernateCommand = "/usr/bin/dbus-send --system --print-reply --dest='org.freedesktop.UPower' /org/freedesktop/UPower org.freedesktop.UPower.Suspend";
}
if (executeCommand("sudo service nova-compute status").contains("running")) {
executeCommand(hibernateCommand);
hibernateCommand = "/usr/bin/dbus-send --system --print-reply --dest='org.freedesktop.UPower' "
+ "/org/freedesktop/UPower org.freedesktop.UPower.Suspend";
}
}
......
package org.fogbowcloud.green.agent.core;
import java.io.IOException;
public class testeMain {
public static void main(String[] args) throws RuntimeException, IOException {
}
public static void main(String[] args) throws RuntimeException, IOException {
}
}
......@@ -5,7 +5,8 @@ import java.io.IOException;
import java.util.Properties;
import org.fogbowcloud.green.server.core.greenStrategy.DefaultGreenStrategy;
import org.fogbowcloud.green.server.xmpp.GreenSitterXMPPComponent;
import org.fogbowcloud.green.server.xmpp.GreenSitterCommunicationComponent;
public class Main {
......@@ -20,9 +21,10 @@ public class Main {
try {
Properties prop = getProp(args[0]);
DefaultGreenStrategy gs = new DefaultGreenStrategy(prop);
GreenSitterXMPPComponent xmppComponent = new GreenSitterXMPPComponent(prop, gs);
GreenSitterCommunicationComponent gscc = new GreenSitterCommunicationComponent(prop, gs);
gs.setCommunicationComponent(gscc);
gs.start();
xmppComponent.process(true);
gscc.process(true);
} catch (Exception e) {
System.err.println("You must provide as parameter the"
+ " path for the configuration file");
......
......@@ -2,10 +2,8 @@ package org.fogbowcloud.green.server.core.greenStrategy;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
......@@ -13,6 +11,7 @@ import java.util.concurrent.TimeUnit;
import org.fogbowcloud.green.server.core.plugins.CloudInfoPlugin;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
import org.fogbowcloud.green.server.xmpp.GreenSitterCommunicationComponent;
public class DefaultGreenStrategy implements GreenStrategy {
......@@ -20,25 +19,27 @@ public class DefaultGreenStrategy implements GreenStrategy {
private List<? extends Host> allHosts;
private List<Host> nappingHosts = new LinkedList<Host>();
private List<Host> sleepingHosts = new LinkedList<Host>();
private Map<String, String> jidToIp = new HashMap<String, String>();
private GreenSitterCommunicationComponent gscc;
private Date lastUpdatedTime;
private long graceTime;
private long sleepingTime;
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
private ScheduledExecutorService executor = Executors
.newScheduledThreadPool(1);
public DefaultGreenStrategy(Properties greenProperties) {
this.openStackPlugin = new OpenStackInfoPlugin(greenProperties
.getProperty("openstack.endpoint").toString(), greenProperties
.getProperty("openstack.username").toString(), greenProperties.get(
"openstack.password").toString(), greenProperties.getProperty(
"openstack.tenant").toString());
.getProperty("openstack.username").toString(), greenProperties
.get("openstack.password").toString(), greenProperties
.getProperty("openstack.tenant").toString());
this.lastUpdatedTime = new Date();
this.sleepingTime = Long.parseLong(greenProperties.getProperty("greenstrategy.sleeptime"));
this.graceTime = Long.parseLong(greenProperties.get("greenstrategy.gracetime").toString());
this.sleepingTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.sleeptime"));
this.graceTime = Long.parseLong(greenProperties.get(
"greenstrategy.gracetime").toString());
}
public DefaultGreenStrategy(CloudInfoPlugin openStackPlugin, long graceTime) {
......@@ -49,18 +50,14 @@ public class DefaultGreenStrategy implements GreenStrategy {
private void setAllHosts() {
this.allHosts = this.openStackPlugin.getHostInformation();
}
protected void setDate(Date date){
protected void setDate(Date date) {
this.lastUpdatedTime = date;
}
public List<Host> getNappingHosts() {
return nappingHosts;
}
public Map<String, String> getJidToIp() {
return jidToIp;
}
public List<Host> getSleepingHosts() {
return sleepingHosts;
......@@ -82,8 +79,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
* napping than put it in sleeping host list
*/
if (nowTime - host.getUpdateTime() > this.graceTime) {
// terá comando estilo
// "xmpp, mande esse host dormir"
gscc.sendHostToBed(host.getName());
this.getSleepingHosts().add(host);
this.getNappingHosts().remove(host);
}
......@@ -93,15 +89,21 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
}
public void setCommunicationComponent(GreenSitterCommunicationComponent gscc) {
this.gscc = gscc;
}
public void setAgentAddress(String hostName, String jid, String ip) {
gscc.setAgentAddress(hostName, jid, ip);
}
public void wakeUpSleepingHost(int minCPU, int minRAM) {
Collections.sort(this.sleepingHosts);
for (Host host : this.getSleepingHosts()) {
if (host.getAvailableCPU() >= minCPU) {
if (host.getAvailableRAM() >= minRAM) {
// terá comando como wake on lan, acorde esse host
gscc.wakeUpHost(host.getName());
this.sleepingHosts.remove(host);
return;
}
......@@ -110,7 +112,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
}
}
public void start() {
executor.scheduleWithFixedDelay(new Runnable() {
@Override
......@@ -119,10 +121,4 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
}, 0, sleepingTime, TimeUnit.MILLISECONDS);
}
@Override
public void setAgentAddress(String JID, String IP) {
jidToIp.put(JID, IP);
}
}
package org.fogbowcloud.green.server.core.greenStrategy;
import org.fogbowcloud.green.server.xmpp.GreenSitterCommunicationComponent;
public interface GreenStrategy {
public void sendIdleHostsToBed();
public void wakeUpSleepingHost(int minCPU, int minRAM);
public void setAgentAddress(String JID, String IP);
public void setAgentAddress(String Name, String JID, String IP);
public void setCommunicationComponent(GreenSitterCommunicationComponent gscc);
}
......@@ -78,13 +78,13 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
private static class NovaHost {
boolean enabled;
boolean running;
public NovaHost(boolean enabled, boolean running) {
this.enabled = enabled;
this.running = running;
}
}
private HashMap<String, NovaHost> getNovaState(List<String> hostsName) {
ZoneService zones = os.compute().zones();
HashMap<String, NovaHost> novaRunning = new HashMap<String, NovaHost>();
......@@ -96,14 +96,15 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
if (zoneState.getAvailable()) {
for (String host : hostsName) {
try {
HashMap<String, ? extends NovaService> hostService = availabilityZone.getHosts().get(
host.toLowerCase());
HashMap<String, ? extends NovaService> hostService = availabilityZone
.getHosts().get(host.toLowerCase());
NovaService ns = hostService.get("nova-compute");
if (ns != null) {
String active = ns.getStatusActive();
String available = ns.getAvailable();
novaRunning.put(host, new NovaHost(
available.equals("true"), active.equals("true")));
novaRunning.put(host,
new NovaHost(available.equals("true"),
active.equals("true")));
}
} catch (Exception e) {
}
......@@ -113,15 +114,15 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
}
return novaRunning;
}
public List<Host> getHostInformation() {
List<String> hostsName = this.getHostsName();
HashMap<String, Integer> runningVM = this.getRunningVM();
HashMap<String, Integer> availableRam = this.getAvailableRam();
HashMap<String, Integer> availableCPU = this.getAvailableCPU();
HashMap<String, NovaHost> novaState = getNovaState(hostsName);
List<Host> hosts = new LinkedList<Host>();
for (String hostName : hostsName) {
try {
......@@ -132,8 +133,9 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
long updateTime = new Date().getTime();
int availableRamInTheHost = availableRam.get(hostName);
int availableCPUInTheHost = availableCPU.get(hostName);
Host host = new Host(name, runningVMInTheHost, novaEnable, novaRunning,
updateTime, availableRamInTheHost, availableCPUInTheHost);
Host host = new Host(name, runningVMInTheHost, novaEnable,
novaRunning, updateTime, availableRamInTheHost,
availableCPUInTheHost);
hosts.add(host);
} catch (Exception e) {
// Ignoring exceptions for hosts in unavailable zones
......
package org.fogbowcloud.green.server.xmpp;
import java.util.LinkedList;
import java.util.Properties;
import org.fogbowcloud.green.server.core.greenStrategy.GreenStrategy;
import org.jamppa.component.XMPPComponent;
public class GreenSitterXMPPComponent extends XMPPComponent {
public class GreenSitterCommunicationComponent extends XMPPComponent {
private GreenStrategy gs;
private LinkedList<Agent> listAgent = new LinkedList<Agent>();
public GreenSitterXMPPComponent(Properties prop, GreenStrategy gs) {
public GreenSitterCommunicationComponent(Properties prop, GreenStrategy gs) {
super(prop.getProperty("xmpp.jid"), prop.getProperty("xmpp.password"),
prop.getProperty("xmpp.host"), Integer.parseInt(prop.getProperty("xmpp.port")));
this.gs = gs;
addHandlers();
}
public void setAgentAddress(String hostName, String jid, String ip) {
Agent agent = new Agent(hostName, jid, ip);
listAgent.add(agent);
}
public void sendHostToBed(String host){
}
public void wakeUpHost(String host){
}
private void addHandlers() {
IAmAliveHandler alive = new IAmAliveHandler(gs);
......
......@@ -16,16 +16,14 @@ public class IAmAliveHandler extends AbstractQueryHandler {
@Override
public IQ handle(IQ query) {
//expected content format: "123.456.789"
String ip = query.getElement().element("query").elementText("content");
//expected content format: "123.456.789" IP
String ip = query.getElement().element("query").elementText("ip");
String hostName = query.getElement().element("query").elementText("hostName");
String jid = query.getFrom().toString();
gs.setAgentAddress(jid, ip);
gs.setAgentAddress(hostName, jid, ip);
IQ resultIQ;
resultIQ = IQ.createResultIQ(query);
resultIQ.getElement().addElement("query", getNamespace())
.addElement("content").setText("Received from JID: " + jid + " IP: "+ip);
IQ resultIQ = IQ.createResultIQ(query);
return resultIQ;
}
......
......@@ -31,11 +31,11 @@ public class TestIamAliverHandler {
.addElement("content").setText(IP);
DefaultGreenStrategy gs = Mockito.mock(DefaultGreenStrategy.class);
Mockito.doNothing().when(gs).setAgentAddress(JID, IP);
Mockito.doNothing().when(gs).setAgentAddress("nothing",JID, IP);
IAmAliveHandler iah = new IAmAliveHandler(gs);
IQ result = iah.handle(iq);
Assert.assertEquals("Received from JID: " + JID+ " IP: "+IP, result.getElement().element("query").elementText("content"));
Assert.assertEquals("result", result.getType().name());
}
}
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