Merge "Bug 8253: Set-Field can not accept vendor extension fields."
[openflowplugin.git] / applications / bulk-o-matic / src / main / java / org / opendaylight / openflowplugin / applications / bulk / o / matic / FlowWriterDirectOFRpc.java
1 /*
2  * Copyright (c) 2016 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.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;
35
36 public class FlowWriterDirectOFRpc {
37
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;
43
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;
50     }
51
52
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);
57         }
58     }
59
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 !!!!");
64         }
65         else{
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);
70             }
71         }
72     }
73
74     private Set<String> getAllNodes(){
75
76         Set<String> nodeIds = new HashSet<>();
77         InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
78         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
79
80         try {
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());
88                     }
89                 }
90                 else{
91                     return Collections.emptySet();
92                 }
93             }
94             else{
95                 return Collections.emptySet();
96             }
97         }
98         catch(ReadFailedException rdFailedException){
99             LOG.error("Failed to read connected nodes {}", rdFailedException);
100         }
101         return nodeIds;
102     }
103
104     public class FlowRPCHandlerTask implements Runnable {
105         private final String dpId;
106         private final int flowsPerDpn;
107         private final int batchSize;
108
109         public FlowRPCHandlerTask(final String dpId,
110                                   final int flowsPerDpn,
111                                   final int batchSize){
112             this.dpId = dpId;
113             this.flowsPerDpn = flowsPerDpn;
114             this.batchSize = batchSize;
115         }
116
117         @Override
118         public void run() {
119
120             short tableId = (short)1;
121             int initFlowId = 500;
122
123             for (int i=1; i<= flowsPerDpn; i++){
124
125                 String flowId = Integer.toString(initFlowId + i);
126
127                 LOG.debug("Framing AddFlowInput for flow-id {}", flowId);
128
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);
133
134                 Flow flow = BulkOMaticUtils.buildFlow(tableId, flowId, match);
135
136                 AddFlowInputBuilder builder = new AddFlowInputBuilder(flow);
137                 builder.setNode(new NodeRef(nodeIId));
138                 builder.setFlowTable(new FlowTableRef(tableIId));
139                 builder.setFlowRef(new FlowRef(flowIId));
140
141                 AddFlowInput addFlowInput = builder.build();
142
143                 LOG.debug("RPC invocation for adding flow-id {} with input {}", flowId,
144                         addFlowInput.toString());
145                 flowService.addFlow(addFlowInput);
146
147                 if (i % batchSize == 0) {
148                     try {
149                         LOG.info("Pausing for {} MILLISECONDS after batch of {} RPC invocations",
150                                 PAUSE_BETWEEN_BATCH_MILLIS, batchSize);
151
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);
155                     }
156                 }
157             }
158         }
159     }
160 }