Do not use Binding DTO compat methods
[controller.git] / benchmark / rpcbenchmark / src / main / java / rpcbenchmark / impl / RpcbenchmarkProvider.java
1 /*
2  * Copyright (c) 2015 Cisco 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 rpcbenchmark.impl;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.atomic.AtomicReference;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.NodeContext;
22 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
23 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchRpcRoutes;
24 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRoute;
25 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRouteKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.RpcbenchmarkService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint32;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
43
44     private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
45     private static final int TEST_TIMEOUT = 5;
46
47     private final GlobalBindingRTCServer globalServer;
48     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
49     private final RpcProviderRegistry providerRegistry;
50
51     public RpcbenchmarkProvider(final RpcProviderRegistry providerRegistry, final GlobalBindingRTCServer globalServer) {
52         this.providerRegistry = providerRegistry;
53         this.globalServer = globalServer;
54     }
55
56     public void init() {
57         LOG.info("RpcbenchmarkProvider initiated");
58     }
59
60     @Override
61     public void close() {
62         LOG.info("RpcbenchmarkProvider closed");
63     }
64
65     @Override
66     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
67         LOG.debug("startTest {}", input);
68
69         final RTCClient client;
70         final List<RoutedRpcRegistration<?>> rpcRegs = new ArrayList<>();
71
72         switch (input.getOperation()) {
73             case ROUTEDRTC:
74                 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
75                 for (int i = 0; i < input.getNumServers().intValue(); i++) {
76                     GlobalBindingRTCServer server = new GlobalBindingRTCServer();
77                     RoutedRpcRegistration<RpcbenchPayloadService> routedReg =
78                             providerRegistry.addRoutedRpcImplementation(RpcbenchPayloadService.class, server);
79
80                     KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
81                             InstanceIdentifier
82                             .create(RpcbenchRpcRoutes.class)
83                             .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
84                     routeIid.add(iid);
85                     routedReg.registerPath(NodeContext.class, iid);
86                     rpcRegs.add(routedReg);
87                 }
88
89                 client = new RoutedBindingRTClient(providerRegistry, input.getPayloadSize().intValue(), routeIid);
90                 break;
91
92             case GLOBALRTC:
93                 client = new GlobalBindingRTCClient(providerRegistry, input.getPayloadSize().intValue());
94                 break;
95
96             default:
97                 LOG.error("Unsupported server/client type {}", input.getOperation());
98                 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
99         }
100
101         try {
102             ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
103
104             final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
105
106             LOG.info("Test Started");
107             final long startTime = System.nanoTime();
108
109             for (int i = 0; i < input.getNumClients().intValue(); i++) {
110                 // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE, but we should check more
111                 verifyNotNull(executor.submit(testRun));
112             }
113
114             executor.shutdown();
115             try {
116                 executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
117             } catch (final InterruptedException e) {
118                 LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
119             }
120
121             long endTime = System.nanoTime();
122             LOG.info("Test Done");
123
124             long elapsedTime = endTime - startTime;
125
126             StartTestOutput output = new StartTestOutputBuilder()
127                                             .setRate(Uint32.ZERO)
128                                             .setGlobalRtcClientError(Uint32.valueOf(client.getRpcError()))
129                                             .setGlobalRtcClientOk(Uint32.valueOf(client.getRpcOk()))
130                                             .setExecTime(Uint32.valueOf(TimeUnit.NANOSECONDS.toMillis(elapsedTime)))
131                                             .setRate(Uint32.valueOf(
132                                                 (client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime))
133                                             .build();
134             return RpcResultBuilder.success(output).buildFuture();
135         } finally {
136             for (RoutedRpcRegistration<?> routedRpcRegistration : rpcRegs) {
137                 routedRpcRegistration.close();
138             }
139         }
140     }
141
142     @Override
143     public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
144         LOG.info("testStatus");
145         TestStatusOutput output = new TestStatusOutputBuilder()
146                                         .setGlobalServerCnt(Uint32.valueOf(globalServer.getNumRpcs()))
147                                         .setExecStatus(execStatus.get())
148                                         .build();
149         return RpcResultBuilder.success(output).buildFuture();
150     }
151
152 }