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 Configurations configurations = new Configurations();
45 List<ConfigurationType> configurationTypes = configurations.getConfiguration();
47 for (ConfigurationType configurationType : this.getSavedConfigurations()) {
48 configurationTypes.add(configurationType);
51 ConfigurationType configurationType = new ConfigurationType();
52 configurationType.name = configurationName;
54 configurationType.controllerIp = params.controllerIP;
55 configurationType.devicesCount = BigInteger.valueOf(params.deviceCount);
56 configurationType.freeze = BigInteger.valueOf(params.freeze);
57 configurationType.port = BigInteger.valueOf(params.port);
58 configurationType.sleep = params.sleep;
59 configurationType.ssl = params.ssl;
60 configurationType.threads = BigInteger.valueOf(params.threads);
61 configurationType.timeout = BigInteger.valueOf(params.timeout);
63 configurationTypes.add(configurationType);
65 configurations.setConfiguration(configurationTypes);
66 jaxbMarshaller.marshal(configurations, file);
71 public ConnectionTestTool.Params unMarshallData(String configurationName) throws SAXException, JAXBException {
72 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
73 Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
74 LOG.debug("Loading schema from: {}", XSD_SCHEMA_PATH_WITH_FILE_NAME);
76 JAXBContext jc = JAXBContext.newInstance(Configurations.class);
78 Unmarshaller unmarshaller = jc.createUnmarshaller();
79 unmarshaller.setSchema(schema);
81 Configurations configurations = (Configurations) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
82 LOG.debug("Configurations ({}) are un-marshaled from {}", configurations.getConfiguration().size(), XML_FILE_PATH_WITH_FILE_NAME);
84 boolean foundConfiguration = false;
85 ConfigurationType configuration = null;
86 for (ConfigurationType configurationType : configurations.getConfiguration()) {
87 if (configurationType.getName().equals(configurationName)) {
88 configuration = configurationType;
89 foundConfiguration = true;
92 ConnectionTestTool.Params params = null;
93 if (foundConfiguration) {
94 LOG.info("Configuration {} found, loading parameters.", configurationName);
95 params = new ConnectionTestTool.Params();
96 params.controllerIP = configuration.getControllerIp();
97 params.deviceCount = configuration.getDevicesCount().intValue();
98 params.freeze = configuration.getFreeze().intValue();
99 params.port = configuration.getPort().intValue();
100 params.sleep = configuration.getSleep();
101 params.ssl = configuration.isSsl();
102 params.threads = configuration.getThreads().intValue();
103 params.timeout = configuration.getTimeout().intValue();
105 LOG.warn("Configuration {} not found. Using default parameters.", configurationName);
111 private List<ConfigurationType> getSavedConfigurations() throws SAXException, JAXBException{
113 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
114 Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
116 JAXBContext jc = JAXBContext.newInstance(Configurations.class);
118 Unmarshaller unmarshaller = jc.createUnmarshaller();
119 unmarshaller.setSchema(schema);
121 Configurations configurations = (Configurations) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
123 return configurations.getConfiguration();