f309a7dd814122d7a3ea435fcbfa9449e1069827
[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         Configurations configurations = new Configurations();
44
45         List<ConfigurationType> configurationTypes = configurations.getConfiguration();
46
47         for (ConfigurationType configurationType : this.getSavedConfigurations()) {
48             configurationTypes.add(configurationType);
49         }
50
51         ConfigurationType configurationType = new ConfigurationType();
52         configurationType.name = configurationName;
53
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);
62
63         configurationTypes.add(configurationType);
64
65         configurations.setConfiguration(configurationTypes);
66         jaxbMarshaller.marshal(configurations, file);
67
68     }
69
70     @Override
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);
75
76         JAXBContext jc = JAXBContext.newInstance(Configurations.class);
77
78         Unmarshaller unmarshaller = jc.createUnmarshaller();
79         unmarshaller.setSchema(schema);
80
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);
83
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;
90             }
91         }
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();
104         } else {
105             LOG.warn("Configuration {} not found. Using default parameters.", configurationName);
106         }
107
108         return params;
109     }
110
111     private List<ConfigurationType> getSavedConfigurations() throws SAXException, JAXBException{
112
113         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
114         Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
115
116         JAXBContext jc = JAXBContext.newInstance(Configurations.class);
117
118         Unmarshaller unmarshaller = jc.createUnmarshaller();
119         unmarshaller.setSchema(schema);
120
121         Configurations configurations = (Configurations) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
122
123         return configurations.getConfiguration();
124
125     }
126 }