Commit 3e6c0789 authored by afreyssin's avatar afreyssin
Browse files

CTRL-Green use-case.

parent a83e9d1e
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package com.scalagent.ctrlgreen;
import java.io.Serializable;
import javax.jms.Message;
public class Action implements Serializable {
public final static int NoAction = 0;
public final static int AddEntry = 1;
public final static int DeleteEntry = 2;
public final static int AddRule = 3;
public final static int DeleteRule = 4;
public final static int ValidatePlan = 5;
public final static int ExecutePlan = 6;
public final static int CloneVM = 7;
public final static int StartVM = 8;
public final static int StopVM = 9;
public final static int MigrateVM = 10;
public final static int DeleteVM = 11;
public final static int StartHypervisor = 12;
public final static int StopHypervisor = 13;
public final static int PowerOffHypervisor = 14;
public final static int ModifyCPU = 15;
public final static int ModifyRAM = 16;
public final static int ModifyStorage = 17;
public final static int AddMonitoring = 18;
public final static int RemoveMonitoring = 19;
public final static int GetInventoryCMDB = 20;
public final static int GetInventoryVMware = 21;
public final static int MaxAction = 21;
final static String[] actionsStr = {
"NoAction",
"AddEntry",
"DeleteEntry",
"AddRule",
"DeleteRule",
"ValidatePlan",
"ExecutePlan",
"CloneVM",
"StartVM",
"StopVM",
"MigrateVM",
"DeleteVM",
"StartHypervisor",
"StopHypervisor",
"PowerOffHypervisor",
"ModifyCPU",
"ModifyRAM",
"ModifyStorage",
"AddMonitoring",
"RemoveMonitoring",
"GetInventoryCMDB",
"GetInventoryVMware"
};
private int type = NoAction;
public int getType() {
return type;
}
public Action(int type) {
this.type = type;
}
protected Message toJMSMessage() {
return null;
}
public String toString() {
String action = null;
if ((type < NoAction) || (type >= MaxAction))
action = "Unknown#" + type;
else
action = actionsStr[type];
return action;
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package com.scalagent.ctrlgreen;
public interface ActionHandler {
ActionReturn onAction(Action action);
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package com.scalagent.ctrlgreen;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
public class ActionListener implements MessageListener {
ActionHandler handler = null;
Session session = null;
MessageConsumer consumer = null;
MessageProducer producer = null;
ActionListener(ActionHandler handler, Connection cnx, Destination dest, String name) throws JMSException {
Trace.debug("Initialize ActionListener");
this.handler = handler;
try {
session = cnx.createSession(true, 0);
consumer = session.createConsumer(dest, "callee='" + name + "'");
producer = session.createProducer(null);
consumer.setMessageListener(this);
} catch (JMSException exc) {
close();
throw exc;
}
}
@Override
public void onMessage(Message msg) {
try {
Trace.debug("ActionListener: get " + msg);
Destination replyTo = null;
String correlationId = null;
ActionReturn reply = null;
try {
replyTo = msg.getJMSReplyTo();
correlationId = msg.getJMSMessageID();
} catch (JMSException exc) {
Trace.error("ActionListener: Cannot get replyTo.");
}
try {
int type = msg.getIntProperty("type");
Action action = (Action) ((ObjectMessage) msg).getObject();
Trace.debug("ActionListener: call handler");
reply = handler.onAction(action);
} catch (Exception exc) {
Trace.error("ActionListener: Bad action.", exc);
reply = new ActionReturn(ActionReturn.ERROR, "Bad action definition");
}
Trace.debug("ActionListener: reply to " + replyTo);
if (replyTo != null) {
Message replyMsg;
if (reply == null)
reply = new ActionReturn(ActionReturn.NOP);
try {
replyMsg = session.createMessage();
replyMsg.setJMSCorrelationID(correlationId);
replyMsg.setIntProperty("replyCode", reply.error);
if (reply.msg != null)
replyMsg.setStringProperty("replyMsg", reply.msg);
producer.send(replyTo, replyMsg);
} catch (JMSException exc) {
Trace.error("ActionListener: Cannot send reply.", exc);
}
}
try {
session.commit();
} catch (JMSException exc) {
Trace.error("ActionListener: Cannot commit.", exc);
}
} catch (Throwable t) {
Trace.fatal("ActionListener.onMessage", t);
} finally {
Trace.debug("ActionListener: end ");
}
}
void close() {
try {
if (producer != null)
producer.close();
} catch (JMSException exc) {
Trace.error("InventoryListener: Cannot close producer.", exc);
} finally {
producer = null;
}
try {
if (consumer != null)
consumer.close();
} catch (JMSException exc) {
Trace.error("InventoryListener: Cannot close consumer.", exc);
} finally {
consumer = null;
}
try {
if (session != null)
session.close();
} catch (JMSException exc) {
Trace.error("InventoryListener: Cannot close session.");
} finally {
session = null;
}
handler = null;
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package com.scalagent.ctrlgreen;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
public class ActionRequestor {
Destination queue = null;
Session session = null;
MessageConsumer cons = null;
MessageProducer prod = null;
/**
* Initialize a Requestor.
* @throws Exception
*/
ActionRequestor(Connection cnx) throws JMSException {
session = cnx.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = session.createTemporaryQueue();
prod = session.createProducer(null);
cons = session.createConsumer(queue);
}
/**
* Execute a synchronous request.
*
* @param dest The destination to send requests.
* @param callee The name of called component.
* @param action the action to execute.
* @return The result of action.
* @throws JMSException
*/
synchronized ActionReturn request(Destination dest, String callee, Action action) throws JMSException {
if (dest == null)
throw new IllegalStateException("Cannot request \"null\" destination");
ObjectMessage request = session.createObjectMessage();
request.setJMSReplyTo(queue);
request.setStringProperty("callee", callee);
request.setIntProperty("type", action.getType());
request.setObject(action);
prod.send(dest, request);
String msgid = request.getJMSMessageID();
Message reply = null;
do {
reply = cons.receive();
} while ((reply != null) && !msgid.equals(reply.getJMSCorrelationID()));
return new ActionReturn(reply.getIntProperty("replyCode"),
reply.getStringProperty("replyMsg"));
}
/**
* Execute a request with timeout, if timeout is 0 the request is asynchronous.
*
* @param dest The destination to send requests.
* @param callee The name of called component.
* @param action the action to execute.
* @param timeout The maximum time to wait the reply.
* @return The result of action.
* @throws JMSException
*/
synchronized ActionReturn request(Destination dest, String callee, Action action, long timeout) throws JMSException {
if (dest == null)
throw new IllegalStateException("Cannot request \"null\" destination");
if (timeout < 0)
throw new IllegalStateException("Invalid timeout:" + timeout);
ObjectMessage request = session.createObjectMessage();
if (timeout != 0) request.setJMSReplyTo(queue);
request.setStringProperty("callee", callee);
request.setIntProperty("type", action.getType());
request.setObject(action);
prod.send(dest, request);
if (timeout == 0) return null;
String msgid = request.getJMSMessageID();
long end = System.currentTimeMillis() + timeout;
Message reply = null;
while (timeout > 0) {
reply = null;
reply = cons.receive(timeout);
if (reply == null) break;
if (msgid.equals(reply.getJMSCorrelationID()))
break;
timeout = end - System.currentTimeMillis();
}
if (reply == null) return null;
return new ActionReturn(reply.getIntProperty("replyCode"),
reply.getStringProperty("replyMsg"));
}
synchronized void close() {
try {
if (session != null)
session.close();
} catch (JMSException exc) {
Trace.error("ActionRequestor: Cannot close session", exc);
}
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package com.scalagent.ctrlgreen;
public class ActionReturn {
boolean replied = true;
int error;
String msg;
public static final int NOP = -99;
public static final int FATAL = -3;
public static final int WARN = -2;
public static final int ERROR = -1;
public static final int OK = 0;
/**
* Creates an action return with OK result and no message.
*/
public ActionReturn() {
error = OK;
msg = null;
}
/**
* Creates an action return with specified result and no message.
*
* @param error The error code to transmit to caller.
*/
public ActionReturn(int error) {
this.error = error;
this.msg = null;
}
/**
* Creates an action return with OK result and specified message.
*
* @param msg The message to transmit to caller.
*/
public ActionReturn(String msg) {
this.error = OK;
this.msg = msg;
}
/**
* Creates an action return with specified result and message.
*
* @param error The error code to transmit to caller.
* @param msg The message to transmit to caller.
*/
public ActionReturn(int error, String msg) {
this.error = error;
this.msg = msg;
}
public int getReturnCode() {
return error;
}
public String getReturnMessage() {
return msg;
}
public String toString() {
if (msg != null)
return msg + '[' + error + ']';
return "[" + error + ']';
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package com.scalagent.ctrlgreen;
import javax.jms.JMSException;
import org.objectweb.joram.client.jms.ConnectionFactory;
import org.objectweb.joram.client.jms.FactoryParameters;
import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
public class CMDBFramework {
ClientFramework framework = null;
/**
* Initialization of ScopeBR framework.
*
* @param host hostname of Joram server.
* @param port listening port of Joram server.
* @param actionHandler Handler for actions.
* @param VMWareHandler Handler for VMWare inventory.
* @throws Exception if the framework can not initialize.
*/
public CMDBFramework(String host, int port,
ActionHandler actionHandler,
InventoryHandler VMWareHandler) throws Exception {
ConnectionFactory cf = TcpConnectionFactory.create(host, port);
FactoryParameters parameters = cf.getParameters();
parameters.connectingTimer = 10;
parameters.cnxPendingTimer = 5000;
try {
framework = new ClientFramework("CMDB", cf);
} catch (Exception exc) {
Trace.fatal("TuneFramework: Cannot initialize framework.", exc);
throw exc;
}
try {
framework.setActionHandler(actionHandler);
} catch (Exception exc) {
Trace.fatal("DNSFramework: Cannot initialize framework.", exc);
close();
throw exc;
}
try {
framework.setInventoryVMWareHandler(VMWareHandler);
} catch (Exception exc) {
Trace.fatal("TuneFramework: Cannot initialize framework.", exc);
close();
throw exc;
}
}
public ActionReturn invokeAction(String name, Action action) throws JMSException {
return framework.invokeAction(name, action);
}
public void publishInventoryCMDB(Inventory inventory) throws JMSException {
framework.publishInventoryCMDB(inventory);
}
/**
* Close the framework.
*/
public void close() {
if (framework != null)
framework.close();
framework = null;
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2012 ScalAgent Distributed Technologies
*