lower log severity to warn where appropriate
[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.Callable;
12 import java.util.concurrent.atomic.AtomicLong;
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.openflowplugin.common.wait.SimpleTaskRetryLooper;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.NotificationListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
40  * device using dataStore strategy (FRM involved)
41  */
42 public class DropTestCommiter extends AbstractDropTest {
43     private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
44
45     private DataBroker dataService;
46
47     private static final AtomicLong ID_COUNTER = new AtomicLong();
48
49     private static final ThreadLocal<FlowBuilder> BUILDER = new ThreadLocal<FlowBuilder>() {
50         @Override
51         protected FlowBuilder initialValue() {
52             final FlowBuilder fb = new FlowBuilder();
53
54             fb.setPriority(PRIORITY);
55             fb.setBufferId(BUFFER_ID);
56             final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
57             fb.setCookie(cookie);
58             fb.setCookieMask(cookie);
59
60             fb.setTableId(TABLE_ID);
61             fb.setHardTimeout(HARD_TIMEOUT);
62             fb.setIdleTimeout(IDLE_TIMEOUT);
63             fb.setFlags(new FlowModFlags(false, false, false, false, false));
64             return fb;
65         }
66     };
67
68     private NotificationProviderService notificationService;
69
70     private ListenerRegistration<NotificationListener> notificationRegistration;
71
72     /**
73      * start listening on packetIn
74      */
75     public void start() {
76         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
77                 STARTUP_LOOP_MAX_RETRIES);
78         try {
79             notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<NotificationListener>>() {
80                 @Override
81                 public ListenerRegistration<NotificationListener> call() throws Exception {
82                     return notificationService.registerNotificationListener(DropTestCommiter.this);
83                 }
84             });
85         } catch (Exception e) {
86             LOG.warn("DropTest committer notification listener registration fail!");
87             LOG.debug("DropTest committer notification listener registration fail! ..", e);
88             throw new IllegalStateException("DropTest startup fail! Try again later.", e);
89         }
90     }
91
92     /**
93      * @param dataService the dataService to set
94      */
95     public void setDataService(DataBroker dataService) {
96         this.dataService = dataService;
97     }
98
99     @Override
100     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
101
102         // Finally build our flow
103         final FlowBuilder fb = BUILDER.get();
104         fb.setMatch(match);
105         fb.setInstructions(instructions);
106         fb.setId(new FlowId(String.valueOf(fb.hashCode()) +"."+ ID_COUNTER.getAndIncrement()));
107
108         // Construct the flow instance id
109         final InstanceIdentifier<Flow> flowInstanceId =
110                 // File under nodes
111                 InstanceIdentifier.builder(Nodes.class)
112                         // A particular node identified by nodeKey
113                         .child(Node.class, node)
114                         // That is flow capable, only FlowCapableNodes have tables
115                         .augmentation(FlowCapableNode.class)
116                         // In the table identified by TableKey
117                         .child(Table.class, new TableKey((short) 0))
118                         // A flow identified by flowKey
119                         .child(Flow.class, new FlowKey(fb.getId()))
120                         .build();
121
122         final Flow flow = fb.build();
123         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
124
125         if (LOG.isDebugEnabled()) {
126             LOG.debug("onPacketReceived - About to write flow {}", flow);
127         }
128         transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
129         transaction.submit();
130         LOG.debug("onPacketReceived - About to write flow commited");
131     }
132
133     @Override
134     public void close() {
135         try {
136             LOG.debug("DropTestProvider stopped.");
137             if (notificationRegistration != null) {
138                 notificationRegistration.close();
139             }
140         } catch (Exception e) {
141             LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
142             LOG.debug("unregistration of notification listener failed.. ", e);
143         }
144     }
145
146     /**
147      * @param notificationService
148      */
149     public void setNotificationService(NotificationProviderService notificationService) {
150         this.notificationService = notificationService;
151     }
152 }