1 package org.opendaylight.openflowjava.tools;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.xml.sax.SAXException;
7 import javax.xml.XMLConstants;
8 import javax.xml.bind.JAXBContext;
9 import javax.xml.bind.JAXBException;
10 import javax.xml.bind.Marshaller;
11 import javax.xml.bind.Unmarshaller;
12 import javax.xml.validation.Schema;
13 import javax.xml.validation.SchemaFactory;
15 import java.math.BigInteger;
16 import java.util.List;
19 * @author Jozef Bacigal
22 public class ConnectionToolConfigurationServiceImpl implements ConnectionToolConfigurationService {
24 private static final Logger LOG = LoggerFactory.getLogger(ConnectionToolConfigurationServiceImpl.class);
27 public void marshallData(ConnectionTestTool.Params params, String configurationName) throws JAXBException, SAXException {
28 File file = new File(XML_FILE_PATH_WITH_FILE_NAME);
29 LOG.info("Marshaling configuration data to: {}", XML_FILE_PATH_WITH_FILE_NAME);
31 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
32 Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
33 LOG.info("with schema: {}", XSD_SCHEMA_PATH_WITH_FILE_NAME);
35 JAXBContext jaxbContext = JAXBContext.newInstance(Configurations.class);
36 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
38 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
39 jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, CONFIGURATION_XSD);
41 jaxbMarshaller.setSchema(schema);
43 ObjectFactory objectFactory = new ObjectFactory();
44 Configurations configurations = objectFactory.createConfigurations();
46 List<ConfigurationType> configurationTypes = configurations.getConfiguration();
48 for (ConfigurationType configurationType : this.getSavedConfigurations()) {
49 configurationTypes.add(configurationType);
52 ConfigurationType configurationType = new ConfigurationType();
53 configurationType.name = configurationName;
55 configurationType.controllerIp = params.controllerIP;
56 configurationType.devicesCount = BigInteger.valueOf(params.deviceCount);
57 configurationType.freeze = BigInteger.valueOf(params.freeze);
58 configurationType.port = BigInteger.valueOf(params.port);
59 configurationType.sleep = params.sleep;
60 configurationType.ssl = params.ssl;
61 configurationType.threads = BigInteger.valueOf(params.threads);
62 configurationType.timeout = BigInteger.valueOf(params.timeout);
64 configurationTypes.add(configurationType);
66 configurations.setConfiguration(configurationTypes);
67 jaxbMarshaller.marshal(configurations, file);
72 public ConnectionTestTool.Params unMarshallData(String configurationName) throws SAXException, JAXBException {
73 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
74 Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
75 LOG.debug("Loading schema from: {}", XSD_SCHEMA_PATH_WITH_FILE_NAME);
77 JAXBContext jc = JAXBContext.newInstance(Configurations.class);
79 Unmarshaller unmarshaller = jc.createUnmarshaller();
80 unmarshaller.setSchema(schema);
82 Configurations configurations = (Configurations) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
83 LOG.debug("Configurations ({}) are un-marshaled from {}", configurations.getConfiguration().size(), XML_FILE_PATH_WITH_FILE_NAME);
85 boolean foundConfiguration = false;
86 ConfigurationType configuration = null;
87 for (ConfigurationType configurationType : configurations.getConfiguration()) {
88 if (configurationType.getName().equals(configurationName)) {
89 configuration = configurationType;
90 foundConfiguration = true;
93 ConnectionTestTool.Params params = null;
94 if (foundConfiguration) {
95 LOG.info("Configuration {} found, loading parameters.", configurationName);
96 params = new ConnectionTestTool.Params();
97 params.controllerIP = configuration.getControllerIp();
98 params.deviceCount = configuration.getDevicesCount().intValue();
99 params.freeze = configuration.getFreeze().intValue();
100 params.port = configuration.getPort().intValue();
101 params.sleep = configuration.getSleep();
102 params.ssl = configuration.isSsl();
103 params.threads = configuration.getThreads().intValue();
104 params.timeout = configuration.getTimeout().intValue();
106 LOG.warn("Configuration {} not found. Using default parameters.", configurationName);
112 private List<ConfigurationType> getSavedConfigurations() throws SAXException, JAXBException{
114 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
115 Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
117 JAXBContext jc = JAXBContext.newInstance(Configurations.class);
119 Unmarshaller unmarshaller = jc.createUnmarshaller();
120 unmarshaller.setSchema(schema);
122 Configurations configurations = (Configurations) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
124 return configurations.getConfiguration();