a45b9b5204f55b3a581e93bdf57d1e16d792816c
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / droptest / DropTestCommiter.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.controller.sal.binding.api.data.DataModificationTransaction;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
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.Instructions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Preconditions;
32
33 public class DropTestCommiter extends AbstractDropTest {
34     private final static Logger LOG = LoggerFactory.getLogger(DropTestProvider.class);
35
36     private static final ThreadLocal<FlowBuilder> BUILDER = new ThreadLocal<FlowBuilder>() {
37         @Override
38         protected FlowBuilder initialValue() {
39             final FlowBuilder fb = new FlowBuilder();
40
41             fb.setPriority(4);
42             fb.setBufferId(0L);
43             final FlowCookie cookie = new FlowCookie(BigInteger.valueOf(10));
44             fb.setCookie(cookie);
45             fb.setCookieMask(cookie);
46
47             fb.setTableId((short) 0);
48             fb.setHardTimeout(300);
49             fb.setIdleTimeout(240);
50             fb.setFlags(new FlowModFlags(false, false, false, false, false));
51             return fb;
52         }
53     };
54
55     private final DropTestProvider manager;
56
57     public DropTestCommiter(final DropTestProvider manager) {
58         this.manager = Preconditions.checkNotNull(manager);
59     }
60
61     @Override
62     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
63
64         // Finally build our flow
65         final FlowBuilder fb = BUILDER.get();
66         fb.setMatch(match);
67         fb.setInstructions(instructions);
68         fb.setId(new FlowId(String.valueOf(fb.hashCode())));
69
70         // Construct the flow instance id
71         final InstanceIdentifier<Flow> flowInstanceId =
72                 InstanceIdentifier.builder(Nodes.class) // File under nodes
73                         .child(Node.class, node) // A particular node identified by nodeKey
74                         .augmentation(FlowCapableNode.class) // That is flow capable, only FlowCapableNodes have tables
75                         .child(Table.class, new TableKey((short) 0)) // In the table identified by TableKey
76                         .child(Flow.class, new FlowKey(fb.getId())) // A flow identified by flowKey
77                         .build();
78
79         final Flow flow = fb.build();
80         final DataModificationTransaction transaction = manager.getDataService().beginTransaction();
81
82         LOG.debug("onPacketReceived - About to write flow {}", flow);
83         transaction.putConfigurationData(flowInstanceId, flow);
84         transaction.commit();
85         LOG.debug("onPacketReceived - About to write flow commited");
86     }
87 }