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 java.util.Collections;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.ReadTransaction;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
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.service.rev130819.AddFlowInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
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.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class FlowWriterDirectOFRpc {
39 private static final Logger LOG = LoggerFactory.getLogger(FlowWriterDirectOFRpc.class);
40 private final DataBroker dataBroker;
41 private final SalFlowService flowService;
42 private final ExecutorService flowPusher;
43 private static final long PAUSE_BETWEEN_BATCH_MILLIS = 40;
45 public FlowWriterDirectOFRpc(final DataBroker dataBroker, final SalFlowService salFlowService,
46 final ExecutorService flowPusher) {
47 this.dataBroker = dataBroker;
48 this.flowService = salFlowService;
49 this.flowPusher = flowPusher;
52 public void rpcFlowAdd(String dpId, int flowsPerDpn, int batchSize) {
53 if (!getAllNodes().isEmpty() && getAllNodes().contains(dpId)) {
54 FlowRPCHandlerTask addFlowRpcTask = new FlowRPCHandlerTask(dpId, flowsPerDpn, batchSize);
55 flowPusher.execute(addFlowRpcTask);
59 public void rpcFlowAddAll(int flowsPerDpn, int batchSize) {
60 Set<String> nodeIdSet = getAllNodes();
61 if (nodeIdSet.isEmpty()) {
62 LOG.warn("No nodes seen on OPERATIONAL DS. Aborting !!!!");
64 for (String dpId : nodeIdSet) {
65 LOG.info("Starting FlowRPCTaskHandler for switch id {}", dpId);
66 FlowRPCHandlerTask addFlowRpcTask = new FlowRPCHandlerTask(dpId, flowsPerDpn, batchSize);
67 flowPusher.execute(addFlowRpcTask);
72 private Set<String> getAllNodes() {
74 Set<String> nodeIds = new HashSet<>();
75 InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
77 try (ReadTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
78 Optional<Nodes> nodesDataNode = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodes).get();
79 if (nodesDataNode.isPresent()) {
80 List<Node> nodesCollection = nodesDataNode.get().getNode();
81 if (nodesCollection != null && !nodesCollection.isEmpty()) {
82 for (Node node : nodesCollection) {
83 LOG.info("Switch with ID {} discovered !!", node.getId().getValue());
84 nodeIds.add(node.getId().getValue());
87 return Collections.emptySet();
90 return Collections.emptySet();
92 } catch (InterruptedException | ExecutionException e) {
93 LOG.error("Failed to read connected nodes", e);
98 public class FlowRPCHandlerTask implements Runnable {
99 private final String dpId;
100 private final int flowsPerDpn;
101 private final int batchSize;
103 public FlowRPCHandlerTask(final String dpId, final int flowsPerDpn, final int batchSize) {
105 this.flowsPerDpn = flowsPerDpn;
106 this.batchSize = batchSize;
112 short tableId = (short) 1;
113 int initFlowId = 500;
115 for (int i = 1; i <= flowsPerDpn; i++) {
117 String flowId = Integer.toString(initFlowId + i);
119 LOG.debug("Framing AddFlowInput for flow-id {}", flowId);
121 Match match = BulkOMaticUtils.getMatch(i);
122 InstanceIdentifier<Node> nodeIId = BulkOMaticUtils.getFlowCapableNodeId(dpId);
123 InstanceIdentifier<Table> tableIId = BulkOMaticUtils.getTableId(tableId, dpId);
124 InstanceIdentifier<Flow> flowIId = BulkOMaticUtils.getFlowId(tableIId, flowId);
126 Flow flow = BulkOMaticUtils.buildFlow(tableId, flowId, match);
128 AddFlowInputBuilder builder = new AddFlowInputBuilder(flow);
129 builder.setNode(new NodeRef(nodeIId));
130 builder.setFlowTable(new FlowTableRef(tableIId));
131 builder.setFlowRef(new FlowRef(flowIId));
133 AddFlowInput addFlowInput = builder.build();
135 LOG.debug("RPC invocation for adding flow-id {} with input {}", flowId, addFlowInput);
136 LoggingFutures.addErrorLogging(flowService.addFlow(addFlowInput), LOG, "addFlow");
138 if (i % batchSize == 0) {
140 LOG.info("Pausing for {} MILLISECONDS after batch of {} RPC invocations",
141 PAUSE_BETWEEN_BATCH_MILLIS, batchSize);
143 TimeUnit.MILLISECONDS.sleep(PAUSE_BETWEEN_BATCH_MILLIS);
144 } catch (InterruptedException iEx) {
145 LOG.error("Interrupted while pausing after batched push upto {} Ex ", i, iEx);