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