BUG 2661 - sonar issues in test-common artifact
[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 static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.BUFFER_ID;
11 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.HARD_TIMEOUT;
12 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.IDLE_TIMEOUT;
13 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.PRIORITY;
14 import static org.opendaylight.openflowplugin.testcommon.AbstractDropTest.TABLE_ID;
15
16 import java.math.BigInteger;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
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.opendaylight.yangtools.yang.binding.NotificationListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
37  * device using {@link SalFlowService} strategy
38  */
39 public class DropTestRpcSender extends AbstractDropTest {
40     private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
41
42     private SalFlowService flowService;
43
44     /**
45      * @param flowService the flowService to set
46      */
47     public void setFlowService(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 NotificationProviderService notificationService;
72
73     private ListenerRegistration<NotificationListener> notificationRegistration;
74
75     /**
76      * start listening on packetIn
77      */
78     public void start() {
79         notificationRegistration = notificationService.registerNotificationListener(this);
80     }
81
82     @Override
83     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
84         final AddFlowInputBuilder fb = BUILDER.get();
85
86         // Finally build our flow
87         fb.setMatch(match);
88         fb.setInstructions(instructions);
89         //fb.setId(new FlowId(Long.toString(fb.hashCode)));
90
91         // Construct the flow instance id
92         final InstanceIdentifier<Node> flowInstanceId = InstanceIdentifier
93                 // File under nodes
94                 .builder(Nodes.class)
95                 // A particular node identified by nodeKey
96                 .child(Node.class, node).build();
97         fb.setNode(new NodeRef(flowInstanceId));
98
99         // Add flow
100         AddFlowInput flow = fb.build();
101         if (LOG.isDebugEnabled()) {
102             LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
103         }
104         flowService.addFlow(flow);
105     }
106
107     /**
108      * @param notificationService
109      */
110     public void setNotificationService(NotificationProviderService notificationService) {
111         this.notificationService = notificationService;
112     }
113
114     @Override
115     public void close() {
116         try {
117             LOG.debug("DropTestProvider stopped.");
118             if (notificationRegistration != null) {
119                 notificationRegistration.close();
120             }
121         } catch (Exception e) {
122             LOG.error("unregistration of notification listener failed", e);
123         }
124     }
125 }