Use String(byte[], Charset)
[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 java.util.Collections;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.Set;
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;
36
37 public class FlowWriterDirectOFRpc {
38
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;
44
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;
50     }
51
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);
56         }
57     }
58
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 !!!!");
63         } else {
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);
68             }
69         }
70     }
71
72     private Set<String> getAllNodes() {
73
74         Set<String> nodeIds = new HashSet<>();
75         InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
76
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());
85                     }
86                 } else {
87                     return Collections.emptySet();
88                 }
89             } else {
90                 return Collections.emptySet();
91             }
92         } catch (InterruptedException | ExecutionException e) {
93             LOG.error("Failed to read connected nodes", e);
94         }
95         return nodeIds;
96     }
97
98     public class FlowRPCHandlerTask implements Runnable {
99         private final String dpId;
100         private final int flowsPerDpn;
101         private final int batchSize;
102
103         public FlowRPCHandlerTask(final String dpId, final int flowsPerDpn, final int batchSize) {
104             this.dpId = dpId;
105             this.flowsPerDpn = flowsPerDpn;
106             this.batchSize = batchSize;
107         }
108
109         @Override
110         public void run() {
111
112             short tableId = (short) 1;
113             int initFlowId = 500;
114
115             for (int i = 1; i <= flowsPerDpn; i++) {
116
117                 String flowId = Integer.toString(initFlowId + i);
118
119                 LOG.debug("Framing AddFlowInput for flow-id {}", flowId);
120
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);
125
126                 Flow flow = BulkOMaticUtils.buildFlow(tableId, flowId, match);
127
128                 AddFlowInputBuilder builder = new AddFlowInputBuilder(flow);
129                 builder.setNode(new NodeRef(nodeIId));
130                 builder.setFlowTable(new FlowTableRef(tableIId));
131                 builder.setFlowRef(new FlowRef(flowIId));
132
133                 AddFlowInput addFlowInput = builder.build();
134
135                 LOG.debug("RPC invocation for adding flow-id {} with input {}", flowId, addFlowInput.toString());
136                 LoggingFutures.addErrorLogging(flowService.addFlow(addFlowInput), LOG, "addFlow");
137
138                 if (i % batchSize == 0) {
139                     try {
140                         LOG.info("Pausing for {} MILLISECONDS after batch of {} RPC invocations",
141                                 PAUSE_BETWEEN_BATCH_MILLIS, batchSize);
142
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);
146                     }
147                 }
148             }
149         }
150     }
151 }