fix for BUG 661
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / droptest / DropTestRpcSender.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.droptest
9
10 import java.math.BigInteger
11 import java.util.ArrayList
12 import java.util.Arrays
13 import org.apache.commons.codec.binary.Hex
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
37 import org.slf4j.LoggerFactory
38
39 class DropTestRpcSender implements PacketProcessingListener {
40     static val LOG = LoggerFactory.getLogger(DropTestProvider);
41     @Property
42     val DropTestRpcProvider manager;
43
44     @Property
45     val SalFlowService flowService;
46
47     new(DropTestRpcProvider manager,SalFlowService flowService) {
48         _manager = manager;
49         _flowService = flowService;
50     }
51     
52     override onPacketReceived(PacketReceived notification) {
53         LOG.debug("onPacketReceived - Entering - " + notification);
54         val ncr = notification.ingress // Get the Ingress nodeConnectorRef
55         val ncri = (ncr.value as InstanceIdentifier<NodeConnector>); // Get the instance identifier for the nodeConnectorRef
56         val ncKey = InstanceIdentifier.keyOf(ncri);
57         val nodeInstanceId = ncri.firstIdentifierOf(Node); // Get the instanceID for the Node in the tree above us
58         val nodeKey = InstanceIdentifier.keyOf(nodeInstanceId);
59         val rawPacket = notification.payload;
60         LOG.debug("onPacketReceived - received Packet on Node {} and NodeConnector {} payload {}",nodeKey.id,ncKey.id,Hex.encodeHexString(rawPacket));
61         val srcMac = Arrays.copyOfRange(rawPacket,6,12);
62         LOG.debug("onPacketReceived - received Packet on Node {} and NodeConnector {} srcMac {}",nodeKey.id,ncKey.id,Hex.encodeHexString(srcMac));
63         
64         val match = new MatchBuilder();
65         val ethernetMatch = new EthernetMatchBuilder();
66         val ethSourceBuilder = new EthernetSourceBuilder();
67         //TODO: use HEX, use binary form
68         //Hex.decodeHex("000000000001".toCharArray());
69         ethSourceBuilder.setAddress(new MacAddress(DropTestUtils.macToString(srcMac)));
70         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
71         match.setEthernetMatch(ethernetMatch.build());
72         val dab = new DropActionBuilder();
73         val dropAction = dab.build();
74         val ab = new ActionBuilder();
75         ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
76         
77         // Add our drop action to a list
78         val actionList = new ArrayList<Action>();
79         actionList.add(ab.build());
80         
81         // Create an Apply Action
82         val aab = new ApplyActionsBuilder();
83         aab.setAction(actionList);
84         
85         // Wrap our Apply Action in an Instruction
86         val ib = new InstructionBuilder();
87         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
88         
89         // Put our Instruction in a list of Instructions
90         val isb = new InstructionsBuilder();
91         val instructions = new ArrayList<Instruction>();
92         instructions.add(ib.build());
93         isb.setInstruction(instructions);
94         
95         // Finally build our flow
96         val fb = new AddFlowInputBuilder
97         fb.setMatch(match.build());
98         fb.setInstructions(isb.build());
99         //fb.setId(new FlowId(Long.toString(fb.hashCode)));
100
101         // Construct the flow instance id
102         val flowInstanceId = InstanceIdentifier.builder(Nodes) // File under nodes
103             .child(Node,nodeKey).toInstance // A particular node indentified by nodeKey        
104         fb.setNode(new NodeRef(flowInstanceId));
105
106         fb.setPriority(4);
107         fb.setBufferId(0L);
108         val value = new BigInteger("10", 10);
109         fb.setCookie(value);
110         fb.setCookieMask(value);
111         fb.setTableId(0 as short);
112         fb.setHardTimeout(300);
113         fb.setIdleTimeout(240);
114         fb.setFlags(new FlowModFlags(false, false, false, false, false));
115         
116         // Construct the flow instance id
117         flowService.addFlow(fb.build());
118     }
119     
120 }