Merge "Drop Felix Gogo"
[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.DeviceInfo;
17 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
21 import org.opendaylight.openflowplugin.api.openflow.statistics.compatibility.Delegator;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * @author joe
45  */
46 public class OpendaylightFlowStatisticsServiceImpl implements OpendaylightFlowStatisticsService, Delegator<OpendaylightFlowStatisticsService> {
47
48     private static final Logger LOG = LoggerFactory.getLogger(OpendaylightFlowStatisticsServiceImpl.class);
49
50     private final Function<RpcResult<List<MultipartReply>>, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> matchingConvertor =
51             new Function<RpcResult<List<MultipartReply>>, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>>() {
52                 @Override
53                 public RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> apply(final RpcResult<List<MultipartReply>> input) {
54                     final DeviceInfo deviceInfo = matchingFlowsInTable.getDeviceInfo();
55                     final RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> rpcResult;
56                     if (input.isSuccessful()) {
57                         MultipartReply reply = input.getResult().get(0);
58                         final TranslatorKey translatorKey = new TranslatorKey(reply.getVersion(), MultipartReplyAggregateCase.class.getName());
59                         final MessageTranslator<MultipartReply, AggregatedFlowStatistics> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
60                         List<AggregatedFlowStatistics> aggregStats = new ArrayList<AggregatedFlowStatistics>();
61
62                         for (MultipartReply multipartReply : input.getResult()) {
63                             aggregStats.add(messageTranslator.translate(multipartReply, deviceInfo, null));
64                         }
65
66                         GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder =
67                                 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder();
68                         getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.setAggregatedFlowStatistics(aggregStats);
69
70                         rpcResult = RpcResultBuilder
71                                 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>success()
72                                 .withResult(getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.build())
73                                 .build();
74
75                     } else {
76                         rpcResult = RpcResultBuilder
77                                 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
78                                 .withRpcErrors(input.getErrors())
79                                 .build();
80                     }
81                     return rpcResult;
82                 }
83     };
84
85     private final MatchingFlowsInTableService matchingFlowsInTable;
86     private final TranslatorLibrary translatorLibrary;
87     private OpendaylightFlowStatisticsService delegate;
88
89     public static OpendaylightFlowStatisticsServiceImpl createWithOook(final RequestContextStack requestContextStack,
90                                                               final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
91         return new OpendaylightFlowStatisticsServiceImpl(requestContextStack, deviceContext, deviceContext.oook(), convertorExecutor);
92     }
93
94     public OpendaylightFlowStatisticsServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
95                                                  final TranslatorLibrary translatorLibrary, final ConvertorExecutor convertorExecutor) {
96         matchingFlowsInTable = new MatchingFlowsInTableService(requestContextStack, deviceContext, convertorExecutor);
97         this.translatorLibrary = translatorLibrary;
98     }
99
100     @Override
101     public void setDelegate(OpendaylightFlowStatisticsService delegate) {
102         this.delegate = delegate;
103     }
104
105     /**
106      * @deprecated provided for Be-release as backward compatibility relic
107      */
108     @Override
109     @Deprecated
110     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
111             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
112         if (delegate != null) {
113             return delegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input);
114         } else {
115             throw new IllegalAccessError("no delegate available - service is currently out of order");
116         }
117     }
118
119     @Override
120     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
121             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
122         return Futures.transform(matchingFlowsInTable.handleServiceCall(input), matchingConvertor);
123     }
124
125     /**
126      * @deprecated provided for Be-release as backward compatibility relic
127      */
128     @Override
129     @Deprecated
130     public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
131             final GetAllFlowStatisticsFromFlowTableInput input) {
132         if (delegate != null) {
133             return delegate.getAllFlowStatisticsFromFlowTable(input);
134         } else {
135             throw new IllegalAccessError("no delegate available - service is currently out of order");
136         }
137     }
138
139     /**
140      * @deprecated provided for Be-release as backward compatibility relic
141      */
142     @Override
143     @Deprecated
144     public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
145             final GetAllFlowsStatisticsFromAllFlowTablesInput input) {
146         if (delegate != null) {
147             return delegate.getAllFlowsStatisticsFromAllFlowTables(input);
148         } else {
149             throw new IllegalAccessError("no delegate available - service is currently out of order");
150         }
151     }
152
153     /**
154      * @deprecated provided for Be-release as backward compatibility relic
155      */
156     @Override
157     @Deprecated
158     public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
159             final GetFlowStatisticsFromFlowTableInput input) {
160         if (delegate != null) {
161             return delegate.getFlowStatisticsFromFlowTable(input);
162         } else {
163             throw new IllegalAccessError("no delegate available - service is currently out of order");
164         }
165     }
166 }