Merge "Simplify CommonService interface"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowStatisticsServiceImpl.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 org.opendaylight.openflowplugin.impl.statistics.services;
9
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.Futures;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.Future;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * @author joe
42  */
43 public class OpendaylightFlowStatisticsServiceImpl implements OpendaylightFlowStatisticsService {
44     private static final Logger LOG = LoggerFactory.getLogger(OpendaylightFlowStatisticsServiceImpl.class);
45
46     private final Function<RpcResult<List<MultipartReply>>, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> matchingConvertor =
47             new Function<RpcResult<List<MultipartReply>>, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>>() {
48                 @Override
49                 public RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> apply(final RpcResult<List<MultipartReply>> input) {
50                     final DeviceContext deviceContext = matchingFlowsInTable.getDeviceContext();
51                     TranslatorLibrary translatorLibrary = deviceContext.oook();
52                     RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> rpcResult;
53                     if (input.isSuccessful()) {
54                         MultipartReply reply = input.getResult().get(0);
55                         final TranslatorKey translatorKey = new TranslatorKey(reply.getVersion(), MultipartReplyAggregateCase.class.getName());
56                         final MessageTranslator<MultipartReply, AggregatedFlowStatistics> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
57                         List<AggregatedFlowStatistics> aggregStats = new ArrayList<AggregatedFlowStatistics>();
58
59                         for (MultipartReply multipartReply : input.getResult()) {
60                             aggregStats.add(messageTranslator.translate(multipartReply, deviceContext, null));
61                         }
62
63                         GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder =
64                                 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder();
65                         getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.setAggregatedFlowStatistics(aggregStats);
66
67                         rpcResult = RpcResultBuilder
68                                 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>success()
69                                 .withResult(getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.build())
70                                 .build();
71
72                     } else {
73                         rpcResult = RpcResultBuilder
74                                 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
75                                 .withRpcErrors(input.getErrors())
76                                 .build();
77                     }
78                     return rpcResult;
79                 }
80     };
81
82     private final AggregateFlowsInTableService aggregateFlowsInTable;
83     private final MatchingFlowsInTableService matchingFlowsInTable;
84     private final AllFlowsInAllTablesService allFlowsInAllTables;
85     private final AllFlowsInTableService allFlowsInTable;
86     private final FlowsInTableService flowsInTable;
87
88     public OpendaylightFlowStatisticsServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
89         aggregateFlowsInTable = new AggregateFlowsInTableService(requestContextStack, deviceContext);
90         allFlowsInAllTables = new AllFlowsInAllTablesService(requestContextStack, deviceContext);
91         allFlowsInTable = new AllFlowsInTableService(requestContextStack, deviceContext);
92         flowsInTable = new FlowsInTableService(requestContextStack, deviceContext);
93         matchingFlowsInTable = new MatchingFlowsInTableService(requestContextStack, deviceContext);
94     }
95
96     @Override
97     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
98             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
99         return aggregateFlowsInTable.handleServiceCall(input);
100     }
101
102     @Override
103     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
104             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
105         return Futures.transform(matchingFlowsInTable.handleServiceCall(input), matchingConvertor);
106     }
107
108     @Override
109     public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
110             final GetAllFlowStatisticsFromFlowTableInput input) {
111         return allFlowsInTable.handleServiceCall(input);
112     }
113
114     @Override
115     public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
116             final GetAllFlowsStatisticsFromAllFlowTablesInput input) {
117         return allFlowsInAllTables.handleServiceCall(input);
118     }
119
120     @Override
121     public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
122             final GetFlowStatisticsFromFlowTableInput input) {
123         return flowsInTable.handleServiceCall(input);
124     }
125 }