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