b4a4566490aa92d488cd724282eb9c52687d1f1e
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / DropTestRpcSender.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 com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import java.util.concurrent.Callable;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
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.NodeRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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 {@link SalFlowService} strategy
37  */
38 public class DropTestRpcSender extends AbstractDropTest {
39     private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
40     private static final InstanceIdentifier<Nodes> NODES_IDENTIFIER = InstanceIdentifier.create(Nodes.class);
41
42     private SalFlowService flowService;
43
44     /**
45      * @param flowService the flowService to set
46      */
47     public void setFlowService(final SalFlowService flowService) {
48         this.flowService = flowService;
49     }
50
51     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
52         @Override
53         protected AddFlowInputBuilder initialValue() {
54             final AddFlowInputBuilder fb = new AddFlowInputBuilder();
55
56             fb.setPriority(PRIORITY);
57             fb.setBufferId(BUFFER_ID);
58
59             final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
60             fb.setCookie(cookie);
61             fb.setCookieMask(cookie);
62             fb.setTableId(TABLE_ID);
63             fb.setHardTimeout(HARD_TIMEOUT);
64             fb.setIdleTimeout(IDLE_TIMEOUT);
65             fb.setFlags(new FlowModFlags(false, false, false, false, false));
66
67             return fb;
68         }
69     };
70
71     private NotificationService notificationService;
72
73     private ListenerRegistration<DropTestRpcSender> notificationRegistration;
74
75     /**
76      * start listening on packetIn
77      */
78     public void start() {
79         final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
80                 STARTUP_LOOP_MAX_RETRIES);
81         try {
82             notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DropTestRpcSender>>() {
83                 @Override
84                 public ListenerRegistration<DropTestRpcSender> call() throws Exception {
85                     return notificationService.registerNotificationListener(DropTestRpcSender.this);
86                 }
87             });
88         } catch (final Exception e) {
89             LOG.warn("DropTest sender notification listener registration fail!");
90             LOG.debug("DropTest sender notification listener registration fail! ..", e);
91             throw new IllegalStateException("DropTest startup fail! Try again later.", e);
92         }
93     }
94
95     @Override
96     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
97         final AddFlowInputBuilder fb = BUILDER.get();
98
99         // Finally build our flow
100         fb.setMatch(match);
101         fb.setInstructions(instructions);
102
103         // Construct the flow instance id
104         final InstanceIdentifier<Node> flowInstanceId = NODES_IDENTIFIER.child(Node.class, node);
105         fb.setNode(new NodeRef(flowInstanceId));
106
107         // Add flow
108         final AddFlowInput flow = fb.build();
109         if (LOG.isDebugEnabled()) {
110             LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
111         }
112         ListenableFuture result = JdkFutureAdapters.listenInPoolThread(flowService.addFlow(flow));
113         Futures.addCallback(result, new FutureCallback() {
114             @Override
115             public void onSuccess(final Object o) {
116                 countFutureSuccess();
117             }
118
119             @Override
120             public void onFailure(final Throwable throwable) {
121                 countFutureError();
122             }
123         });
124     }
125
126     /**
127      * @param notificationService
128      */
129     public void setNotificationService(final NotificationService notificationService) {
130         this.notificationService = notificationService;
131     }
132
133     @Override
134     public void close() {
135         super.close();
136         try {
137             LOG.debug("DropTestProvider stopped.");
138             if (notificationRegistration != null) {
139                 notificationRegistration.close();
140             }
141         } catch (final Exception e) {
142             LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
143             LOG.debug("unregistration of notification listener failed.. ", e);
144         }
145     }
146 }