3e01cfa543a758bb21b602ff3aa4d5cfbc1a5c96
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ScenarioFactory.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.openflowjava.protocol.impl.clients;
10
11 import java.io.IOException;
12 import java.util.ArrayDeque;
13 import java.util.Deque;
14 import java.util.Map;
15
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.xml.sax.SAXException;
18
19 import javax.xml.bind.JAXBException;
20
21 /**
22  * Class for providing prepared handshake scenario
23  *
24  * @author michal.polkorab
25  */
26 public final class ScenarioFactory {
27
28     private ScenarioFactory() {
29         throw new UnsupportedOperationException("Utility class shouldn't be instantiated");
30     }
31
32     /**
33      * Creates stack with handshake needed messages. XID of messages:
34      * <ol>
35      *   <li> hello sent - 00000001
36      *   <li> hello waiting - 00000002
37      *   <li> features request waiting - 00000003
38      *   <li> features reply sent - 00000003
39      * </ol>
40      * @return stack filled with Handshake messages
41      */
42     public static Deque<ClientEvent> createHandshakeScenario() {
43         Deque<ClientEvent> stack = new ArrayDeque<>();
44         stack.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 00 00 08 00 00 00 01")));
45         stack.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 00 00 08 00 00 00 02")));
46         stack.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 05 00 08 00 00 00 03")));
47         stack.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 06 00 20 00 00 00 03 "
48                 + "00 01 02 03 04 05 06 07 00 01 02 03 01 00 00 00 00 01 02 03 00 01 02 03")));
49         return stack;
50     }
51
52     /**
53      * Creates stack with handshake needed messages. XID of messages:
54      * <ol>
55      *   <li> hello sent - 00000001
56      *   <li> hello waiting - 00000021
57      *   <li> features request waiting - 00000002
58      *   <li> features reply sent - 00000002
59      * </ol>
60      * @return stack filled with Handshake messages
61      */
62     public static Deque<ClientEvent> createHandshakeScenarioWithBarrier() {
63         Deque<ClientEvent> stack = new ArrayDeque<>();
64         stack.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 00 00 08 00 00 00 01")));
65         stack.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 00 00 10 00 00 00 15 00 01 00 08 00 00 00 12"))); //Hello message 21
66         stack.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 05 00 08 00 00 00 02")));
67         stack.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 06 00 20 00 00 00 02 "
68                 + "00 01 02 03 04 05 06 07 00 01 02 03 01 00 00 00 00 01 02 03 00 01 02 03")));
69         stack.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 14 00 08 00 00 00 00"))); //Barrier request
70         stack.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 15 00 08 00 00 00 04"))); //Barrier reply
71         return stack;
72     }
73
74     /**
75      * Creates stack from XML file
76      * @return stack filled with Handshake messages
77      */
78     public static Deque<ClientEvent> getScenarioFromXml(String scenarioName, String scenarioFile) throws JAXBException, SAXException, IOException {
79         ScenarioService scenarioService = new ScenarioServiceImpl(scenarioFile);
80         Deque<ClientEvent> stack = new ArrayDeque<>();
81         for (Map.Entry<Integer, ClientEvent> clientEvent : scenarioService.getEventsFromScenario(scenarioService.unMarshallData(scenarioName)).entrySet()) {
82             stack.addFirst(clientEvent.getValue());
83         }
84         return stack;
85     }
86
87     /**
88      * Creates stack with handshake needed messages. XID of messages:
89      * <ol>
90      *   <li> hello sent - 00000001
91      *   <li> hello waiting - 00000002
92      *   <li> features request waiting - 00000003
93      *   <li> features reply sent - 00000003
94      * </ol>
95      * @param auxiliaryId auxiliaryId wanted in featuresReply message
96      * @return stack filled with Handshake messages (featuresReply with auxiliaryId set)
97      */
98     public static Deque<ClientEvent> createHandshakeScenarioWithAuxiliaryId(byte auxiliaryId) {
99         Deque<ClientEvent> queue = new ArrayDeque<>();
100         queue.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 00 00 08 00 00 00 01")));
101         queue.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 00 00 08 00 00 00 02")));
102         queue.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 05 00 08 00 00 00 03")));
103         queue.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 06 00 20 00 00 00 03 "
104                 + "00 01 02 03 04 05 06 07 00 01 02 03 01 " + String.format("%02x ", auxiliaryId) + " 00 00 00 01 02 03 00 01 02 03")));
105         return queue;
106     }
107
108 }