Close ReadTransactions
[openflowplugin.git] / applications / bulk-o-matic / src / main / java / org / opendaylight / openflowplugin / applications / bulk / o / matic / FlowWriterDirectOFRpc.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson 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.applications.bulk.o.matic;
9
10 import com.google.common.base.Optional;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
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;
39
40 public class FlowWriterDirectOFRpc {
41
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;
47
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;
53     }
54
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);
59         }
60     }
61
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 !!!!");
66         } else {
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);
71             }
72         }
73     }
74
75     private Set<String> getAllNodes() {
76
77         Set<String> nodeIds = new HashSet<>();
78         InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
79
80         try (ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
81             Optional<Nodes> nodesDataNode = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodes)
82                     .checkedGet();
83             if (nodesDataNode.isPresent()) {
84                 List<Node> nodesCollection = nodesDataNode.get().getNode();
85                 if (nodesCollection != null && !nodesCollection.isEmpty()) {
86                     for (Node node : nodesCollection) {
87                         LOG.info("Switch with ID {} discovered !!", node.getId().getValue());
88                         nodeIds.add(node.getId().getValue());
89                     }
90                 } else {
91                     return Collections.emptySet();
92                 }
93             } else {
94                 return Collections.emptySet();
95             }
96         } catch (ReadFailedException rdFailedException) {
97             LOG.error("Failed to read connected nodes {}", rdFailedException);
98         }
99         return nodeIds;
100     }
101
102     public class FlowRPCHandlerTask implements Runnable {
103         private final String dpId;
104         private final int flowsPerDpn;
105         private final int batchSize;
106
107         public FlowRPCHandlerTask(final String dpId, final int flowsPerDpn, final int batchSize) {
108             this.dpId = dpId;
109             this.flowsPerDpn = flowsPerDpn;
110             this.batchSize = batchSize;
111         }
112
113         @Override
114         public void run() {
115
116             short tableId = (short) 1;
117             int initFlowId = 500;
118
119             for (int i = 1; i <= flowsPerDpn; i++) {
120
121                 String flowId = Integer.toString(initFlowId + i);
122
123                 LOG.debug("Framing AddFlowInput for flow-id {}", flowId);
124
125                 Match match = BulkOMaticUtils.getMatch(i);
126                 InstanceIdentifier<Node> nodeIId = BulkOMaticUtils.getFlowCapableNodeId(dpId);
127                 InstanceIdentifier<Table> tableIId = BulkOMaticUtils.getTableId(tableId, dpId);
128                 InstanceIdentifier<Flow> flowIId = BulkOMaticUtils.getFlowId(tableIId, flowId);
129
130                 Flow flow = BulkOMaticUtils.buildFlow(tableId, flowId, match);
131
132                 AddFlowInputBuilder builder = new AddFlowInputBuilder(flow);
133                 builder.setNode(new NodeRef(nodeIId));
134                 builder.setFlowTable(new FlowTableRef(tableIId));
135                 builder.setFlowRef(new FlowRef(flowIId));
136
137                 AddFlowInput addFlowInput = builder.build();
138
139                 LOG.debug("RPC invocation for adding flow-id {} with input {}", flowId, addFlowInput.toString());
140                 final Future<RpcResult<AddFlowOutput>> resultFuture = flowService.addFlow(addFlowInput);
141                 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
142
143                 if (i % batchSize == 0) {
144                     try {
145                         LOG.info("Pausing for {} MILLISECONDS after batch of {} RPC invocations",
146                                 PAUSE_BETWEEN_BATCH_MILLIS, batchSize);
147
148                         TimeUnit.MILLISECONDS.sleep(PAUSE_BETWEEN_BATCH_MILLIS);
149                     } catch (InterruptedException iEx) {
150                         LOG.error("Interrupted while pausing after batched push upto {} Ex ", i, iEx);
151                     }
152                 }
153             }
154         }
155     }
156 }