Do not use InstanceIdentifier.builder()
[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.NotificationService;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.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 static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
43
44     private DataBroker dataService;
45
46     private static final AtomicLong ID_COUNTER = 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(PRIORITY);
54             fb.setBufferId(BUFFER_ID);
55             final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
56             fb.setCookie(cookie);
57             fb.setCookieMask(cookie);
58
59             fb.setTableId(TABLE_ID);
60             fb.setHardTimeout(HARD_TIMEOUT);
61             fb.setIdleTimeout(IDLE_TIMEOUT);
62             fb.setFlags(new FlowModFlags(false, false, false, false, false));
63             return fb;
64         }
65     };
66
67     private NotificationService notificationService;
68
69     private ListenerRegistration<DropTestCommiter> notificationRegistration;
70
71     /**
72      * start listening on packetIn
73      */
74     public void start() {
75         final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
76                 STARTUP_LOOP_MAX_RETRIES);
77         try {
78             notificationRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DropTestCommiter>>() {
79                 @Override
80                 public ListenerRegistration<DropTestCommiter> call() throws Exception {
81                     return notificationService.registerNotificationListener(DropTestCommiter.this);
82                 }
83             });
84         } catch (final Exception e) {
85             LOG.warn("DropTest committer notification listener registration fail!");
86             LOG.debug("DropTest committer notification listener registration fail! ..", e);
87             throw new IllegalStateException("DropTest startup fail! Try again later.", e);
88         }
89     }
90
91     /**
92      * @param dataService the dataService to set
93      */
94     public void setDataService(final DataBroker dataService) {
95         this.dataService = dataService;
96     }
97
98     @Override
99     protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
100
101         // Finally build our flow
102         final FlowBuilder fb = BUILDER.get();
103         fb.setMatch(match);
104         fb.setInstructions(instructions);
105         fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
106
107         // Construct the flow instance id
108         final InstanceIdentifier<Flow> flowInstanceId =
109                 // File under nodes
110                 InstanceIdentifier.builder(Nodes.class)
111                         // A particular node identified by nodeKey
112                         .child(Node.class, node)
113                                 // That is flow capable, only FlowCapableNodes have tables
114                         .augmentation(FlowCapableNode.class)
115                                 // In the table identified by TableKey
116                         .child(Table.class, new TableKey((short) 0))
117                                 // A flow identified by flowKey
118                         .child(Flow.class, new FlowKey(fb.getId()))
119                         .build();
120
121         final Flow flow = fb.build();
122         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
123
124         if (LOG.isDebugEnabled()) {
125             LOG.debug("onPacketReceived - About to write flow {}", flow);
126         }
127         transaction.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
128         transaction.submit();
129         LOG.debug("onPacketReceived - About to write flow commited");
130     }
131
132     @Override
133     public void close() {
134         super.close();
135         try {
136             LOG.debug("DropTestProvider stopped.");
137             if (notificationRegistration != null) {
138                 notificationRegistration.close();
139             }
140         } catch (final 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(final NotificationService notificationService) {
150         this.notificationService = notificationService;
151     }
152 }