Fix drop-test-karaf service injection
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / DropTestCommiterImpl.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 java.util.concurrent.atomic.AtomicLong;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.NotificationService;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
32 import org.opendaylight.yangtools.concepts.Registration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.Uint64;
35 import org.opendaylight.yangtools.yang.common.Uint8;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Deactivate;
39 import org.osgi.service.component.annotations.Reference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to device using dataStore
45  * strategy (FRM involved).
46  */
47 @Singleton
48 @Component(service = DropTestCommiter.class, immediate = true)
49 public final class DropTestCommiterImpl extends AbstractDropTest implements DropTestCommiter {
50     private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiterImpl.class);
51     private static final TableKey ZERO_TABLE = new TableKey(Uint8.ZERO);
52     private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
53         final var cookie = new FlowCookie(Uint64.TEN);
54         return new FlowBuilder()
55             .setPriority(PRIORITY)
56             .setBufferId(BUFFER_ID)
57             .setCookie(cookie)
58             .setCookieMask(cookie)
59             .setTableId(TABLE_ID)
60             .setHardTimeout(HARD_TIMEOUT)
61             .setIdleTimeout(IDLE_TIMEOUT)
62             .setFlags(new FlowModFlags(false, false, false, false, false));
63     });
64
65     private final AtomicLong idCounter = new AtomicLong();
66     private final DataBroker dataBroker;
67     private final NotificationService notificationService;
68
69     private Registration reg = null;
70
71     @Inject
72     @Activate
73     public DropTestCommiterImpl(@Reference final DataBroker dataBroker,
74             @Reference final NotificationService notificationService) {
75         this.dataBroker = requireNonNull(dataBroker);
76         this.notificationService = requireNonNull(notificationService);
77     }
78
79     @PreDestroy
80     @Deactivate
81     @Override
82     public void close() {
83         stop();
84         super.close();
85         LOG.debug("DropTestProvider terminated");
86     }
87
88     /**
89      * Start listening on packetIn.
90      *
91      * @return {@code false} if already started
92      */
93     @Override
94     public synchronized boolean start() {
95         if (reg != null) {
96             return false;
97         }
98         reg = notificationService.registerListener(PacketReceived.class, this);
99         LOG.debug("DropTestProvider started");
100         return true;
101     }
102
103     /**
104      * Stop listening on packetIn.
105      *
106      * @return {@code false} if already stopped
107      */
108     @Override
109     public synchronized boolean stop() {
110         if (reg == null) {
111             return false;
112         }
113         reg.close();
114         reg = null;
115         LOG.debug("DropTestProvider stopped");
116         return true;
117     }
118
119     @Override
120     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
121             final Instructions instructions) {
122
123         // Finally build our flow
124         final FlowBuilder fb = BUILDER.get();
125         fb.setMatch(match);
126         fb.setInstructions(instructions);
127         fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + idCounter.getAndIncrement()));
128
129         // Construct the flow instance id
130         final var flowInstanceId = node.builder()
131                 // That is flow capable, only FlowCapableNodes have tables
132                 .augmentation(FlowCapableNode.class)
133                 // In the table identified by TableKey
134                 .child(Table.class, ZERO_TABLE)
135                 // A flow identified by flowKey
136                 .child(Flow.class, new FlowKey(fb.getId()))
137                 .build();
138
139         final var flow = fb.build();
140         final var transaction = dataBroker.newReadWriteTransaction();
141
142         LOG.debug("onPacketReceived - About to write flow {}", flow);
143         transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
144         transaction.commit();
145         LOG.debug("onPacketReceived - About to write flow commited");
146     }
147 }