NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / statistics / impl / src / main / java / org / opendaylight / netvirt / statistics / EgressCountersServiceImpl.java
1 /*
2  * Copyright (c) 2017 HPE, 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.netvirt.statistics;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
16 import org.opendaylight.genius.mdsalutil.ActionInfo;
17 import org.opendaylight.genius.mdsalutil.InstructionInfo;
18 import org.opendaylight.genius.mdsalutil.MDSALUtil;
19 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
20 import org.opendaylight.genius.mdsalutil.NwConstants;
21 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26
27 public class EgressCountersServiceImpl extends AbstractCountersService {
28
29     public EgressCountersServiceImpl(DataBroker db, IInterfaceManager interfaceManager,
30             IMdsalApiManager mdsalApiManager) {
31         super(db, interfaceManager, mdsalApiManager);
32     }
33
34     @Override
35     public void bind(String interfaceId) {
36         int instructionKey = 0;
37         List<Instruction> instructions = new ArrayList<>();
38         instructions
39                 .add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.EGRESS_COUNTERS_TABLE, ++instructionKey));
40         BoundServices serviceInfo =
41                 CountersServiceUtils.getBoundServices(String.format("%s.%s", "counters-egress", interfaceId),
42                         CountersServiceUtils.EGRESS_COUNTERS_SERVICE_INDEX,
43                         CountersServiceUtils.EGRESS_COUNTERS_DEFAULT_FLOW_PRIORITY,
44                         CountersServiceUtils.COOKIE_COUNTERS_BASE, instructions);
45         InstanceIdentifier<BoundServices> serviceId = CountersServiceUtils.buildServiceId(interfaceId,
46                 CountersServiceUtils.EGRESS_COUNTERS_SERVICE_INDEX, ServiceModeEgress.class);
47         MDSALUtil.syncWrite(db, LogicalDatastoreType.CONFIGURATION, serviceId, serviceInfo);
48     }
49
50     @Override
51     public void installDefaultCounterRules(String interfaceId) {
52         BigInteger dpn = interfaceManager.getDpnForInterface(interfaceId);
53         String defaultFlowName = CountersServiceUtils.DEFAULT_EGRESS_COUNTER_FLOW_PREFIX + dpn + interfaceId;
54         List<MatchInfoBase> defaultFlowMatches = new ArrayList<>();
55         List<ActionInfo> actionsInfos = new ArrayList<>();
56         List<InstructionInfo> instructions = CountersServiceUtils.getDispatcherTableResubmitInstructions(actionsInfos,
57                 ElementCountersDirection.EGRESS);
58         syncFlow(dpn, NwConstants.EGRESS_COUNTERS_TABLE, defaultFlowName,
59                 CountersServiceUtils.COUNTER_TABLE_DEFAULT_FLOW_PRIORITY, CountersServiceUtils.COUNTER_FLOW_NAME, 0, 0,
60                 CountersServiceUtils.COOKIE_COUNTERS_BASE, defaultFlowMatches, instructions, NwConstants.ADD_FLOW);
61     }
62
63     @Override
64     public void unBindService(String interfaceId) {
65         InstanceIdentifier<BoundServices> serviceId = CountersServiceUtils.buildServiceId(interfaceId,
66                 CountersServiceUtils.EGRESS_COUNTERS_SERVICE_INDEX, ServiceModeEgress.class);
67         MDSALUtil.syncDelete(db, LogicalDatastoreType.CONFIGURATION, serviceId);
68     }
69
70     @Override
71     public void syncCounterFlows(ElementCountersRequest ecr, int operation) {
72         int lportTag = ecr.getLportTag();
73         List<MatchInfoBase> flowMatches =
74                 CountersServiceUtils.getCounterFlowMatch(ecr, lportTag, ElementCountersDirection.EGRESS);
75         List<ActionInfo> actionsInfos = new ArrayList<>();
76         List<InstructionInfo> instructions = CountersServiceUtils.getDispatcherTableResubmitInstructions(actionsInfos,
77                 ElementCountersDirection.EGRESS);
78
79         BigInteger dpn = ecr.getDpn();
80         String flowName = createFlowName(ecr, lportTag, dpn);
81
82         syncFlow(dpn, NwConstants.EGRESS_COUNTERS_TABLE, flowName,
83                 CountersServiceUtils.COUNTER_TABLE_COUNTER_FLOW_PRIORITY, CountersServiceUtils.COUNTER_FLOW_NAME, 0, 0,
84                 CountersServiceUtils.COOKIE_COUNTERS_BASE, flowMatches, instructions, operation);
85     }
86
87     private String createFlowName(ElementCountersRequest ecr, int lportTag, BigInteger dpn) {
88         return "Egress_Counters" + dpn + "_" + lportTag + "_" + ecr.toString().hashCode();
89     }
90 }