NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / policyservice / impl / src / main / java / org / opendaylight / netvirt / policyservice / util / PolicyServiceFlowUtil.java
1 /*
2  * Copyright (c) 2017 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.policyservice.util;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18
19 import org.opendaylight.mdsal.binding.api.WriteTransaction;
20 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
21 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
22 import org.opendaylight.genius.mdsalutil.ActionInfo;
23 import org.opendaylight.genius.mdsalutil.FlowEntity;
24 import org.opendaylight.genius.mdsalutil.GroupEntity;
25 import org.opendaylight.genius.mdsalutil.InstructionInfo;
26 import org.opendaylight.genius.mdsalutil.MDSALUtil;
27 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
28 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
29 import org.opendaylight.genius.mdsalutil.NwConstants;
30 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
31 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
32 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
33 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
34 import org.opendaylight.genius.mdsalutil.instructions.InstructionWriteMetadata;
35 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
36 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
37 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchRegister;
38 import org.opendaylight.netvirt.elanmanager.api.IElanService;
39 import org.opendaylight.netvirt.policyservice.PolicyServiceConstants;
40 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @Singleton
48 public class PolicyServiceFlowUtil {
49     private static final Logger LOG = LoggerFactory.getLogger(PolicyServiceFlowUtil.class);
50
51     private final IMdsalApiManager mdsalManager;
52     private final IInterfaceManager interfaceManager;
53     private final IElanService elanService;
54     private final IVpnManager vpnManager;
55
56     @Inject
57     public PolicyServiceFlowUtil(final IMdsalApiManager mdsalManager, final IInterfaceManager interfaceManager,
58             final IElanService elanService, final IVpnManager vpnManager) {
59         this.mdsalManager = mdsalManager;
60         this.interfaceManager = interfaceManager;
61         this.elanService = elanService;
62         this.vpnManager = vpnManager;
63     }
64
65     public List<InstructionInfo> getTableMissInstructions() {
66         List<ActionInfo> actions = Collections
67                 .singletonList(new ActionNxResubmit(NwConstants.EGRESS_LPORT_DISPATCHER_TABLE));
68         return Collections.singletonList(new InstructionApplyActions(actions));
69     }
70
71     public List<InstructionInfo> getPolicyClassifierInstructions(long policyClassifierId) {
72         List<InstructionInfo> instructions = new ArrayList<>();
73         instructions.add(new InstructionWriteMetadata(MetaDataUtil.getPolicyClassifierMetaData(policyClassifierId),
74                 MetaDataUtil.METADATA_MASK_POLICY_CLASSIFER_ID));
75         instructions.add(new InstructionGotoTable(NwConstants.EGRESS_POLICY_ROUTING_TABLE));
76         return instructions;
77     }
78
79     public List<MatchInfoBase> getPolicyRouteMatches(long policyClassifierId, int lportTag) {
80         List<MatchInfoBase> matches = new ArrayList<>();
81         matches.add(new NxMatchRegister(NxmNxReg6.class, lportTag, MetaDataUtil.getLportTagMaskForReg6()));
82         matches.add(new MatchMetadata(MetaDataUtil.getPolicyClassifierMetaData(policyClassifierId),
83                 MetaDataUtil.METADATA_MASK_POLICY_CLASSIFER_ID));
84         return matches;
85     }
86
87     public List<InstructionInfo> getPolicyRouteInstructions(long groupId) {
88         List<InstructionInfo> instructions = new ArrayList<>();
89         instructions.add(new InstructionApplyActions(Collections.singletonList(new ActionGroup(groupId))));
90         return instructions;
91     }
92
93     public List<MatchInfoBase> getIngressInterfaceMatches(String ingressInterface) {
94         InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(ingressInterface);
95         if (interfaceInfo == null) {
96             LOG.warn("No interface info found for {}", ingressInterface);
97             return Collections.emptyList();
98         }
99
100         int lportTag = interfaceInfo.getInterfaceTag();
101         return Collections.singletonList(
102                 new MatchMetadata(MetaDataUtil.getMetadataLPort(lportTag), MetaDataUtil.METADATA_MASK_LPORT_TAG));
103     }
104
105     public List<MatchInfoBase> getElanInstanceMatches(String elanInstanceName) {
106         return elanService.getEgressMatchesForElanInstance(elanInstanceName);
107     }
108
109     public List<MatchInfoBase> getVpnInstanceMatches(String vpnInstanceName) {
110         return vpnManager.getEgressMatchesForVpn(vpnInstanceName);
111     }
112
113     public void updateFlowToTx(BigInteger dpId, short tableId, String flowName, int priority, BigInteger cookie,
114             List<? extends MatchInfoBase> matches, List<InstructionInfo> instructions, int addOrRemove,
115             WriteTransaction tx) {
116         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowName, priority, flowName, 0, 0, cookie,
117                 matches, instructions);
118         if (addOrRemove == NwConstants.ADD_FLOW) {
119             mdsalManager.addFlowToTx(flowEntity, tx);
120         } else {
121             mdsalManager.removeFlowToTx(flowEntity, tx);
122         }
123     }
124
125     public void updateGroupToTx(BigInteger dpId, long groupId, String groupName, GroupTypes groupType, int addOrRemove,
126             WriteTransaction tx) {
127         if (addOrRemove == NwConstants.ADD_FLOW && mdsalManager.groupExists(dpId, groupId)) {
128             LOG.trace("Group {} id {} already exists", groupName, groupId);
129             return;
130         }
131
132         GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpId, groupId, groupName, groupType,
133                 Collections.emptyList() /*listBucketInfo*/);
134         if (addOrRemove == NwConstants.ADD_FLOW) {
135             LOG.debug("Add group {} to DPN {}", groupId, dpId);
136             mdsalManager.addGroupToTx(groupEntity, tx);
137         } else {
138             LOG.debug("Remove group {} from DPN {}", groupId, dpId);
139             mdsalManager.removeGroupToTx(groupEntity, tx);
140         }
141     }
142
143     public void updateInterfaceBucketToTx(BigInteger dpId, long groupId, int bucketId, String interfaceName,
144             int addOrRemove, WriteTransaction tx) {
145         if (groupId == PolicyServiceConstants.INVALID_ID) {
146             LOG.error("No valid group id found for interface {} DPN {}", interfaceName, dpId);
147             return;
148         }
149
150         if (addOrRemove == NwConstants.DEL_FLOW) {
151             LOG.debug("Remove bucket for interface {} from group {} DPN {}", interfaceName, groupId, dpId);
152             mdsalManager.removeBucketToTx(dpId, groupId, bucketId, tx);
153             return;
154         }
155
156         List<ActionInfo> egressActions = interfaceManager.getInterfaceEgressActions(interfaceName);
157         if (egressActions == null || egressActions.isEmpty()) {
158             LOG.error("Failed to get egress actions for interface {} DPN {}", interfaceName, dpId);
159             return;
160         }
161
162         Long port = interfaceManager.getPortForInterface(interfaceName);
163         if (port == null) {
164             LOG.error("Failed to get port for interface {}", interfaceName);
165             return;
166         }
167
168         Bucket bucket = MDSALUtil.buildBucket(MDSALUtil.buildActions(egressActions), MDSALUtil.GROUP_WEIGHT, bucketId,
169                 port, MDSALUtil.WATCH_GROUP);
170         LOG.debug("Add bucket id {} for interface {} to group {} DPN {}", bucketId, interfaceName, groupId, dpId);
171         mdsalManager.addBucketToTx(dpId, groupId, bucket, tx);
172     }
173 }