2 * Copyright (c) 2016 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.TimeUnit;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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.table.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
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.NodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class FlowWriterDirectOFRpc {
38 private static final Logger LOG = LoggerFactory.getLogger(FlowWriterDirectOFRpc.class);
39 private final DataBroker dataBroker;
40 private final SalFlowService flowService;
41 private final ExecutorService flowPusher;
42 private static final long PAUSE_BETWEEN_BATCH_MILLIS = 40;
44 public FlowWriterDirectOFRpc(final DataBroker dataBroker,
45 final SalFlowService salFlowService,
46 final ExecutorService flowPusher) {
47 this.dataBroker = dataBroker;
48 this.flowService = salFlowService;
49 this.flowPusher = flowPusher;
53 public void rpcFlowAdd(String dpId, int flowsPerDpn, int batchSize){
54 if (!getAllNodes().isEmpty() && getAllNodes().contains(dpId)) {
55 FlowRPCHandlerTask addFlowRpcTask = new FlowRPCHandlerTask(dpId, flowsPerDpn, batchSize);
56 flowPusher.execute(addFlowRpcTask);
60 public void rpcFlowAddAll(int flowsPerDpn, int batchSize){
61 Set<String> nodeIdSet = getAllNodes();
62 if (nodeIdSet.isEmpty()){
63 LOG.warn("No nodes seen on OPERATIONAL DS. Aborting !!!!");
66 for (String dpId : nodeIdSet){
67 LOG.info("Starting FlowRPCTaskHandler for switch id {}", dpId);
68 FlowRPCHandlerTask addFlowRpcTask = new FlowRPCHandlerTask(dpId, flowsPerDpn, batchSize);
69 flowPusher.execute(addFlowRpcTask);
74 private Set<String> getAllNodes(){
76 Set<String> nodeIds = new HashSet<>();
77 InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
78 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
81 Optional<Nodes> nodesDataNode = rTx.read(LogicalDatastoreType.OPERATIONAL, nodes).checkedGet();
82 if (nodesDataNode.isPresent()){
83 List<Node> nodesCollection = nodesDataNode.get().getNode();
84 if (nodesCollection != null && !nodesCollection.isEmpty()) {
85 for (Node node : nodesCollection) {
86 LOG.info("Switch with ID {} discovered !!", node.getId().getValue());
87 nodeIds.add(node.getId().getValue());
91 return Collections.emptySet();
95 return Collections.emptySet();
98 catch(ReadFailedException rdFailedException){
99 LOG.error("Failed to read connected nodes {}", rdFailedException);
104 public class FlowRPCHandlerTask implements Runnable {
105 private final String dpId;
106 private final int flowsPerDpn;
107 private final int batchSize;
109 public FlowRPCHandlerTask(final String dpId,
110 final int flowsPerDpn,
111 final int batchSize){
113 this.flowsPerDpn = flowsPerDpn;
114 this.batchSize = batchSize;
120 short tableId = (short)1;
121 int initFlowId = 500;
123 for (int i=1; i<= flowsPerDpn; i++){
125 String flowId = Integer.toString(initFlowId + i);
127 LOG.debug("Framing AddFlowInput for flow-id {}", flowId);
129 Match match = BulkOMaticUtils.getMatch(i);
130 InstanceIdentifier<Node> nodeIId = BulkOMaticUtils.getFlowCapableNodeId(dpId);
131 InstanceIdentifier<Table> tableIId = BulkOMaticUtils.getTableId(tableId, dpId);
132 InstanceIdentifier<Flow> flowIId = BulkOMaticUtils.getFlowId(tableIId, flowId);
134 Flow flow = BulkOMaticUtils.buildFlow(tableId, flowId, match);
136 AddFlowInputBuilder builder = new AddFlowInputBuilder(flow);
137 builder.setNode(new NodeRef(nodeIId));
138 builder.setFlowTable(new FlowTableRef(tableIId));
139 builder.setFlowRef(new FlowRef(flowIId));
141 AddFlowInput addFlowInput = builder.build();
143 LOG.debug("RPC invocation for adding flow-id {} with input {}", flowId,
144 addFlowInput.toString());
145 flowService.addFlow(addFlowInput);
147 if (i % batchSize == 0) {
149 LOG.info("Pausing for {} MILLISECONDS after batch of {} RPC invocations",
150 PAUSE_BETWEEN_BATCH_MILLIS, batchSize);
152 TimeUnit.MILLISECONDS.sleep(PAUSE_BETWEEN_BATCH_MILLIS);
153 } catch (InterruptedException iEx) {
154 LOG.error("Interrupted while pausing after batched push upto {}. Ex {}", i, iEx);