Modernize test-common's use of NotificationService
[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.openflowplugin.common.wait.SimpleTaskRetryLooper;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
29 import org.opendaylight.yangtools.concepts.Registration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.Uint64;
32 import org.opendaylight.yangtools.yang.common.Uint8;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
38  * device using dataStore strategy (FRM involved).
39  */
40 public class DropTestCommiter extends AbstractDropTest {
41     private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
42     private static final TableKey ZERO_TABLE = new TableKey(Uint8.ZERO);
43     private DataBroker dataService;
44
45     private static final AtomicLong ID_COUNTER = new AtomicLong();
46
47     private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
48         final FlowBuilder fb = new FlowBuilder();
49
50         fb.setPriority(PRIORITY);
51         fb.setBufferId(BUFFER_ID);
52         final FlowCookie cookie = new FlowCookie(Uint64.TEN);
53         fb.setCookie(cookie);
54         fb.setCookieMask(cookie);
55
56         fb.setTableId(TABLE_ID);
57         fb.setHardTimeout(HARD_TIMEOUT);
58         fb.setIdleTimeout(IDLE_TIMEOUT);
59         fb.setFlags(new FlowModFlags(false, false, false, false, false));
60         return fb;
61     });
62
63     private NotificationService notificationService;
64
65     private Registration notificationRegistration;
66
67     /**
68      * start listening on packetIn.
69      */
70     @SuppressWarnings("checkstyle:IllegalCatch")
71     public void start() {
72         final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
73                 STARTUP_LOOP_MAX_RETRIES);
74         try {
75             notificationRegistration = looper.loopUntilNoException(
76                 () -> notificationService.registerListener(PacketReceived.class, this));
77         } catch (Exception e) {
78             LOG.warn("DropTest committer notification listener registration fail!");
79             LOG.debug("DropTest committer notification listener registration fail! ..", e);
80             throw new IllegalStateException("DropTest startup fail! Try again later.", e);
81         }
82     }
83
84     public void setDataService(final DataBroker dataService) {
85         this.dataService = dataService;
86     }
87
88     @Override
89     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
90             final Instructions instructions) {
91
92         // Finally build our flow
93         final FlowBuilder fb = BUILDER.get();
94         fb.setMatch(match);
95         fb.setInstructions(instructions);
96         fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
97
98         // Construct the flow instance id
99         final InstanceIdentifier<Flow> flowInstanceId = node.builder()
100                 // That is flow capable, only FlowCapableNodes have tables
101                 .augmentation(FlowCapableNode.class)
102                 // In the table identified by TableKey
103                 .child(Table.class, ZERO_TABLE)
104                 // A flow identified by flowKey
105                 .child(Flow.class, new FlowKey(fb.getId()))
106                 .build();
107
108         final Flow flow = fb.build();
109         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
110
111         if (LOG.isDebugEnabled()) {
112             LOG.debug("onPacketReceived - About to write flow {}", flow);
113         }
114         transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
115         transaction.commit();
116         LOG.debug("onPacketReceived - About to write flow commited");
117     }
118
119     @Override
120     @SuppressWarnings("checkstyle:IllegalCatch")
121     public void close() {
122         super.close();
123         try {
124             LOG.debug("DropTestProvider stopped.");
125             if (notificationRegistration != null) {
126                 notificationRegistration.close();
127             }
128         } catch (RuntimeException e) {
129             LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
130             LOG.debug("unregistration of notification listener failed.. ", e);
131         }
132     }
133
134     public void setNotificationService(final NotificationService notificationService) {
135         this.notificationService = notificationService;
136     }
137 }