1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

feature #198: Classes to deal with the Hosts and the Host Pool

This commit is contained in:
Carlos Martin and Ruben S. Montero 2010-03-22 23:09:02 +01:00 committed by Ruben S. Montero
parent 60fe13afa3
commit 0e3c7311f0
2 changed files with 293 additions and 0 deletions

View File

@ -0,0 +1,200 @@
/*******************************************************************************
* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.opennebula.client.host;
import org.opennebula.client.Client;
import org.opennebula.client.OneResponse;
import org.opennebula.client.PoolElement;
import org.w3c.dom.Node;
/**
* This class represents an OpenNebula host.
* It also offers static XML-RPC call wrappers.
*/
public class Host extends PoolElement{
private static final String METHOD_PREFIX = "host.";
private static final String ALLOCATE = METHOD_PREFIX + "allocate";
private static final String INFO = METHOD_PREFIX + "info";
private static final String DELETE = METHOD_PREFIX + "delete";
private static final String ENABLE = METHOD_PREFIX + "enable";
private static final String[] HOST_STATES =
{"INIT", "MONITORING", "MONITORED", "ERROR", "DISABLED"};
/**
* Creates a new Host representation.
*
* @param id The host id (hid) of the machine.
* @param client XML-RPC Client.
*/
public Host(int id, Client client)
{
super(id, client);
}
/**
* @see PoolElement
*/
protected Host(Node xmlElement, Client client)
{
super(xmlElement, client);
}
// =================================
// Static XML-RPC methods
// =================================
/**
* Allocates a new host in OpenNebula
*
* @param client XML-RPC Client.
* @param hostname Hostname of the machine we want to add
* @param im The name of the information manager (im_mad_name),
* this values are taken from the oned.conf with the tag name IM_MAD (name)
* @param vmm The name of the virtual machine manager mad name
* (vmm_mad_name), this values are taken from the oned.conf with the
* tag name VM_MAD (name)
* @param tm The transfer manager mad name to be used with this host
* @return If successful the message contains the associated
* id generated for this host
*/
public static OneResponse allocate(Client client,
String hostname,
String im,
String vmm,
String tm)
{
return client.call(ALLOCATE, hostname, im, vmm, tm);
}
/**
* Retrieves the information of the given host.
*
* @param client XML-RPC Client.
* @param id The host id (hid) of the target machine.
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse info(Client client, int id)
{
return client.call(INFO, id);
}
/**
* Deletes a host from OpenNebula.
*
* @param client XML-RPC Client.
* @param id The host id (hid) of the target machine.
* @return A encapsulated response.
*/
public static OneResponse delete(Client client, int id)
{
return client.call(DELETE, id);
}
/**
* Enables or disables a given host.
*
* @param client XML-RPC Client.
* @param id The host id (hid) of the target machine.
* @param enable If set true OpenNebula will enable the
* target host, if set false it will disable it.
* @return A encapsulated response.
*/
public static OneResponse enable(Client client, int id, boolean enable)
{
return client.call(ENABLE, id, enable);
}
// =================================
// Instanced object XML-RPC methods
// =================================
/**
* Loads the xml representation of the host.
* The info is also stored internally.
*
* @see Host#info(Client, int)
*/
public OneResponse info()
{
OneResponse response = info(client, id);
super.processInfo(response);
return response;
}
/**
* Deletes the host from OpenNebula.
*
* @see Host#delete(Client, int)
*/
public OneResponse delete()
{
return delete(client, id);
}
/**
* Enables or disables the host.
*
* @see Host#enable(Client, int, boolean)
*/
public OneResponse enable(boolean enable)
{
return enable(client, id, enable);
}
// =================================
// Helpers
// =================================
/**
* Returns the state of the Host.
* <br/>
* The method {@link Host#info()} must be called before.
*
* @return The state of the Host.
*/
public String stateStr()
{
int state = state();
return state != -1 ? HOST_STATES[state()] : null;
}
/**
* Returns the short length string state of the Host.
* <br/>
* The method {@link Host#info()} must be called before.
*
* @return The short length string state of the Host.
*/
public String shortStateStr()
{
String st = stateStr();
if(st == null)
return null;
else if(st.equals("ERROR"))
return "err";
else if (st.equals("DISABLED"))
return "off";
else
return "on";
}
}

View File

@ -0,0 +1,93 @@
/*******************************************************************************
* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.opennebula.client.host;
import java.util.AbstractList;
import java.util.Iterator;
import org.opennebula.client.Client;
import org.opennebula.client.OneResponse;
import org.opennebula.client.Pool;
import org.opennebula.client.PoolElement;
import org.w3c.dom.Node;
/**
* This class represents an OpenNebula host pool.
* It also offers static XML-RPC call wrappers.
*/
public class HostPool extends Pool implements Iterable<Host>{
private static final String ELEMENT_NAME = "HOST";
private static final String INFO_METHOD = "hostpool.info";
/**
* Creates a new host pool
* @param client XML-RPC Client.
*/
public HostPool(Client client)
{
super(ELEMENT_NAME, client);
}
@Override
public PoolElement factory(Node node)
{
return new Host(node, client);
}
/**
* Retrieves all the hosts in the pool.
*
* @param client XML-RPC Client.
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse info(Client client)
{
return client.call(INFO_METHOD);
}
/**
* Loads the xml representation of the host pool.
*
* @see HostPool#info(Client)
*/
public OneResponse info()
{
OneResponse response = info(client);
super.processInfo(response);
return response;
}
public Iterator<Host> iterator()
{
AbstractList<Host> ab = new AbstractList<Host>()
{
public int size()
{
return getLength();
}
public Host get(int index)
{
return (Host) item(index);
}
};
return ab.iterator();
}
}