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