53246ffd31eb299663212a67adc2cb3c2ae22fa2
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / outputtest / OutputTestUtil.xtend
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.openflowplugin.outputtest
9
10 import java.math.BigInteger
11 import java.util.ArrayList
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
45 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService
46 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext
47 import java.util.concurrent.ExecutionException
48 import org.apache.commons.lang.ArrayUtils
49
50 class OutputTestUtil {
51     
52     private new() {
53         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
54     }
55     
56     public static def buildTransmitInputPacket(String nodeId, byte[] packValue, String outPort, String inPort) {
57         
58         var list = new ArrayList<Byte>(40);
59         var msg = (new String("sendOutputMsg_TEST")).getBytes();
60         var index =0;
61         for (byte b : msg) {
62             list.add(b);
63             if(index < 7) {index = index+1} else {index = 0}
64         }
65         while(index < 8) {
66             list.add(new Byte("0"));
67             index = index+1;
68         }
69         
70         var ref = createNodeRef(nodeId);
71         var nEgressConfRef = new NodeConnectorRef(createNodeConnRef(nodeId, outPort));
72         var nIngressConRef = new NodeConnectorRef(createNodeConnRef(nodeId, inPort));
73         var tPackBuilder = new TransmitPacketInputBuilder
74         tPackBuilder.setPayload(ArrayUtils.toPrimitive(list));
75         tPackBuilder.setNode(ref);
76         // TODO VD P2 missing cookies in Test
77         tPackBuilder.setCookie(null);
78         tPackBuilder.setEgress(nEgressConfRef)
79         tPackBuilder.setIngress(nIngressConRef)
80         return tPackBuilder.build
81     }
82
83     public static def makePingFlowForNode(String nodeId, ProviderContext pc) {
84         var nodeBuilder = createNodeBuilder(nodeId)
85         var flowBuilder = createFlowBuilder(1235, null, "ping")
86         
87         var dataBrokerService = pc.getSALService(DataBrokerService)
88         var modif = dataBrokerService.beginTransaction
89         
90         var path = InstanceIdentifier
91                     .builder(Nodes)
92                     .child(Node, nodeBuilder.getKey)
93                     .augmentation(FlowCapableNode)
94                     .child(Table, new TableKey(flowBuilder.getTableId))
95                     .child(Flow, flowBuilder.getKey()).build;
96         
97         modif.putConfigurationData(path, flowBuilder.build)
98         var commitFuture = modif.commit
99         
100         try {
101             var resutl = commitFuture.get
102             var status = resutl.result
103             return "Status of Flow Data Loaded Transaction: " + status
104         } catch (InterruptedException e) {
105             e.printStackTrace();
106             return e.class.name
107         } catch (ExecutionException e) {
108             e.printStackTrace();
109             return e.class.name
110         }
111     }
112     
113     public static def createNodeRef(String nodeId) {
114         var key = new NodeKey(new NodeId(nodeId));
115         var path = InstanceIdentifier.builder(Nodes).child(Node, key).toInstance
116         return new NodeRef(path)
117     }
118     
119     public static def createNodeConnRef(String nodeId, String port) {
120         var sBuild = new StringBuilder(nodeId).append(":").append(port);
121         var nConKey = new NodeConnectorKey(new NodeConnectorId(sBuild.toString));
122         var path = InstanceIdentifier.builder(Nodes)
123                 .child(Node, new NodeKey(new NodeId(nodeId)))
124                 .child(NodeConnector, nConKey).toInstance()
125         return new NodeConnectorRef(path)
126     }
127     
128     
129     
130     private static def createNodeBuilder(String nodeId) {
131         var builder = new NodeBuilder()
132         builder.setId(new NodeId(nodeId))
133         builder.setKey(new NodeKey(builder.getId()))
134         return builder
135     }
136     
137     private static def createFlowBuilder(long flowId, String tableId, String flowName) {
138         var fBuild = new FlowBuilder();
139         fBuild.setMatch(new MatchBuilder().build)
140         fBuild.setInstructions(createPingInstructionsBuilder().build)
141         
142         var key = new FlowKey(new FlowId(Long.toString(flowId)));
143         fBuild.setBarrier(false);
144         // flow.setBufferId(new Long(12));
145         var value = new BigInteger("10", 10);
146         fBuild.setCookie(value);
147         fBuild.setCookieMask(value);
148         fBuild.setHardTimeout(0);
149         fBuild.setIdleTimeout(0);
150         fBuild.setInstallHw(false);
151         fBuild.setStrict(false);
152         fBuild.setContainerName(null);
153         fBuild.setFlags(new FlowModFlags(false, false, false, false, false));
154         fBuild.setId(new FlowId("12"));
155         fBuild.setTableId(checkTableId(tableId));
156         fBuild.setOutGroup(new Long(2));
157         fBuild.setOutPort(value);
158
159         fBuild.setKey(key);
160         fBuild.setPriority(2);
161         fBuild.setFlowName(flowName);
162         return fBuild
163     }
164
165     private static def createPingInstructionsBuilder() {
166         var aList = new ArrayList<Action>
167         var aBuild = new ActionBuilder
168
169         var output = new OutputActionBuilder
170         output.setMaxLength(56)
171         output.setOutputNodeConnector(new Uri("CONTROLLER"))
172         aBuild.setAction(new OutputActionCaseBuilder().setOutputAction(output.build).build)
173         aBuild.setOrder(0)
174         aBuild.setKey(new ActionKey(0))
175         aList.add(aBuild.build)
176         var asBuild = new ApplyActionsBuilder(); asBuild.setAction(aList)
177
178         var iBuild = new InstructionBuilder
179         iBuild.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(asBuild.build).build);
180         iBuild.setOrder(0);
181         iBuild.setKey(new InstructionKey(0));
182         
183         var instr = new ArrayList<Instruction>(); instr.add(iBuild.build)
184         return new InstructionsBuilder().setInstruction(instr) 
185     }
186     
187     private static def checkTableId(String tableId) {
188         try {
189             return Short.parseShort(tableId)
190         } catch (Exception ex) {
191             return Short.parseShort("2")
192         }
193     }
194 }