Fix bug on ByteBufUtils method serializeList
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ScenarioServiceImpl.java
1 package org.opendaylight.openflowjava.protocol.impl.clients;
2
3 import com.google.common.base.Preconditions;
4 import java.io.File;
5 import java.io.IOException;
6 import java.util.SortedMap;
7 import java.util.TreeMap;
8 import javax.xml.XMLConstants;
9 import javax.xml.bind.JAXBContext;
10 import javax.xml.bind.JAXBException;
11 import javax.xml.bind.Unmarshaller;
12 import javax.xml.validation.Schema;
13 import javax.xml.validation.SchemaFactory;
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.xml.sax.SAXException;
18
19 /**
20  * @author Jozef Bacigal
21  *         Date: 9.3.2016
22  */
23 public class ScenarioServiceImpl implements ScenarioService {
24
25     private static final Logger LOG = LoggerFactory.getLogger(ScenarioServiceImpl.class);
26
27     private String XML_FILE_PATH_WITH_FILE_NAME = SIMPLE_CLIENT_SRC_MAIN_RESOURCES + SCENARIO_XML;
28
29     public ScenarioServiceImpl(String scenarioFile){
30         if (null != scenarioFile && !scenarioFile.isEmpty()) {
31             this.XML_FILE_PATH_WITH_FILE_NAME = scenarioFile;
32         }
33     }
34
35     @Override
36     public Scenario unMarshallData(String scenarioName) throws SAXException, JAXBException {
37         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
38         Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
39         LOG.debug("Loading schema from: {}", XSD_SCHEMA_PATH_WITH_FILE_NAME);
40
41         JAXBContext jc = JAXBContext.newInstance(Scenarios.class);
42
43         Unmarshaller unmarshaller = jc.createUnmarshaller();
44         unmarshaller.setSchema(schema);
45
46         Scenarios scenarios = (Scenarios) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
47         LOG.debug("Scenarios ({}) are un-marshaled from {}", scenarios.getScenario().size(), XML_FILE_PATH_WITH_FILE_NAME);
48
49         boolean foundConfiguration = false;
50         Scenario scenarioType = null;
51         for (Scenario scenario : scenarios.getScenario()) {
52             if (scenario.getName().equals(scenarioName)) {
53                 scenarioType = scenario;
54                 foundConfiguration = true;
55             }
56         }
57         if (!foundConfiguration) {
58             LOG.warn("Scenario {} not found.", scenarioName);
59         } else {
60             LOG.info("Scenario {} found with {} steps.", scenarioName, scenarioType.getStep().size());
61         }
62         return scenarioType;
63     }
64
65     @Override
66     public SortedMap<Integer, ClientEvent> getEventsFromScenario(Scenario scenario) throws IOException {
67         Preconditions.checkNotNull(scenario, "Scenario name not found. Check XML file, scenario name or directories.");
68         SortedMap<Integer, ClientEvent> events = new TreeMap<>();
69         Integer counter = 0;
70         for (Step step : scenario.getStep()) {
71             LOG.debug("Step {}: {}, type {}, bytes {}", step.getOrder(), step.getName(), step.getEvent().value(), step.getBytes().toArray());
72             switch (step.getEvent()) {
73                 case SLEEP_EVENT: events.put(counter++, new SleepEvent(1000)); break;
74                 case SEND_EVENT: events.put(counter++, new SendEvent(ByteBufUtils.serializeList(step.getBytes()))); break;
75                 case WAIT_FOR_MESSAGE_EVENT: events.put(counter++, new WaitForMessageEvent(ByteBufUtils.serializeList(step.getBytes()))); break;
76             }
77         }
78         return events;
79     }
80
81 }