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.base.Optional;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class FlowWriterDirectOFRpc {
42 private static final Logger LOG = LoggerFactory.getLogger(FlowWriterDirectOFRpc.class);
43 private final DataBroker dataBroker;
44 private final SalFlowService flowService;
45 private final ExecutorService flowPusher;
46 private static final long PAUSE_BETWEEN_BATCH_MILLIS = 40;
48 public FlowWriterDirectOFRpc(final DataBroker dataBroker, final SalFlowService salFlowService,
49 final ExecutorService flowPusher) {
50 this.dataBroker = dataBroker;
51 this.flowService = salFlowService;
52 this.flowPusher = flowPusher;
55 public void rpcFlowAdd(String dpId, int flowsPerDpn, int batchSize) {
56 if (!getAllNodes().isEmpty() && getAllNodes().contains(dpId)) {
57 FlowRPCHandlerTask addFlowRpcTask = new FlowRPCHandlerTask(dpId, flowsPerDpn, batchSize);
58 flowPusher.execute(addFlowRpcTask);
62 public void rpcFlowAddAll(int flowsPerDpn, int batchSize) {
63 Set<String> nodeIdSet = getAllNodes();
64 if (nodeIdSet.isEmpty()) {
65 LOG.warn("No nodes seen on OPERATIONAL DS. Aborting !!!!");
67 for (String dpId : nodeIdSet) {
68 LOG.info("Starting FlowRPCTaskHandler for switch id {}", dpId);
69 FlowRPCHandlerTask addFlowRpcTask = new FlowRPCHandlerTask(dpId, flowsPerDpn, batchSize);
70 flowPusher.execute(addFlowRpcTask);
75 private Set<String> getAllNodes() {
77 Set<String> nodeIds = new HashSet<>();
78 InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
79 ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();
82 Optional<Nodes> nodesDataNode = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodes)
84 if (nodesDataNode.isPresent()) {
85 List<Node> nodesCollection = nodesDataNode.get().getNode();
86 if (nodesCollection != null && !nodesCollection.isEmpty()) {
87 for (Node node : nodesCollection) {
88 LOG.info("Switch with ID {} discovered !!", node.getId().getValue());
89 nodeIds.add(node.getId().getValue());
92 return Collections.emptySet();
95 return Collections.emptySet();
97 } catch (ReadFailedException rdFailedException) {
98 LOG.error("Failed to read connected nodes {}", rdFailedException);
103 public class FlowRPCHandlerTask implements Runnable {
104 private final String dpId;
105 private final int flowsPerDpn;
106 private final int batchSize;
108 public FlowRPCHandlerTask(final String dpId, final int flowsPerDpn, final int batchSize) {
110 this.flowsPerDpn = flowsPerDpn;
111 this.batchSize = batchSize;
117 short tableId = (short) 1;
118 int initFlowId = 500;
120 for (int i = 1; i <= flowsPerDpn; i++) {
122 String flowId = Integer.toString(initFlowId + i);
124 LOG.debug("Framing AddFlowInput for flow-id {}", flowId);
126 Match match = BulkOMaticUtils.getMatch(i);
127 InstanceIdentifier<Node> nodeIId = BulkOMaticUtils.getFlowCapableNodeId(dpId);
128 InstanceIdentifier<Table> tableIId = BulkOMaticUtils.getTableId(tableId, dpId);
129 InstanceIdentifier<Flow> flowIId = BulkOMaticUtils.getFlowId(tableIId, flowId);
131 Flow flow = BulkOMaticUtils.buildFlow(tableId, flowId, match);
133 AddFlowInputBuilder builder = new AddFlowInputBuilder(flow);
134 builder.setNode(new NodeRef(nodeIId));
135 builder.setFlowTable(new FlowTableRef(tableIId));
136 builder.setFlowRef(new FlowRef(flowIId));
138 AddFlowInput addFlowInput = builder.build();
140 LOG.debug("RPC invocation for adding flow-id {} with input {}", flowId, addFlowInput.toString());
141 final Future<RpcResult<AddFlowOutput>> resultFuture = flowService.addFlow(addFlowInput);
142 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
144 if (i % batchSize == 0) {
146 LOG.info("Pausing for {} MILLISECONDS after batch of {} RPC invocations",
147 PAUSE_BETWEEN_BATCH_MILLIS, batchSize);
149 TimeUnit.MILLISECONDS.sleep(PAUSE_BETWEEN_BATCH_MILLIS);
150 } catch (InterruptedException iEx) {
151 LOG.error("Interrupted while pausing after batched push upto {} Ex ", i, iEx);