Fix drop-test-karaf service injection
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / DropTestRpcSenderImpl.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.mdsal.binding.api.NotificationService;
19 import org.opendaylight.mdsal.binding.api.RpcService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
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.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
30 import org.opendaylight.yangtools.concepts.Registration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.Uint64;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Deactivate;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to device using
43  * {@link AddFlow} strategy.
44  */
45 @Singleton
46 @Component(service = DropTestRpcSender.class, immediate = true)
47 public final class DropTestRpcSenderImpl extends AbstractDropTest implements DropTestRpcSender {
48     private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSenderImpl.class);
49     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
50         final var cookie = new FlowCookie(Uint64.TEN);
51         return new AddFlowInputBuilder()
52             .setPriority(PRIORITY)
53             .setBufferId(BUFFER_ID)
54             .setCookie(cookie)
55             .setCookieMask(cookie)
56             .setTableId(TABLE_ID)
57             .setHardTimeout(HARD_TIMEOUT)
58             .setIdleTimeout(IDLE_TIMEOUT)
59             .setFlags(new FlowModFlags(false, false, false, false, false));
60     });
61
62     private final NotificationService notificationService;
63     private final AddFlow addFlow;
64
65     private Registration reg = null;
66
67     @Inject
68     @Activate
69     public DropTestRpcSenderImpl(@Reference final NotificationService notificationService,
70             @Reference final RpcService rpcService) {
71         this.notificationService = requireNonNull(notificationService);
72         addFlow = rpcService.getRpc(AddFlow.class);
73     }
74
75     @PreDestroy
76     @Deactivate
77     @Override
78     public void close() {
79         stop();
80         super.close();
81         LOG.debug("DropTestProvider terminated");
82     }
83
84     @Override
85     public synchronized boolean start() {
86         if (reg != null) {
87             return false;
88         }
89         reg = notificationService.registerListener(PacketReceived.class, this);
90         LOG.debug("DropTestProvider started");
91         return true;
92     }
93
94     @Override
95     public synchronized boolean stop() {
96         if (reg == null) {
97             return false;
98         }
99         reg.close();
100         reg = null;
101         LOG.debug("DropTestProvider stopped");
102         return true;
103     }
104
105     @Override
106     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
107             final Instructions instructions) {
108         final AddFlowInputBuilder fb = BUILDER.get();
109
110         // Finally build our flow
111         fb.setMatch(match);
112         fb.setInstructions(instructions);
113
114         // Construct the flow instance id
115
116         fb.setNode(new NodeRef(node));
117
118         // Add flow
119         final var flow = fb.build();
120         LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
121         Futures.addCallback(addFlow.invoke(flow), new FutureCallback<RpcResult<AddFlowOutput>>() {
122             @Override
123             public void onSuccess(final RpcResult<AddFlowOutput> result) {
124                 countFutureSuccess();
125             }
126
127             @Override
128             public void onFailure(final Throwable throwable) {
129                 countFutureError();
130             }
131         }, MoreExecutors.directExecutor());
132     }
133 }