BUG-2661: Sonar issue
[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
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
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;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.NotificationListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to 
39  * device using dataStore strategy (FRM involved)
40  */
41 public class DropTestCommiter extends AbstractDropTest {
42     private final static Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
43     
44     private DataBroker dataService;
45     
46     private static final AtomicLong idCounter = new AtomicLong();
47
48     private static final ThreadLocal<FlowBuilder> BUILDER = new ThreadLocal<FlowBuilder>() {
49         @Override
50         protected FlowBuilder initialValue() {
51             final FlowBuilder fb = new FlowBuilder();
52
53             fb.setPriority(4);
54             fb.setBufferId(0L);
55             final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
56             fb.setCookie(cookie);
57             fb.setCookieMask(cookie);
58
59             fb.setTableId((short) 0);
60             fb.setHardTimeout(300);
61             fb.setIdleTimeout(240);
62             fb.setFlags(new FlowModFlags(false, false, false, false, false));
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         notificationRegistration = notificationService.registerNotificationListener(this);
76     }
77     
78     /**
79      * @param dataService the dataService to set
80      */
81     public void setDataService(DataBroker dataService) {
82         this.dataService = dataService;
83     }
84
85     @Override
86     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
87
88         // Finally build our flow
89         final FlowBuilder fb = BUILDER.get();
90         fb.setMatch(match);
91         fb.setInstructions(instructions);
92         fb.setId(new FlowId(String.valueOf(fb.hashCode()) +"."+ String.valueOf(idCounter.getAndIncrement())));
93         
94         // Construct the flow instance id
95         final InstanceIdentifier<Flow> flowInstanceId =
96                 InstanceIdentifier.builder(Nodes.class) // File under nodes
97                         .child(Node.class, node) // A particular node identified by nodeKey
98                         .augmentation(FlowCapableNode.class) // That is flow capable, only FlowCapableNodes have tables
99                         .child(Table.class, new TableKey((short) 0)) // In the table identified by TableKey
100                         .child(Flow.class, new FlowKey(fb.getId())) // A flow identified by flowKey
101                         .build();
102
103         final Flow flow = fb.build();
104         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
105
106         if (LOG.isDebugEnabled()) {
107             LOG.debug("onPacketReceived - About to write flow {}", flow);
108         }
109         transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
110         transaction.submit();
111         LOG.debug("onPacketReceived - About to write flow commited");
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
126     /**
127      * @param notificationService
128      */
129     public void setNotificationService(NotificationProviderService notificationService) {
130         this.notificationService = notificationService;
131     }
132 }