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 static final AtomicLong ID_COUNTER = new AtomicLong();
43     private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
44         final var cookie = new FlowCookie(Uint64.TEN);
45         return new FlowBuilder()
46             .setPriority(PRIORITY)
47             .setBufferId(BUFFER_ID)
48             .setCookie(cookie)
49             .setCookieMask(cookie)
50             .setTableId(TABLE_ID)
51             .setHardTimeout(HARD_TIMEOUT)
52             .setIdleTimeout(IDLE_TIMEOUT)
53             .setFlags(new FlowModFlags(false, false, false, false, false));
54     });
55
56     private NotificationService notificationService = null;
57     private Registration notificationRegistration = null;
58     private DataBroker dataService = null;
59
60     /**
61      * start listening on packetIn.
62      */
63     public void start() {
64         notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
65     }
66
67     public void setDataService(final DataBroker dataService) {
68         this.dataService = dataService;
69     }
70
71     @Override
72     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
73             final Instructions instructions) {
74
75         // Finally build our flow
76         final FlowBuilder fb = BUILDER.get();
77         fb.setMatch(match);
78         fb.setInstructions(instructions);
79         fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
80
81         // Construct the flow instance id
82         final InstanceIdentifier<Flow> flowInstanceId = node.builder()
83                 // That is flow capable, only FlowCapableNodes have tables
84                 .augmentation(FlowCapableNode.class)
85                 // In the table identified by TableKey
86                 .child(Table.class, ZERO_TABLE)
87                 // A flow identified by flowKey
88                 .child(Flow.class, new FlowKey(fb.getId()))
89                 .build();
90
91         final Flow flow = fb.build();
92         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
93
94         if (LOG.isDebugEnabled()) {
95             LOG.debug("onPacketReceived - About to write flow {}", flow);
96         }
97         transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
98         transaction.commit();
99         LOG.debug("onPacketReceived - About to write flow commited");
100     }
101
102     @Override
103     public void close() {
104         super.close();
105         LOG.debug("DropTestProvider stopped.");
106         if (notificationRegistration != null) {
107             notificationRegistration.close();
108         }
109     }
110
111     public void setNotificationService(final NotificationService notificationService) {
112         this.notificationService = notificationService;
113     }
114 }