Merge "added initial waiting for mdsal in dropTest"
[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 java.math.BigInteger;
11 import java.util.concurrent.Callable;
12 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
13 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.NotificationListener;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
33  * device using {@link SalFlowService} strategy
34  */
35 public class DropTestRpcSender extends AbstractDropTest {
36     private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
37
38     private SalFlowService flowService;
39
40     /**
41      * @param flowService the flowService to set
42      */
43     public void setFlowService(SalFlowService flowService) {
44         this.flowService = flowService;
45     }
46
47     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
48         @Override
49         protected AddFlowInputBuilder initialValue() {
50             final AddFlowInputBuilder fb = new AddFlowInputBuilder();
51
52             fb.setPriority(PRIORITY);
53             fb.setBufferId(BUFFER_ID);
54
55             final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
56             fb.setCookie(cookie);
57             fb.setCookieMask(cookie);
58             fb.setTableId(TABLE_ID);
59             fb.setHardTimeout(HARD_TIMEOUT);
60             fb.setIdleTimeout(IDLE_TIMEOUT);
61             fb.setFlags(new FlowModFlags(false, false, false, false, false));
62
63             return fb;
64         }
65     };
66
67     private NotificationProviderService notificationService;
68
69     private ListenerRegistration<NotificationListener> notificationRegistration;
70
71     /**
72      * start listening on packetIn
73      */
74     public void start() {
75         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
76                 STARTUP_LOOP_MAX_RETRIES);
77         try {
78             notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<NotificationListener>>() {
79                 @Override
80                 public ListenerRegistration<NotificationListener> call() throws Exception {
81                     return notificationService.registerNotificationListener(DropTestRpcSender.this);
82                 }
83             });
84         } catch (Exception e) {
85             LOG.warn("DropTest sender notification listener registration fail!");
86             LOG.debug("DropTest sender notification listener registration fail! ..", e);
87             throw new IllegalStateException("DropTest startup fail! Try again later.", e);
88         }
89     }
90
91     @Override
92     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
93         final AddFlowInputBuilder fb = BUILDER.get();
94
95         // Finally build our flow
96         fb.setMatch(match);
97         fb.setInstructions(instructions);
98         //fb.setId(new FlowId(Long.toString(fb.hashCode)));
99
100         // Construct the flow instance id
101         final InstanceIdentifier<Node> flowInstanceId = InstanceIdentifier
102                 // File under nodes
103                 .builder(Nodes.class)
104                 // A particular node identified by nodeKey
105                 .child(Node.class, node).build();
106         fb.setNode(new NodeRef(flowInstanceId));
107
108         // Add flow
109         AddFlowInput flow = fb.build();
110         if (LOG.isDebugEnabled()) {
111             LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
112         }
113         flowService.addFlow(flow);
114     }
115
116     /**
117      * @param notificationService
118      */
119     public void setNotificationService(NotificationProviderService notificationService) {
120         this.notificationService = notificationService;
121     }
122
123     @Override
124     public void close() {
125         try {
126             LOG.debug("DropTestProvider stopped.");
127             if (notificationRegistration != null) {
128                 notificationRegistration.close();
129             }
130         } catch (Exception e) {
131             LOG.error("unregistration of notification listener failed", e);
132         }
133     }
134 }