Merge "Fix issues related to checkstyle enforcement on openflow-impl module"
[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 java.util.concurrent.Future;
14 import java.util.stream.Collectors;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.openflowplugin.impl.services.AbstractAggregateFlowMultipartService;
19 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowAggregateStats;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowAggregateStatsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
31
32 public class SingleLayerAggregateFlowMultipartService
33         extends AbstractAggregateFlowMultipartService<MultipartReply> {
34
35     public SingleLayerAggregateFlowMultipartService(final RequestContextStack requestContextStack,
36                                                     final DeviceContext deviceContext) {
37         super(requestContextStack, deviceContext);
38     }
39
40     @Override
41     protected OfHeader buildRequest(final Xid xid,
42                                     final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input)
43                                     throws ServiceException {
44         return new MultipartRequestBuilder()
45                 .setXid(xid.getValue())
46                 .setVersion(getVersion())
47                 .setRequestMore(false)
48                 .setMultipartRequestBody(new MultipartRequestFlowAggregateStatsBuilder(input)
49                         .build())
50                 .build();
51     }
52
53     @Override
54     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> handleAndReply(
55             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
56         return Futures.transform(handleServiceCall(input), result -> {
57             if (Preconditions.checkNotNull(result).isSuccessful()) {
58                 return RpcResultBuilder
59                         .success(new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder()
60                                 .setAggregatedFlowStatistics(result
61                                         .getResult()
62                                         .stream()
63                                         .map(MultipartReply::getMultipartReplyBody)
64                                         .filter(MultipartReplyFlowAggregateStats.class::isInstance)
65                                         .map(multipartReplyBody ->
66                                                 new AggregatedFlowStatisticsBuilder(
67                                                         MultipartReplyFlowAggregateStats.class
68                                                         .cast(multipartReplyBody))
69                                                         .build())
70                                         .collect(Collectors.toList())))
71                         .build();
72             }
73
74             return RpcResultBuilder
75                     .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
76                     .withRpcErrors(result.getErrors())
77                     .build();
78         });
79     }
80 }