2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.statisitcs;
10 import com.google.common.util.concurrent.Futures;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.genius.mdsalutil.NwConstants;
14 import org.opendaylight.netvirt.elan.internal.ElanServiceProvider;
15 import org.opendaylight.netvirt.elan.utils.ElanConstants;
16 import org.opendaylight.netvirt.elan.utils.ElanUtils;
17 //import org.opendaylight.genius.ericsson.mdsalutil.statistics.StatValue;
18 //import org.opendaylight.genius.ericsson.mdsalutil.statistics.StatisticsInfo;
19 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
20 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
21 import org.opendaylight.genius.interfacemanager.globals.InterfaceServiceUtil;
22 import org.opendaylight.genius.interfacemanager.globals.VlanInterfaceInfo;
23 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
24 import org.opendaylight.genius.mdsalutil.MatchInfo;
25 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
26 //import org.opendaylight.yang.gen.v1.urn.opendaylight.genius._interface.service.rev150602._interface.service.info.ServiceInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius._interface.statistics.rev150824.ResultCode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.get.elan._interface.statistics.output.StatResultBuilder;
36 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.math.BigInteger;
43 import java.util.List;
45 import java.util.concurrent.Future;
47 public class ElanStatisticsImpl implements ElanStatisticsService {
48 private ElanServiceProvider elanServiceProvider;
49 private static final Logger logger = LoggerFactory.getLogger(ElanStatisticsImpl.class);
50 private static volatile ElanStatisticsImpl elanStatisticsImpl;
53 private ElanStatisticsImpl(ElanServiceProvider elanServiceProvider) {
55 this.elanServiceProvider = elanServiceProvider;
60 public static ElanStatisticsImpl getElanStatisticsService(ElanServiceProvider elanServiceProvider) {
61 if (elanStatisticsImpl == null)
62 synchronized (ElanStatisticsImpl.class) {
63 if (elanStatisticsImpl == null) {
64 elanStatisticsImpl = new ElanStatisticsImpl(elanServiceProvider);
67 return elanStatisticsImpl;
71 public Future<RpcResult<GetElanInterfaceStatisticsOutput>> getElanInterfaceStatistics(
72 GetElanInterfaceStatisticsInput input) {
73 String interfaceName = input.getInterfaceName();
74 logger.debug("getElanInterfaceStatistics is called for elan interface {}", interfaceName);
75 RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder = null;
76 if (interfaceName == null) {
77 rpcResultBuilder = RpcResultBuilder.failed();
78 return getFutureWithAppErrorMessage(rpcResultBuilder, "Interface name is not provided");
80 ElanInterface elanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
81 if (elanInterface == null) {
82 rpcResultBuilder = RpcResultBuilder.failed();
83 return getFutureWithAppErrorMessage(rpcResultBuilder, String.format("Interface %s is not a ELAN interface", interfaceName));
85 String elanInstanceName = elanInterface.getElanInstanceName();
86 ElanInstance elanInfo = ElanUtils.getElanInstanceByName(elanInstanceName);
87 long elanTag = elanInfo.getElanTag();
88 InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager().getInterfaceInfo(interfaceName);
89 ServicesInfo serviceInfo = ElanUtils.getServiceInfo(elanInstanceName, elanTag, interfaceName);
90 //FIXME [ELANBE] Get this API Later
94 // //tableId = interfaceManager.getTableIdForService(interfaceName, serviceInfo);
95 // } catch (InterfaceNotFoundException | InterfaceServiceNotFoundException e) {
96 // rpcResultBuilder = RpcResultBuilder.failed();
97 // return getFutureWithAppErrorMessage(rpcResultBuilder, String.format("Interface %s or Service %s doesn't exist", interfaceName, serviceInfo));
99 if (!interfaceInfo.isOperational()) {
100 logger.debug("interface {} is down and returning with no statistics", interfaceName);
101 rpcResultBuilder = RpcResultBuilder.success();
102 return Futures.immediateFuture(rpcResultBuilder.withResult(new GetElanInterfaceStatisticsOutputBuilder().setStatResult(new StatResultBuilder()
103 .setStatResultCode(ResultCode.NotFound).setByteRxCount(0L).setByteTxCount(0L).setPacketRxCount(0L)
104 .setPacketTxCount(0L).build()).build()).build());
106 rpcResultBuilder = RpcResultBuilder.success();
107 return Futures.immediateFuture(rpcResultBuilder.withResult(queryforElanInterfaceStatistics(tableId, elanInstanceName, interfaceInfo)).build());
110 private GetElanInterfaceStatisticsOutput queryforElanInterfaceStatistics(short tableId, String elanInstanceName, InterfaceInfo interfaceInfo) {
111 BigInteger dpId = interfaceInfo.getDpId();
112 List<MatchInfo> matches = null;
113 String interfaceName = interfaceInfo.getInterfaceName();
114 if (tableId == IfmConstants.VLAN_INTERFACE_INGRESS_TABLE) {
115 VlanInterfaceInfo vlanInterfaceInfo = (VlanInterfaceInfo)interfaceInfo;
116 matches = InterfaceServiceUtil.getMatchInfoForVlanLPort(dpId, interfaceInfo.getPortNo(),
117 InterfaceServiceUtil.getVlanId(interfaceName, elanServiceProvider.getBroker()), vlanInterfaceInfo.isVlanTransparent());
119 matches = InterfaceServiceUtil.getLPortDispatcherMatches(NwConstants.ELAN_SERVICE_INDEX, interfaceInfo.getInterfaceTag());
121 long groupId = interfaceInfo.getGroupId();
122 Set<Object> statRequestKeys = InterfaceServiceUtil.getStatRequestKeys(dpId, tableId, matches, String.format("%s.%s", elanInstanceName, interfaceName), groupId);
123 // StatisticsInfo statsInfo = new StatisticsInfo(statRequestKeys);
124 // org.opendaylight.vpnservice.ericsson.mdsalutil.statistics.StatResult statResult = mdsalMgr.queryForStatistics(interfaceName, statsInfo);
125 // ResultCode resultCode = ResultCode.Success;
126 // if (!statResult.isComplete()) {
127 // resultCode = ResultCode.Incomplete;
130 //StatValue ingressFlowStats = statResult.getStatResult(InterfaceServiceUtil.getFlowStatisticsKey(dpId, tableId, matches, elanInstanceName));
131 //StatValue groupStats = statResult.getStatResult(InterfaceServiceUtil.getGroupStatisticsKey(dpId, groupId));
132 // return new GetElanInterfaceStatisticsOutputBuilder().setStatResult(new StatResultBuilder().setStatResultCode(resultCode)
133 // .setByteRxCount(ingressFlowStats.getByteCount()).setPacketRxCount(ingressFlowStats.getPacketCount())
134 // .setByteTxCount(groupStats.getByteCount()).setPacketTxCount(groupStats.getPacketCount()).build()).build();
138 private Future<RpcResult<GetElanInterfaceStatisticsOutput>> getFutureWithAppErrorMessage(
139 RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder, String message) {
140 rpcResultBuilder.withError(ErrorType.APPLICATION, message);
141 return Futures.immediateFuture(rpcResultBuilder.build());