Handle nullable lists in elanmanager
[netvirt.git] / 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.base.Optional;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import javax.annotation.Nullable;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
17 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
18 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius._interface.statistics.rev150824.ResultCode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.GetElanInterfaceStatisticsOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.get.elan._interface.statistics.output.StatResultBuilder;
26 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Singleton
33 public class ElanStatisticsImpl implements ElanStatisticsService {
34     private static final Logger LOG = LoggerFactory.getLogger(ElanStatisticsImpl.class);
35
36     private final IInterfaceManager interfaceManager;
37     private final ElanInterfaceCache elanInterfaceCache;
38
39     @Inject
40     public ElanStatisticsImpl(IInterfaceManager interfaceManager,
41             ElanInterfaceCache elanInterfaceCache) {
42         this.interfaceManager = interfaceManager;
43         this.elanInterfaceCache = elanInterfaceCache;
44     }
45
46     @Override
47     public ListenableFuture<RpcResult<GetElanInterfaceStatisticsOutput>> getElanInterfaceStatistics(
48         GetElanInterfaceStatisticsInput input) {
49         String interfaceName = input.getInterfaceName();
50         LOG.debug("getElanInterfaceStatistics is called for elan interface {}", interfaceName);
51         RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder = null;
52         if (interfaceName == null) {
53             rpcResultBuilder = RpcResultBuilder.failed();
54             return getFutureWithAppErrorMessage(rpcResultBuilder, "Interface name is not provided");
55         }
56         Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
57         if (!elanInterface.isPresent()) {
58             rpcResultBuilder = RpcResultBuilder.failed();
59             return getFutureWithAppErrorMessage(rpcResultBuilder,
60                     String.format("Interface %s is not a ELAN interface", interfaceName));
61         }
62         String elanInstanceName = elanInterface.get().getElanInstanceName();
63         InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
64         //FIXME [ELANBE] Get this API Later
65         short tableId = 0;
66 //        try {
67 //
68 //            //tableId = interfaceManager.getTableIdForService(interfaceName, serviceInfo);
69 //        } catch (InterfaceNotFoundException | InterfaceServiceNotFoundException e) {
70 //            rpcResultBuilder = RpcResultBuilder.failed();
71 //            return getFutureWithAppErrorMessage(rpcResultBuilder,
72 //                String.format("Interface %s or Service %s doesn't exist", interfaceName, serviceInfo));
73 //        }
74         if (!interfaceInfo.isOperational()) {
75             LOG.debug("interface {} is down and returning with no statistics", interfaceName);
76             rpcResultBuilder = RpcResultBuilder.success();
77             return rpcResultBuilder.withResult(new GetElanInterfaceStatisticsOutputBuilder()
78                             .setStatResult(
79                                     new StatResultBuilder().setStatResultCode(ResultCode.NotFound).setByteRxCount(0L)
80                                             .setByteTxCount(0L).setPacketRxCount(0L).setPacketTxCount(0L).build())
81                             .build()).buildFuture();
82         }
83         rpcResultBuilder = RpcResultBuilder.success();
84         return Futures.immediateFuture(rpcResultBuilder
85                 .withResult(queryforElanInterfaceStatistics(tableId, elanInstanceName, interfaceInfo)).build());
86     }
87
88     @Nullable
89     private GetElanInterfaceStatisticsOutput queryforElanInterfaceStatistics(short tableId, String elanInstanceName,
90             InterfaceInfo interfaceInfo) {
91 //        BigInteger dpId = interfaceInfo.getDpId();
92 //        List<MatchInfo> matches;
93 //        String interfaceName = interfaceInfo.getInterfaceName();
94 //        if (interfaceInfo instanceof VlanInterfaceInfo) {
95 //            VlanInterfaceInfo vlanInterfaceInfo = (VlanInterfaceInfo)interfaceInfo;
96 //            matches = InterfaceServiceUtil.getMatchInfoForVlanLPort(dpId, interfaceInfo.getPortNo(),
97 //                InterfaceServiceUtil.getVlanId(interfaceName, dataBroker), vlanInterfaceInfo.isVlanTransparent());
98 //        } else {
99 //            matches = InterfaceServiceUtil.getLPortDispatcherMatches(
100 //                    ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME, NwConstants.ELAN_SERVICE_INDEX),
101 //                    interfaceInfo.getInterfaceTag());
102 //        }
103 //        long groupId = interfaceInfo.getGroupId();
104 //        Set<Object> statRequestKeys = InterfaceServiceUtil.getStatRequestKeys(dpId, tableId, matches,
105 //                String.format("%s.%s", elanInstanceName, interfaceName), groupId);
106         // StatisticsInfo statsInfo = new StatisticsInfo(statRequestKeys);
107 //        org.opendaylight.vpnservice.ericsson.mdsalutil.statistics.StatResult statResult
108 //            = mdsalMgr.queryForStatistics(interfaceName, statsInfo);
109 //        ResultCode resultCode = ResultCode.Success;
110 //        if (!statResult.isComplete()) {
111 //            resultCode = ResultCode.Incomplete;
112 //        }
113
114         //StatValue ingressFlowStats = statResult.getStatResult(InterfaceServiceUtil
115 //            .getFlowStatisticsKey(dpId, tableId, matches, elanInstanceName));
116         //StatValue groupStats = statResult.getStatResult(InterfaceServiceUtil.getGroupStatisticsKey(dpId, groupId));
117 //      return new GetElanInterfaceStatisticsOutputBuilder().setStatResult(new
118 //          StatResultBuilder().setStatResultCode(resultCode)
119 //                .setByteRxCount(ingressFlowStats.getByteCount()).setPacketRxCount(ingressFlowStats.getPacketCount())
120 //                .setByteTxCount(groupStats.getByteCount()).setPacketTxCount(groupStats.getPacketCount()).build())
121 //                .build();
122         return null;
123     }
124
125     private ListenableFuture<RpcResult<GetElanInterfaceStatisticsOutput>> getFutureWithAppErrorMessage(
126         RpcResultBuilder<GetElanInterfaceStatisticsOutput> rpcResultBuilder, String message) {
127         rpcResultBuilder.withError(ErrorType.APPLICATION, message);
128         return rpcResultBuilder.buildFuture();
129     }
130
131 }