Skip to content
Snippets Groups Projects
Commit c334c76a authored by Edileuton Henrique de Oliveira's avatar Edileuton Henrique de Oliveira
Browse files

webservice: Add net use inserts

parent 9fde97ee
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,7 @@ package br.ufpr.c3sl.datasid; ...@@ -24,6 +24,7 @@ package br.ufpr.c3sl.datasid;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.regex.*; import java.util.regex.*;
import java.math.BigInteger;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -41,8 +42,8 @@ import javax.xml.validation.SchemaFactory; ...@@ -41,8 +42,8 @@ import javax.xml.validation.SchemaFactory;
public class DataSID { public class DataSID {
private static final String SA_INVENTORY = "sidtb00_sa_inventory"; private static final String SA_INVENTORY = "telecenter_inventory";
private static final String SA_NET_USAGE = "sidtb00_sa_net_usage"; private static final String SA_NET_USAGE = "telecenter_net_usage";
private static final File XML_INVENTORY_SCHEMA = new File("/home/datasid/apache-tomcat/webapps/axis2/WEB-INF/collected-data.xsd"); private static final File XML_INVENTORY_SCHEMA = new File("/home/datasid/apache-tomcat/webapps/axis2/WEB-INF/collected-data.xsd");
private static final File XML_NET_USAGE_SCHEMA = new File("/home/datasid/apache-tomcat/webapps/axis2/WEB-INF/net-collected-data.xsd"); private static final File XML_NET_USAGE_SCHEMA = new File("/home/datasid/apache-tomcat/webapps/axis2/WEB-INF/net-collected-data.xsd");
...@@ -117,56 +118,6 @@ public class DataSID { ...@@ -117,56 +118,6 @@ public class DataSID {
return timestamp.toString(); return timestamp.toString();
} }
/**
* Receive an XML string which has the inventory data to be parsed and
* inserted into database. Return "Success" string if insertion operation
* is successful. Any other errors will throw exceptions.
*
* @author Eduardo Luis Buratti
* @param xmlData XML string of inventory
* @return String
*/
public static String setInventory(String xmlData) {
try {
InitialContext cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/datasid");
if (ds == null)
throw new Exception("Data source not found!");
Connection con = ds.getConnection();
if (con == null)
throw new Exception("Failed to get a database connection!");
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(XML_INVENTORY_SCHEMA);
JAXBContext context = JAXBContext.newInstance("br.ufpr.c3sl.datasid");
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
// Strip spaces in the beginning of the xml
xmlData = xmlData.replaceAll("^\\s+", "");
// transform the xml string into a InputStream
InputStream is = new ByteArrayInputStream(xmlData.getBytes());
// Decode the XML into a Java Object
JAXBElement<CollectedData> element = (JAXBElement<CollectedData>) unmarshaller.unmarshal(is);
CollectedData collected = element.getValue();
PreparedStatement st = createInventoryStatement(con, collected);
st.executeUpdate();
con.close();
return "Success";
} catch (Exception e) {
log(ERROR, e.getMessage() + " " + xmlData);
e.printStackTrace();
return "ERROR: " + e.getMessage();
}
}
/** /**
* Return a string with current agent version. * Return a string with current agent version.
* *
...@@ -189,7 +140,6 @@ public class DataSID { ...@@ -189,7 +140,6 @@ public class DataSID {
return AGENT_UPDATE_LINK; return AGENT_UPDATE_LINK;
} }
/** /**
* Receive an XML string which has the inventory data to be parsed and * Receive an XML string which has the inventory data to be parsed and
* inserted into database. Return "Success" string if insertion operation * inserted into database. Return "Success" string if insertion operation
...@@ -199,8 +149,7 @@ public class DataSID { ...@@ -199,8 +149,7 @@ public class DataSID {
* @param xmlData XML string of inventory * @param xmlData XML string of inventory
* @return String * @return String
*/ */
public static String setNetUsage(String xmlData) public static String setInventory(String xmlData) {
{
try { try {
InitialContext cxt = new InitialContext(); InitialContext cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/datasid"); DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/datasid");
...@@ -212,7 +161,7 @@ public class DataSID { ...@@ -212,7 +161,7 @@ public class DataSID {
throw new Exception("Failed to get a database connection!"); throw new Exception("Failed to get a database connection!");
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(XML_NET_USAGE_SCHEMA); Schema schema = factory.newSchema(XML_INVENTORY_SCHEMA);
JAXBContext context = JAXBContext.newInstance("br.ufpr.c3sl.datasid"); JAXBContext context = JAXBContext.newInstance("br.ufpr.c3sl.datasid");
Unmarshaller unmarshaller = context.createUnmarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller();
...@@ -225,10 +174,10 @@ public class DataSID { ...@@ -225,10 +174,10 @@ public class DataSID {
InputStream is = new ByteArrayInputStream(xmlData.getBytes()); InputStream is = new ByteArrayInputStream(xmlData.getBytes());
// Decode the XML into a Java Object // Decode the XML into a Java Object
JAXBElement<NetCollectedData> element = (JAXBElement<NetCollectedData>) unmarshaller.unmarshal(is); JAXBElement<CollectedData> element = (JAXBElement<CollectedData>) unmarshaller.unmarshal(is);
NetCollectedData collected = element.getValue(); CollectedData collected = element.getValue();
PreparedStatement st = createNetUsageStatement(con, collected); PreparedStatement st = createInventoryStatement(con, collected);
st.executeUpdate(); st.executeUpdate();
con.close(); con.close();
...@@ -243,15 +192,15 @@ public class DataSID { ...@@ -243,15 +192,15 @@ public class DataSID {
private static PreparedStatement createInventoryStatement(Connection con, CollectedData collectedData) throws SQLException { private static PreparedStatement createInventoryStatement(Connection con, CollectedData collectedData) throws SQLException {
final String query = "INSERT INTO " + SA_INVENTORY + " " + final String query = "INSERT INTO " + SA_INVENTORY + " " +
"(sa_contact_date, sa_telecentro_id, sa_versao, sa_machine, sa_machine_type, " + "(contact_date, telecentro_id, versao, machine, machine_type, " +
"sa_admin_name, sa_admin_phone, sa_tl_name, " + "admin_name, admin_phone, tl_name, " +
"sa_tl_phone, sa_state, sa_city, " + "tl_phone, state, city, " +
"sa_tl_street, sa_tl_number, sa_zipcode, " + "tl_street, tl_number, zipcode, " +
"sa_tl_neighborhood, sa_geolocation, sa_mirrors_timestamp, " + "tl_neighborhood, geolocation, mirrors_timestamp, " +
"sa_hd_model, sa_hd_size, sa_hd_used, " + "hd_model, hd_size, hd_used, " +
"sa_hd2_model, sa_hd2_size, sa_hd2_used, " + "hd2_model, hd2_size, hd2_used, " +
"sa_extra_hds, sa_memory_size, sa_processor, " + "extra_hds, memory_size, processor, " +
"sa_os_type, sa_os_distro, sa_kernel) VALUES " + "os_type, os_distro, kernel) VALUES " +
"(?, ?, ?, ?, ?, " + "(?, ?, ?, ?, ?, " +
"?, ?, ?, " + "?, ?, ?, " +
"?, ?, ?, " + "?, ?, ?, " +
...@@ -270,141 +219,205 @@ public class DataSID { ...@@ -270,141 +219,205 @@ public class DataSID {
List<Disk> disks = inventory.getDisks().getDisk(); List<Disk> disks = inventory.getDisks().getDisk();
TeleCentroInfo teleCentroInfo = collectedData.getTelecentroInfo(); TeleCentroInfo teleCentroInfo = collectedData.getTelecentroInfo();
// sa_contact_date = current date // contact_date = current date
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
st.setDate(1, new java.sql.Date(cal.getTimeInMillis())); st.setDate(1, new java.sql.Date(cal.getTimeInMillis()));
// sa_telecentro_id // telecentro_id
st.setString(2, teleCentroInfo.getDatasid()); st.setString(2, teleCentroInfo.getDatasid());
// sa_machine // machine
org.postgresql.util.PGobject macaddr = new org.postgresql.util.PGobject(); org.postgresql.util.PGobject macaddr = new org.postgresql.util.PGobject();
macaddr.setType("macaddr"); macaddr.setType("macaddr");
macaddr.setValue(interfaces.get(0).getMacAddress()); macaddr.setValue(interfaces.get(0).getMacAddress());
st.setObject(3, macaddr); st.setObject(3, macaddr);
// sa_versao // versao
st.setString(4, collectedData.getAgentVersion()); st.setString(4, collectedData.getAgentVersion());
// sa_machine_type // machine_type
if(collectedData.getMachineType().compareTo("client") == 0) if(collectedData.getMachineType().compareTo("client") == 0)
st.setInt(5, 0); st.setInt(5, 0);
else else
st.setInt(5, 1); st.setInt(5, 1);
// sa_admin_name // admin_name
st.setString(6, teleCentroInfo.getAdminName()); st.setString(6, teleCentroInfo.getAdminName());
// sa_admin_phone // admin_phone
st.setString(7, teleCentroInfo.getAdminPhone()); st.setString(7, teleCentroInfo.getAdminPhone());
// sa_tl_name // tl_name
st.setString(8, teleCentroInfo.getTlName()); st.setString(8, teleCentroInfo.getTlName());
// sa_tl_phone // tl_phone
st.setString(9, teleCentroInfo.getTlPhone()); st.setString(9, teleCentroInfo.getTlPhone());
// sa_state // state
st.setString(10, teleCentroInfo.getState()); st.setString(10, teleCentroInfo.getState());
// sa_city // city
st.setString(11, teleCentroInfo.getCity()); st.setString(11, teleCentroInfo.getCity());
// sa_tl_street // tl_street
st.setString(12, teleCentroInfo.getTlStreet()); st.setString(12, teleCentroInfo.getTlStreet());
// sa_tl_number // tl_number
st.setString(13, teleCentroInfo.getTlNumber()); st.setString(13, teleCentroInfo.getTlNumber());
// sa_tl_zipcode // tl_zipcode
st.setString(14, teleCentroInfo.getTlZipcode()); st.setString(14, teleCentroInfo.getTlZipcode());
// sa_tl_neighborhood // tl_neighborhood
st.setString(15, teleCentroInfo.getTlNeighborhood()); st.setString(15, teleCentroInfo.getTlNeighborhood());
// sa_geolocation // geolocation
st.setString(16, teleCentroInfo.getGeolocation()); st.setString(16, teleCentroInfo.getGeolocation());
// sa_mirrors_timestamp // mirrors_timestamp
st.setString(17, collectedData.getMirrorsTimestamp()); st.setString(17, collectedData.getMirrorsTimestamp());
// sa_hd_model // hd_model
st.setString(18, disks.get(0).getModel()); st.setString(18, disks.get(0).getModel());
// sa_hd_size // hd_size
st.setInt(19, disks.get(0).getSize().intValue()); st.setInt(19, disks.get(0).getSize().intValue());
// sa_hd_used // hd_used
st.setInt(20, disks.get(0).getUsed().intValue()); st.setInt(20, disks.get(0).getUsed().intValue());
if (disks.size() > 1) { if (disks.size() > 1) {
// sa_hd2_model // hd2_model
st.setString(18, disks.get(1).getModel()); st.setString(18, disks.get(1).getModel());
// sa_hd2_size // hd2_size
st.setInt(19, disks.get(1).getSize().intValue()); st.setInt(19, disks.get(1).getSize().intValue());
// sa_hd2_used // hd2_used
st.setInt(20, disks.get(1).getUsed().intValue()); st.setInt(20, disks.get(1).getUsed().intValue());
} }
else { else {
// sa_hd2_model // hd2_model
st.setNull(18, Types.VARCHAR); st.setNull(18, Types.VARCHAR);
// sa_hd2_size // hd2_size
st.setNull(19, Types.INTEGER); st.setNull(19, Types.INTEGER);
// sa_hd2_used // hd2_used
st.setNull(20, Types.INTEGER); st.setNull(20, Types.INTEGER);
} }
// sa_extra_hds // extra_hds
st.setInt(21, (disks.size() > 2) ? (disks.size() - 2) : 0); st.setInt(21, (disks.size() > 2) ? (disks.size() - 2) : 0);
// sa_memory_size // memory_size
st.setInt(22, inventory.getMemory().intValue()); st.setInt(22, inventory.getMemory().intValue());
// sa_processor // processor
st.setString(23, inventory.getProcessor()); st.setString(23, inventory.getProcessor());
// sa_os_type // os_type
st.setString(24, inventory.getOs()); st.setString(24, inventory.getOs());
// sa_os_distro // os_distro
st.setString(25, inventory.getDistro()); st.setString(25, inventory.getDistro());
// sa_kernel // kernel
st.setString(26, inventory.getKernel()); st.setString(26, inventory.getKernel());
return st; return st;
} }
/**
* Receive an XML string which has the inventory data to be parsed and
* inserted into database. Return "Success" string if insertion operation
* is successful. Any other errors will throw exceptions.
*
* @author Eduardo Luis Buratti
* @param xmlData XML string of inventory
* @return String
*/
public static String setNetUsage(String xmlData)
{
try {
InitialContext cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/datasid");
if (ds == null)
throw new Exception("Data source not found!");
private static PreparedStatement createNetUsageStatement(Connection con, NetCollectedData netCollectedData) throws SQLException { Connection con = ds.getConnection();
final String query = "INSERT INTO " + SA_NET_USAGE + " " + if (con == null)
"(sa_contact_date, sa_telecentro_id, sa_versao, sa_machine) VALUES " + throw new Exception("Failed to get a database connection!");
"(?, ?, ?, ?);";
PreparedStatement st = con.prepareStatement(query); SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(XML_NET_USAGE_SCHEMA);
List<Interface> interfaces = netCollectedData.getInterfaces().getInterface(); JAXBContext context = JAXBContext.newInstance("br.ufpr.c3sl.datasid");
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
// Strip spaces in the beginning of the xml
xmlData = xmlData.replaceAll("^\\s+", "");
// sa_contact_date = current date // transform the xml string into a InputStream
Calendar cal = Calendar.getInstance(); InputStream is = new ByteArrayInputStream(xmlData.getBytes());
st.setDate(1, new java.sql.Date(cal.getTimeInMillis()));
// sa_telecentro_id // Decode the XML into a Java Object
st.setString(2, netCollectedData.getTelecentroId()); JAXBElement<NetCollectedData> element = (JAXBElement<NetCollectedData>) unmarshaller.unmarshal(is);
NetCollectedData netCollectedData = element.getValue();
List<Interface> interfaces = netCollectedData.getInterfaces().getInterface();
// sa_machine // contact_date = current date
Calendar cal = Calendar.getInstance();
java.sql.Date contactDate = new java.sql.Date(cal.getTimeInMillis());
// macaddr
org.postgresql.util.PGobject macaddr = new org.postgresql.util.PGobject(); org.postgresql.util.PGobject macaddr = new org.postgresql.util.PGobject();
macaddr.setType("macaddr"); macaddr.setType("macaddr");
macaddr.setValue(interfaces.get(0).getMacAddress()); macaddr.setValue(interfaces.get(0).getMacAddress());
List<NetUse> netUses = netCollectedData.getBandwidthUsage().getNetuse();
for(NetUse netUse : netUses) {
PreparedStatement st = createNetUsageStatement(con, contactDate, netCollectedData.getTelecentroId(), (Object)macaddr, netUse.getDate(), netUse.getTime(), netUse.getRx().getBytes(), netUse.getRx().getPackets(), netUse.getTx().getBytes(), netUse.getTx().getPackets());
st.executeUpdate();
}
con.close();
return "Success";
} catch (Exception e) {
log(ERROR, e.getMessage() + " " + xmlData);
e.printStackTrace();
return "ERROR: " + e.getMessage();
}
}
private static PreparedStatement createNetUsageStatement(Connection con, java.sql.Date contactDate, String m_superid, Object macaddr, String collect_date, String collect_time, BigInteger down_kbits, BigInteger down_packages, BigInteger up_kbits, BigInteger up_packages) throws SQLException {
final String query = "INSERT INTO " + SA_NET_USAGE + " " +
"(contact_date, m_superid, macaddr, collect_date, collect_time," +
"down_kbits, down_packages, up_kbits, up_packages) VALUES " +
"(?, ?, ?, ?, ?, ?, ?, ?, ?);";
PreparedStatement st = con.prepareStatement(query);
st.setDate(1, contactDate);
// m_superid = telecentro-id
st.setString(2, m_superid);
st.setObject(3, macaddr); st.setObject(3, macaddr);
// sa_versao st.setString(4, collect_date);
st.setString(4, netCollectedData.getAgentVersion());
st.setString(5, collect_time);
st.setInt(6, down_kbits.intValue() );
st.setInt(7, down_packages.intValue());
st.setInt(8, up_kbits.intValue());
st.setInt(9, up_packages.intValue());
return st; return st;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment