700eb0f78396ff09c0492088b77c6acf6feedf05
[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.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.flow.types.rev131026.multipart.request.multipart.request.body.multipart.request.flow.aggregate.stats.FlowAggregateStatsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32
33 public class SingleLayerAggregateFlowMultipartService
34         extends AbstractAggregateFlowMultipartService<MultipartReply> {
35
36     public SingleLayerAggregateFlowMultipartService(final RequestContextStack requestContextStack,
37                                                     final DeviceContext deviceContext) {
38         super(requestContextStack, deviceContext);
39     }
40
41     @Override
42     protected OfHeader buildRequest(final Xid xid,
43                                     final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
44         return new MultipartRequestBuilder()
45                 .setXid(xid.getValue())
46                 .setVersion(getVersion())
47                 .setRequestMore(false)
48                 .setMultipartRequestBody(new MultipartRequestFlowAggregateStatsBuilder()
49                     .setFlowAggregateStats(new FlowAggregateStatsBuilder(input).build()).build())
50                 .build();
51     }
52
53     @Override
54     public ListenableFuture<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) multipartReplyBody)
68                                                         .build())
69                                         .collect(Collectors.toList())))
70                         .build();
71             }
72
73             return RpcResultBuilder
74                     .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
75                     .withRpcErrors(result.getErrors())
76                     .build();
77         }, MoreExecutors.directExecutor());
78     }
79 }