4a486defab6d3ffec16fbfe673424ca6813353bf
[openflowjava.git] / openflowjava-tools / src / main / java / org / opendaylight / openflowjava / tools / ConnectionToolConfigurationServiceImpl.java
1 package org.opendaylight.openflowjava.tools;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.xml.sax.SAXException;
6
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;
14 import java.io.File;
15 import java.math.BigInteger;
16 import java.util.List;
17
18 /**
19  * @author Jozef Bacigal
20  *         Date: 8.3.2016
21  */
22 public class ConnectionToolConfigurationServiceImpl implements ConnectionToolConfigurationService {
23
24     private static final Logger LOG = LoggerFactory.getLogger(ConnectionToolConfigurationServiceImpl.class);
25
26     @Override
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);
30
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);
34
35         JAXBContext jaxbContext = JAXBContext.newInstance(Configurations.class);
36         Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
37
38         jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
39         jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, CONFIGURATION_XSD);
40
41         jaxbMarshaller.setSchema(schema);
42
43         ObjectFactory objectFactory = new ObjectFactory();
44         Configurations configurations = objectFactory.createConfigurations();
45
46         List<ConfigurationType> configurationTypes = configurations.getConfiguration();
47
48         for (ConfigurationType configurationType : this.getSavedConfigurations()) {
49             configurationTypes.add(configurationType);
50         }
51
52         ConfigurationType configurationType = new ConfigurationType();
53         configurationType.name = configurationName;
54
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);
63
64         configurationTypes.add(configurationType);
65
66         configurations.setConfiguration(configurationTypes);
67         jaxbMarshaller.marshal(configurations, file);
68
69     }
70
71     @Override
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);
76
77         JAXBContext jc = JAXBContext.newInstance(Configurations.class);
78
79         Unmarshaller unmarshaller = jc.createUnmarshaller();
80         unmarshaller.setSchema(schema);
81
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);
84
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;
91             }
92         }
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();
105         } else {
106             LOG.warn("Configuration {} not found. Using default parameters.", configurationName);
107         }
108
109         return params;
110     }
111
112     private List<ConfigurationType> getSavedConfigurations() throws SAXException, JAXBException{
113
114         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
115         Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
116
117         JAXBContext jc = JAXBContext.newInstance(Configurations.class);
118
119         Unmarshaller unmarshaller = jc.createUnmarshaller();
120         unmarshaller.setSchema(schema);
121
122         Configurations configurations = (Configurations) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
123
124         return configurations.getConfiguration();
125
126     }
127 }