DeviceState changes
[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                     final RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> rpcResult;
54                     if (input.isSuccessful()) {
55                         MultipartReply reply = input.getResult().get(0);
56                         final TranslatorKey translatorKey = new TranslatorKey(reply.getVersion(), MultipartReplyAggregateCase.class.getName());
57                         final MessageTranslator<MultipartReply, AggregatedFlowStatistics> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
58                         List<AggregatedFlowStatistics> aggregStats = new ArrayList<AggregatedFlowStatistics>();
59
60                         for (MultipartReply multipartReply : input.getResult()) {
61                             aggregStats.add(messageTranslator.translate(multipartReply, deviceContext.getDeviceInfo(), null));
62                         }
63
64                         GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder =
65                                 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder();
66                         getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.setAggregatedFlowStatistics(aggregStats);
67
68                         rpcResult = RpcResultBuilder
69                                 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>success()
70                                 .withResult(getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.build())
71                                 .build();
72
73                     } else {
74                         rpcResult = RpcResultBuilder
75                                 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
76                                 .withRpcErrors(input.getErrors())
77                                 .build();
78                     }
79                     return rpcResult;
80                 }
81     };
82
83     private final MatchingFlowsInTableService matchingFlowsInTable;
84     private final TranslatorLibrary translatorLibrary;
85     private OpendaylightFlowStatisticsService delegate;
86
87     public static OpendaylightFlowStatisticsServiceImpl createWithOook(final RequestContextStack requestContextStack,
88                                                               final DeviceContext deviceContext) {
89         return new OpendaylightFlowStatisticsServiceImpl(requestContextStack, deviceContext, deviceContext.oook());
90     }
91
92     public OpendaylightFlowStatisticsServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
93                                                  final TranslatorLibrary translatorLibrary) {
94         matchingFlowsInTable = new MatchingFlowsInTableService(requestContextStack, deviceContext);
95         this.translatorLibrary = translatorLibrary;
96     }
97
98     @Override
99     public void setDelegate(OpendaylightFlowStatisticsService delegate) {
100         this.delegate = delegate;
101     }
102
103     /**
104      * @deprecated provided for Be-release as backward compatibility relic
105      */
106     @Override
107     @Deprecated
108     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
109             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
110         if (delegate != null) {
111             return delegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input);
112         } else {
113             throw new IllegalAccessError("no delegate available - service is currently out of order");
114         }
115     }
116
117     @Override
118     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
119             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
120         return Futures.transform(matchingFlowsInTable.handleServiceCall(input), matchingConvertor);
121     }
122
123     /**
124      * @deprecated provided for Be-release as backward compatibility relic
125      */
126     @Override
127     @Deprecated
128     public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
129             final GetAllFlowStatisticsFromFlowTableInput input) {
130         if (delegate != null) {
131             return delegate.getAllFlowStatisticsFromFlowTable(input);
132         } else {
133             throw new IllegalAccessError("no delegate available - service is currently out of order");
134         }
135     }
136
137     /**
138      * @deprecated provided for Be-release as backward compatibility relic
139      */
140     @Override
141     @Deprecated
142     public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
143             final GetAllFlowsStatisticsFromAllFlowTablesInput input) {
144         if (delegate != null) {
145             return delegate.getAllFlowsStatisticsFromAllFlowTables(input);
146         } else {
147             throw new IllegalAccessError("no delegate available - service is currently out of order");
148         }
149     }
150
151     /**
152      * @deprecated provided for Be-release as backward compatibility relic
153      */
154     @Override
155     @Deprecated
156     public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
157             final GetFlowStatisticsFromFlowTableInput input) {
158         if (delegate != null) {
159             return delegate.getFlowStatisticsFromFlowTable(input);
160         } else {
161             throw new IllegalAccessError("no delegate available - service is currently out of order");
162         }
163     }
164 }