2 * Copyright (c) 2016, 2017 Ericsson Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.bulk.o.matic;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class FlowWriterSequential implements FlowCounterMBean {
26 private static final Logger LOG = LoggerFactory.getLogger(FlowWriterSequential.class);
27 private final DataBroker dataBroker;
28 private final ExecutorService flowPusher;
29 protected int dpnCount;
30 private long startTime;
31 private final AtomicInteger writeOpStatus = new AtomicInteger(FlowCounter.OperationStatus.INIT.status());
32 private final AtomicInteger countDpnWriteCompletion = new AtomicInteger();
33 private final AtomicLong taskCompletionTime = new AtomicLong();
35 public FlowWriterSequential(final DataBroker dataBroker, ExecutorService flowPusher) {
36 this.dataBroker = dataBroker;
37 this.flowPusher = flowPusher;
38 LOG.info("Using Sequential implementation of Flow Writer.");
41 public void addFlows(Integer count, Integer flowsPerDPN, int batchSize, int sleepMillis, short startTableId,
42 short endTableId, boolean isCreateParents) {
43 LOG.info("Using Sequential implementation of Flow Writer.");
44 this.dpnCount = count;
45 countDpnWriteCompletion.set(count);
46 startTime = System.nanoTime();
47 for (int i = 1; i <= count; i++) {
48 FlowHandlerTask task = new FlowHandlerTask(Integer.toString(i), flowsPerDPN, true, batchSize, sleepMillis,
49 startTableId, endTableId, isCreateParents);
50 flowPusher.execute(task);
54 public void deleteFlows(Integer count, Integer flowsPerDPN, int batchSize, short startTableId,
56 LOG.info("Using Sequential implementation of Flow Writer.");
57 countDpnWriteCompletion.set(count);
58 for (int i = 1; i <= count; i++) {
59 FlowHandlerTask task = new FlowHandlerTask(Integer.toString(i), flowsPerDPN, false, batchSize, 0,
60 startTableId, endTableId, false);
61 flowPusher.execute(task);
66 public int getWriteOpStatus() {
67 return writeOpStatus.get();
71 public long getTaskCompletionTime() {
72 return taskCompletionTime.get();
75 private class FlowHandlerTask implements Runnable {
76 private final String dpId;
77 private final int flowsPerDpn;
78 private final boolean add;
79 private final int batchSize;
80 private final int sleepMillis;
81 private final short startTableId;
82 private final short endTableId;
83 private final boolean isCreateParents;
85 FlowHandlerTask(final String dpId,
86 final int flowsPerDpn,
90 final short startTableId,
91 final short endTableId,
92 final boolean isCreateParents) {
93 this.dpId = BulkOMaticUtils.DEVICE_TYPE_PREFIX + dpId;
95 this.flowsPerDpn = flowsPerDpn;
96 this.batchSize = batchSize;
97 this.sleepMillis = sleepMillis;
98 this.startTableId = startTableId;
99 this.endTableId = endTableId;
100 this.isCreateParents = isCreateParents;
105 LOG.info("Starting flow writer task for dpid: {}. Number of transactions: {}", dpId,
106 flowsPerDpn / batchSize);
107 writeOpStatus.set(FlowCounter.OperationStatus.IN_PROGRESS.status());
109 Short tableId = startTableId;
111 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
112 short calculatedTableId = tableId;
115 for (; sourceIp <= batchSize; sourceIp++) {
116 String flowId = "Flow-" + dpId + "." + calculatedTableId + "." + sourceIp;
117 LOG.debug("Adding flow with id: {}", flowId);
120 Match match = BulkOMaticUtils.getMatch(sourceIp);
121 flow = BulkOMaticUtils.buildFlow(calculatedTableId, flowId, match);
123 addFlowToTx(writeTransaction, flowId,
124 BulkOMaticUtils.getFlowInstanceIdentifier(calculatedTableId, flowId, dpId), flow);
126 if (sourceIp < batchSize) {
128 short numberB = (short) (endTableId - startTableId + 1);
129 calculatedTableId = (short) ((calculatedTableId + numberA) % numberB + startTableId);
133 LOG.debug("Submitting Txn for dpId: {}, begin tableId: {}, end tableId: {}, sourceIp: {}", dpId, tableId,
134 calculatedTableId, sourceIp);
136 Futures.addCallback(writeTransaction.submit(), new DsCallBack(dpId, sourceIp, calculatedTableId),
137 MoreExecutors.directExecutor());
140 private void addFlowToTx(WriteTransaction writeTransaction, String flowId, InstanceIdentifier<Flow> flowIid,
143 LOG.trace("Adding flow for flowId: {}, flowIid: {}", flowId, flowIid);
144 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, flowIid, flow, isCreateParents);
146 LOG.trace("Deleting flow for flowId: {}, flowIid: {}", flowId, flowIid);
147 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, flowIid);
151 private class DsCallBack implements FutureCallback<Void> {
152 private final String dpId;
153 private int sourceIp;
154 private final Short tableId;
156 DsCallBack(String dpId, int sourceIp, Short tableId) {
158 this.sourceIp = sourceIp;
160 short numberB = (short) (endTableId - startTableId + 1);
161 this.tableId = (short) ((tableId + numberA) % numberB + startTableId);
165 public void onSuccess(Void notUsed) {
166 if (sourceIp > flowsPerDpn) {
167 long dur = System.nanoTime() - startTime;
168 LOG.info("Completed all flows installation for: dpid: {}, tableId: {}, sourceIp: {} in {}ns", dpId,
169 tableId, sourceIp, dur);
170 if (0 == countDpnWriteCompletion.decrementAndGet()
171 && writeOpStatus.get() != FlowCounter.OperationStatus.FAILURE.status()) {
172 writeOpStatus.set(FlowCounter.OperationStatus.SUCCESS.status());
173 taskCompletionTime.set(dur);
178 if (sleepMillis > 0) {
179 Thread.sleep(sleepMillis);
181 } catch (InterruptedException e) {
182 LOG.error("Writer Thread Interrupted while sleeping: {}", e.getMessage());
185 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
186 int newBatchSize = sourceIp + batchSize - 1;
187 short calculatedTableId = tableId;
188 for (; sourceIp <= newBatchSize; sourceIp++) {
189 String flowId = "Flow-" + dpId + "." + calculatedTableId + "." + sourceIp;
192 Match match = BulkOMaticUtils.getMatch(sourceIp);
193 flow = BulkOMaticUtils.buildFlow(calculatedTableId, flowId, match);
195 LOG.debug("Adding flow with id: {}", flowId);
196 addFlowToTx(writeTransaction, flowId,
197 BulkOMaticUtils.getFlowInstanceIdentifier(calculatedTableId, flowId, dpId),
200 if (sourceIp < newBatchSize) {
202 short numberB = (short) (endTableId - startTableId + 1);
203 calculatedTableId = (short) ((calculatedTableId + numberA) % numberB + startTableId);
206 LOG.debug("OnSuccess: Submitting Txn for dpId: {}, begin tableId: {}, end tableId: {}, sourceIp: {}",
207 dpId, tableId, calculatedTableId, sourceIp);
208 Futures.addCallback(writeTransaction.submit(), new DsCallBack(dpId, sourceIp, calculatedTableId),
209 MoreExecutors.directExecutor());
213 public void onFailure(Throwable error) {
214 LOG.error("Error: {} in Datastore write operation: dpid: {}, tableId: {}, sourceIp: {}", error, dpId,
216 writeOpStatus.set(FlowCounter.OperationStatus.FAILURE.status());