Clean up instance checks and casts
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / singlelayer / SingleLayerAggregateFlowMultipartService.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.services.singlelayer;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.stream.Collectors;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.impl.services.AbstractAggregateFlowMultipartService;
20 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowAggregateStats;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowAggregateStatsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.multipart.request.flow.aggregate.stats.FlowAggregateStatsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33
34 public class SingleLayerAggregateFlowMultipartService
35         extends AbstractAggregateFlowMultipartService<MultipartReply> {
36
37     public SingleLayerAggregateFlowMultipartService(final RequestContextStack requestContextStack,
38                                                     final DeviceContext deviceContext) {
39         super(requestContextStack, deviceContext);
40     }
41
42     @Override
43     protected OfHeader buildRequest(final Xid xid,
44                                     final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input)
45                                     throws ServiceException {
46         return new MultipartRequestBuilder()
47                 .setXid(xid.getValue())
48                 .setVersion(getVersion())
49                 .setRequestMore(false)
50                 .setMultipartRequestBody(new MultipartRequestFlowAggregateStatsBuilder()
51                     .setFlowAggregateStats(new FlowAggregateStatsBuilder(input).build()).build())
52                 .build();
53     }
54
55     @Override
56     public ListenableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> handleAndReply(
57             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
58         return Futures.transform(handleServiceCall(input), result -> {
59             if (Preconditions.checkNotNull(result).isSuccessful()) {
60                 return RpcResultBuilder
61                         .success(new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder()
62                                 .setAggregatedFlowStatistics(result
63                                         .getResult()
64                                         .stream()
65                                         .map(MultipartReply::getMultipartReplyBody)
66                                         .filter(MultipartReplyFlowAggregateStats.class::isInstance)
67                                         .map(multipartReplyBody ->
68                                                 new AggregatedFlowStatisticsBuilder(
69                                                     (MultipartReplyFlowAggregateStats) multipartReplyBody)
70                                                         .build())
71                                         .collect(Collectors.toList())))
72                         .build();
73             }
74
75             return RpcResultBuilder
76                     .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
77                     .withRpcErrors(result.getErrors())
78                     .build();
79         }, MoreExecutors.directExecutor());
80     }
81 }