Droptest: optimize builder allocation
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / droptest / DropTestRpcSender.java
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
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class DropTestRpcSender extends AbstractDropTest {
27     private final static Logger LOG = LoggerFactory.getLogger(DropTestProvider.class);
28
29     private final SalFlowService flowService;
30
31     public DropTestRpcSender(final SalFlowService flowService) {
32         this.flowService = flowService;
33     }
34
35     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
36         @Override
37         protected AddFlowInputBuilder initialValue() {
38             final AddFlowInputBuilder fb = new AddFlowInputBuilder();
39
40             fb.setPriority(4);
41             fb.setBufferId(0L);
42
43             final FlowCookie cookie = new FlowCookie(BigInteger.valueOf(10));
44             fb.setCookie(cookie);
45             fb.setCookieMask(cookie);
46             fb.setTableId((short) 0);
47             fb.setHardTimeout(300);
48             fb.setIdleTimeout(240);
49             fb.setFlags(new FlowModFlags(false, false, false, false, false));
50
51             return fb;
52         }
53     };
54
55     @Override
56     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
57         final AddFlowInputBuilder fb = BUILDER.get();
58
59         // Finally build our flow
60         fb.setMatch(match);
61         fb.setInstructions(instructions);
62         //fb.setId(new FlowId(Long.toString(fb.hashCode)));
63
64         // Construct the flow instance id
65         final InstanceIdentifier<Node> flowInstanceId = InstanceIdentifier
66                 .builder(Nodes.class) // File under nodes
67                 .child(Node.class, node).toInstance(); // A particular node identified by nodeKey
68         fb.setNode(new NodeRef(flowInstanceId));
69
70         // Add flow
71         flowService.addFlow(fb.build());
72     }
73 }