1 package org.opendaylight.openflowjava.protocol.impl.clients;
3 import com.google.common.base.Preconditions;
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;
20 * @author Jozef Bacigal
23 public class ScenarioServiceImpl implements ScenarioService {
25 private static final Logger LOG = LoggerFactory.getLogger(ScenarioServiceImpl.class);
27 private String XML_FILE_PATH_WITH_FILE_NAME = SIMPLE_CLIENT_SRC_MAIN_RESOURCES + SCENARIO_XML;
29 public ScenarioServiceImpl(String scenarioFile){
30 if (null != scenarioFile && !scenarioFile.isEmpty()) {
31 this.XML_FILE_PATH_WITH_FILE_NAME = scenarioFile;
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);
41 JAXBContext jc = JAXBContext.newInstance(Scenarios.class);
43 Unmarshaller unmarshaller = jc.createUnmarshaller();
44 unmarshaller.setSchema(schema);
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);
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;
57 if (!foundConfiguration) {
58 LOG.warn("Scenario {} not found.", scenarioName);
60 LOG.info("Scenario {} found with {} steps.", scenarioName, scenarioType.getStep().size());
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<>();
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;