Fix checkstyle/spotbugs violations
[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.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
42
43     private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
44     private static final int TEST_TIMEOUT = 5;
45
46     private final GlobalBindingRTCServer globalServer;
47     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
48     private final RpcProviderRegistry providerRegistry;
49
50     public RpcbenchmarkProvider(final RpcProviderRegistry providerRegistry, final GlobalBindingRTCServer globalServer) {
51         this.providerRegistry = providerRegistry;
52         this.globalServer = globalServer;
53     }
54
55     public void init() {
56         LOG.info("RpcbenchmarkProvider initiated");
57     }
58
59     @Override
60     public void close() {
61         LOG.info("RpcbenchmarkProvider closed");
62     }
63
64     @Override
65     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
66         LOG.debug("startTest {}", input);
67
68         final RTCClient client;
69         final List<RoutedRpcRegistration<?>> rpcRegs = new ArrayList<>();
70
71         switch (input.getOperation()) {
72             case ROUTEDRTC:
73                 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
74                 for (int i = 0; i < input.getNumServers().intValue(); i++) {
75                     GlobalBindingRTCServer server = new GlobalBindingRTCServer();
76                     RoutedRpcRegistration<RpcbenchPayloadService> routedReg =
77                             providerRegistry.addRoutedRpcImplementation(RpcbenchPayloadService.class, server);
78
79                     KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
80                             InstanceIdentifier
81                             .create(RpcbenchRpcRoutes.class)
82                             .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
83                     routeIid.add(iid);
84                     routedReg.registerPath(NodeContext.class, iid);
85                     rpcRegs.add(routedReg);
86                 }
87
88                 client = new RoutedBindingRTClient(providerRegistry, input.getPayloadSize().intValue(), routeIid);
89                 break;
90
91             case GLOBALRTC:
92                 client = new GlobalBindingRTCClient(providerRegistry, input.getPayloadSize().intValue());
93                 break;
94
95             default:
96                 LOG.error("Unsupported server/client type {}", input.getOperation());
97                 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
98         }
99
100         try {
101             ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
102
103             final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
104
105             LOG.info("Test Started");
106             final long startTime = System.nanoTime();
107
108             for (int i = 0; i < input.getNumClients().intValue(); i++) {
109                 // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE, but we should check more
110                 verifyNotNull(executor.submit(testRun));
111             }
112
113             executor.shutdown();
114             try {
115                 executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
116             } catch (final InterruptedException e) {
117                 LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
118             }
119
120             long endTime = System.nanoTime();
121             LOG.info("Test Done");
122
123             long elapsedTime = endTime - startTime;
124
125             StartTestOutput output = new StartTestOutputBuilder()
126                                             .setRate((long)0)
127                                             .setGlobalRtcClientError(client.getRpcError())
128                                             .setGlobalRtcClientOk(client.getRpcOk())
129                                             .setExecTime(TimeUnit.NANOSECONDS.toMillis(elapsedTime))
130                                             .setRate(
131                                                 (client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime)
132                                             .build();
133             return RpcResultBuilder.success(output).buildFuture();
134         } finally {
135             for (RoutedRpcRegistration<?> routedRpcRegistration : rpcRegs) {
136                 routedRpcRegistration.close();
137             }
138         }
139     }
140
141     @Override
142     public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
143         LOG.info("testStatus");
144         TestStatusOutput output = new TestStatusOutputBuilder()
145                                         .setGlobalServerCnt((long)globalServer.getNumRpcs())
146                                         .setExecStatus(execStatus.get())
147                                         .build();
148         return RpcResultBuilder.success(output).buildFuture();
149     }
150
151 }