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