Merge OpenFlowJava repository into OpenFlowPlugin
[openflowplugin.git] / samples / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ScenarioServiceImpl.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.clients;
9
10 import com.google.common.base.Preconditions;
11 import java.io.File;
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;
25
26 /**
27  * @author Jozef Bacigal
28  *         Date: 9.3.2016
29  */
30 public class ScenarioServiceImpl implements ScenarioService {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ScenarioServiceImpl.class);
33
34     private String XML_FILE_PATH_WITH_FILE_NAME = SIMPLE_CLIENT_SRC_MAIN_RESOURCES + SCENARIO_XML;
35
36     public ScenarioServiceImpl(String scenarioFile){
37         if (null != scenarioFile && !scenarioFile.isEmpty()) {
38             this.XML_FILE_PATH_WITH_FILE_NAME = scenarioFile;
39         }
40     }
41
42     @Override
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);
47
48         JAXBContext jc = JAXBContext.newInstance(Scenarios.class);
49
50         Unmarshaller unmarshaller = jc.createUnmarshaller();
51         unmarshaller.setSchema(schema);
52
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);
55
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;
62             }
63         }
64         if (!foundConfiguration) {
65             LOG.warn("Scenario {} not found.", scenarioName);
66         } else {
67             LOG.info("Scenario {} found with {} steps.", scenarioName, scenarioType.getStep().size());
68         }
69         return scenarioType;
70     }
71
72     @Override
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<>();
76         Integer counter = 0;
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;
83             }
84         }
85         return events;
86     }
87
88 }