2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.protocol.impl.clients;
10 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.util.SortedMap;
14 import java.util.TreeMap;
15 import javax.xml.XMLConstants;
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.JAXBException;
18 import javax.xml.bind.Unmarshaller;
19 import javax.xml.validation.Schema;
20 import javax.xml.validation.SchemaFactory;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.xml.sax.SAXException;
27 * @author Jozef Bacigal
30 public class ScenarioServiceImpl implements ScenarioService {
32 private static final Logger LOG = LoggerFactory.getLogger(ScenarioServiceImpl.class);
34 private String XML_FILE_PATH_WITH_FILE_NAME = SIMPLE_CLIENT_SRC_MAIN_RESOURCES + SCENARIO_XML;
36 public ScenarioServiceImpl(String scenarioFile){
37 if (null != scenarioFile && !scenarioFile.isEmpty()) {
38 this.XML_FILE_PATH_WITH_FILE_NAME = scenarioFile;
43 public Scenario unMarshallData(String scenarioName) throws SAXException, JAXBException {
44 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
45 Schema schema = sf.newSchema(new File(XSD_SCHEMA_PATH_WITH_FILE_NAME));
46 LOG.debug("Loading schema from: {}", XSD_SCHEMA_PATH_WITH_FILE_NAME);
48 JAXBContext jc = JAXBContext.newInstance(Scenarios.class);
50 Unmarshaller unmarshaller = jc.createUnmarshaller();
51 unmarshaller.setSchema(schema);
53 Scenarios scenarios = (Scenarios) unmarshaller.unmarshal(new File(XML_FILE_PATH_WITH_FILE_NAME));
54 LOG.debug("Scenarios ({}) are un-marshaled from {}", scenarios.getScenario().size(), XML_FILE_PATH_WITH_FILE_NAME);
56 boolean foundConfiguration = false;
57 Scenario scenarioType = null;
58 for (Scenario scenario : scenarios.getScenario()) {
59 if (scenario.getName().equals(scenarioName)) {
60 scenarioType = scenario;
61 foundConfiguration = true;
64 if (!foundConfiguration) {
65 LOG.warn("Scenario {} not found.", scenarioName);
67 LOG.info("Scenario {} found with {} steps.", scenarioName, scenarioType.getStep().size());
73 public SortedMap<Integer, ClientEvent> getEventsFromScenario(Scenario scenario) throws IOException {
74 Preconditions.checkNotNull(scenario, "Scenario name not found. Check XML file, scenario name or directories.");
75 SortedMap<Integer, ClientEvent> events = new TreeMap<>();
77 for (Step step : scenario.getStep()) {
78 LOG.debug("Step {}: {}, type {}, bytes {}", step.getOrder(), step.getName(), step.getEvent().value(), step.getBytes().toArray());
79 switch (step.getEvent()) {
80 case SLEEP_EVENT: events.put(counter++, new SleepEvent(1000)); break;
81 case SEND_EVENT: events.put(counter++, new SendEvent(ByteBufUtils.serializeList(step.getBytes()))); break;
82 case WAIT_FOR_MESSAGE_EVENT: events.put(counter++, new WaitForMessageEvent(ByteBufUtils.serializeList(step.getBytes()))); break;