Revert "WIP: Bump upstreams"
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / 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.testcommon;
9
10 import java.util.concurrent.atomic.AtomicLong;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.NotificationService;
13 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.Uint64;
31 import org.opendaylight.yangtools.yang.common.Uint8;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
37  * device using dataStore strategy (FRM involved).
38  */
39 public class DropTestCommiter extends AbstractDropTest {
40     private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
41     private static final TableKey ZERO_TABLE = new TableKey(Uint8.ZERO);
42     private DataBroker dataService;
43
44     private static final AtomicLong ID_COUNTER = new AtomicLong();
45
46     private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
47         final var cookie = new FlowCookie(Uint64.TEN);
48         return new FlowBuilder()
49             .setPriority(PRIORITY)
50             .setBufferId(BUFFER_ID)
51             .setCookie(cookie)
52             .setCookieMask(cookie)
53             .setTableId(TABLE_ID)
54             .setHardTimeout(HARD_TIMEOUT)
55             .setIdleTimeout(IDLE_TIMEOUT)
56             .setFlags(new FlowModFlags(false, false, false, false, false));
57     });
58
59     private NotificationService notificationService;
60
61     private Registration notificationRegistration;
62
63     /**
64      * start listening on packetIn.
65      */
66     public void start() {
67         notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
68     }
69
70     public void setDataService(final DataBroker dataService) {
71         this.dataService = dataService;
72     }
73
74     @Override
75     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
76             final Instructions instructions) {
77
78         // Finally build our flow
79         final FlowBuilder fb = BUILDER.get();
80         fb.setMatch(match);
81         fb.setInstructions(instructions);
82         fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
83
84         // Construct the flow instance id
85         final InstanceIdentifier<Flow> flowInstanceId = node.builder()
86                 // That is flow capable, only FlowCapableNodes have tables
87                 .augmentation(FlowCapableNode.class)
88                 // In the table identified by TableKey
89                 .child(Table.class, ZERO_TABLE)
90                 // A flow identified by flowKey
91                 .child(Flow.class, new FlowKey(fb.getId()))
92                 .build();
93
94         final Flow flow = fb.build();
95         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
96
97         if (LOG.isDebugEnabled()) {
98             LOG.debug("onPacketReceived - About to write flow {}", flow);
99         }
100         transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
101         transaction.commit();
102         LOG.debug("onPacketReceived - About to write flow commited");
103     }
104
105     @Override
106     public void close() {
107         super.close();
108         LOG.debug("DropTestProvider stopped.");
109         if (notificationRegistration != null) {
110             notificationRegistration.close();
111         }
112     }
113
114     public void setNotificationService(final NotificationService notificationService) {
115         this.notificationService = notificationService;
116     }
117 }