/** * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowplugin.impl.statistics.services; import com.google.common.base.Function; import com.google.common.util.concurrent.Futures; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Future; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator; import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack; import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary; import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey; import org.opendaylight.openflowplugin.api.openflow.statistics.compatibility.Delegator; import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author joe */ public class OpendaylightFlowStatisticsServiceImpl implements OpendaylightFlowStatisticsService, Delegator { private static final Logger LOG = LoggerFactory.getLogger(OpendaylightFlowStatisticsServiceImpl.class); private final Function>, RpcResult> matchingConvertor = new Function>, RpcResult>() { @Override public RpcResult apply(final RpcResult> input) { final DeviceInfo deviceInfo = matchingFlowsInTable.getDeviceInfo(); final RpcResult rpcResult; if (input.isSuccessful()) { MultipartReply reply = input.getResult().get(0); final TranslatorKey translatorKey = new TranslatorKey(reply.getVersion(), MultipartReplyAggregateCase.class.getName()); final MessageTranslator messageTranslator = translatorLibrary.lookupTranslator(translatorKey); List aggregStats = new ArrayList(); for (MultipartReply multipartReply : input.getResult()) { aggregStats.add(messageTranslator.translate(multipartReply, deviceInfo, null)); } GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder = new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder(); getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.setAggregatedFlowStatistics(aggregStats); rpcResult = RpcResultBuilder .success() .withResult(getAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder.build()) .build(); } else { rpcResult = RpcResultBuilder .failed() .withRpcErrors(input.getErrors()) .build(); } return rpcResult; } }; private final MatchingFlowsInTableService matchingFlowsInTable; private final TranslatorLibrary translatorLibrary; private OpendaylightFlowStatisticsService delegate; public static OpendaylightFlowStatisticsServiceImpl createWithOook(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) { return new OpendaylightFlowStatisticsServiceImpl(requestContextStack, deviceContext, deviceContext.oook(), convertorExecutor); } public OpendaylightFlowStatisticsServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final TranslatorLibrary translatorLibrary, final ConvertorExecutor convertorExecutor) { matchingFlowsInTable = new MatchingFlowsInTableService(requestContextStack, deviceContext, convertorExecutor); this.translatorLibrary = translatorLibrary; } @Override public void setDelegate(OpendaylightFlowStatisticsService delegate) { this.delegate = delegate; } /** * @deprecated provided for Be-release as backward compatibility relic */ @Override @Deprecated public Future> getAggregateFlowStatisticsFromFlowTableForAllFlows( final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) { if (delegate != null) { return delegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input); } else { throw new IllegalAccessError("no delegate available - service is currently out of order"); } } @Override public Future> getAggregateFlowStatisticsFromFlowTableForGivenMatch( final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) { return Futures.transform(matchingFlowsInTable.handleServiceCall(input), matchingConvertor); } /** * @deprecated provided for Be-release as backward compatibility relic */ @Override @Deprecated public Future> getAllFlowStatisticsFromFlowTable( final GetAllFlowStatisticsFromFlowTableInput input) { if (delegate != null) { return delegate.getAllFlowStatisticsFromFlowTable(input); } else { throw new IllegalAccessError("no delegate available - service is currently out of order"); } } /** * @deprecated provided for Be-release as backward compatibility relic */ @Override @Deprecated public Future> getAllFlowsStatisticsFromAllFlowTables( final GetAllFlowsStatisticsFromAllFlowTablesInput input) { if (delegate != null) { return delegate.getAllFlowsStatisticsFromAllFlowTables(input); } else { throw new IllegalAccessError("no delegate available - service is currently out of order"); } } /** * @deprecated provided for Be-release as backward compatibility relic */ @Override @Deprecated public Future> getFlowStatisticsFromFlowTable( final GetFlowStatisticsFromFlowTableInput input) { if (delegate != null) { return delegate.getFlowStatisticsFromFlowTable(input); } else { throw new IllegalAccessError("no delegate available - service is currently out of order"); } } }