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