2 * Copyright (c) 2016 Red Hat, Inc. 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.aclservice;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.genius.mdsalutil.ActionInfo;
15 import org.opendaylight.genius.mdsalutil.ActionType;
16 import org.opendaylight.genius.mdsalutil.FlowEntity;
17 import org.opendaylight.genius.mdsalutil.InstructionInfo;
18 import org.opendaylight.genius.mdsalutil.InstructionType;
19 import org.opendaylight.genius.mdsalutil.MDSALUtil;
20 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
23 import org.opendaylight.netvirt.aclservice.api.AclServiceListener;
24 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
25 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
26 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
27 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public abstract class AbstractAclServiceImpl implements AclServiceListener {
38 private static final Logger LOG = LoggerFactory.getLogger(AbstractAclServiceImpl.class);
40 private final IMdsalApiManager mdsalManager;
41 private final Class<? extends ServiceModeBase> serviceMode;
42 protected final DataBroker dataBroker;
45 * Initialize the member variables.
47 * @param serviceMode the service mode
48 * @param dataBroker the data broker instance.
49 * @param mdsalManager the mdsal manager instance.
51 public AbstractAclServiceImpl(Class<? extends ServiceModeBase> serviceMode, DataBroker dataBroker,
52 IMdsalApiManager mdsalManager) {
53 this.dataBroker = dataBroker;
54 this.mdsalManager = mdsalManager;
55 this.serviceMode = serviceMode;
59 public boolean applyAcl(AclInterface port) {
60 BigInteger dpId = port.getDpId();
62 LOG.error("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
66 programAclWithAllowedAddress(dpId, port.getAllowedAddressPairs(), port.getLPortTag(), port.getSecurityGroups(),
67 Action.ADD, NwConstants.ADD_FLOW);
69 // TODO: uncomment bindservice() when the acl flow programming is
71 // bindService(port.getName());
76 public boolean updateAcl(AclInterface portBefore, AclInterface portAfter) {
77 boolean result = false;
78 boolean isPortSecurityEnable = portAfter.getPortSecurityEnabled();
79 boolean isPortSecurityEnableBefore = portBefore.getPortSecurityEnabled();
80 // if port security is changed, apply/remove Acls
81 if (isPortSecurityEnableBefore != isPortSecurityEnable) {
82 if (isPortSecurityEnable) {
83 result = applyAcl(portAfter);
85 result = removeAcl(portAfter);
87 } else if (isPortSecurityEnable) {
88 // Acls has been updated, find added/removed Acls and act accordingly.
89 processInterfaceUpdate(portBefore, portAfter);
95 private void processInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
96 BigInteger dpId = portAfter.getDpId();
97 List<AllowedAddressPairs> addedAllowedAddressPairs =
98 AclServiceUtils.getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(),
99 portBefore.getAllowedAddressPairs());
100 List<AllowedAddressPairs> deletedAllowedAddressPairs =
101 AclServiceUtils.getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(),
102 portAfter.getAllowedAddressPairs());
103 if (addedAllowedAddressPairs != null && !addedAllowedAddressPairs.isEmpty()) {
104 programAclWithAllowedAddress(dpId, addedAllowedAddressPairs, portAfter.getLPortTag(),
105 portAfter.getSecurityGroups(), Action.UPDATE, NwConstants.ADD_FLOW);
107 if (deletedAllowedAddressPairs != null && !deletedAllowedAddressPairs.isEmpty()) {
108 programAclWithAllowedAddress(dpId, deletedAllowedAddressPairs, portAfter.getLPortTag(),
109 portAfter.getSecurityGroups(), Action.UPDATE, NwConstants.DEL_FLOW);
112 List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(portAfter.getSecurityGroups(),
113 portBefore.getSecurityGroups());
114 List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(portBefore.getSecurityGroups(),
115 portAfter.getSecurityGroups());
116 if (addedAcls != null && !addedAcls.isEmpty()) {
117 updateCustomRules(dpId, portAfter.getLPortTag(), addedAcls, NwConstants.ADD_FLOW);
119 if (deletedAcls != null && !deletedAcls.isEmpty()) {
120 updateCustomRules(dpId, portAfter.getLPortTag(), deletedAcls, NwConstants.DEL_FLOW);
124 private void updateCustomRules(BigInteger dpId, int lportTag, List<Uuid> aclUuidList, int action) {
125 programAclRules(aclUuidList, dpId, lportTag, action);
128 private void programAclWithAllowedAddress(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
129 int lportTag, List<Uuid> aclUuidList, Action action, int addOrRemove) {
130 programFixedRules(dpId, "", allowedAddresses, lportTag, action, addOrRemove);
131 if (action == Action.ADD || action == Action.REMOVE) {
132 programAclRules(aclUuidList, dpId, lportTag, addOrRemove);
137 public boolean removeAcl(AclInterface port) {
138 BigInteger dpId = port.getDpId();
140 LOG.error("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
144 programAclWithAllowedAddress(dpId, port.getAllowedAddressPairs(), port.getLPortTag(), port.getSecurityGroups(),
145 Action.REMOVE, NwConstants.DEL_FLOW);
146 // TODO: uncomment unbindService() when the acl flow programming is
148 // unbindService(port.getName());
153 public boolean applyAce(AclInterface port, Ace ace) {
154 if (!port.isPortSecurityEnabled()) {
157 programAceRule(port.getDpId(), port.getLPortTag(), NwConstants.ADD_FLOW, ace);
162 public boolean removeAce(AclInterface port, Ace ace) {
163 if (!port.isPortSecurityEnabled()) {
166 programAceRule(port.getDpId(), port.getLPortTag(), NwConstants.DEL_FLOW, ace);
174 * @param interfaceName the interface name
176 protected abstract void bindService(String interfaceName);
181 * @param interfaceName the interface name
183 protected abstract void unbindService(String interfaceName);
186 * Program the default anti-spoofing rule and the conntrack rules.
188 * @param dpid the dpid
189 * @param dhcpMacAddress the dhcp mac address.
190 * @param allowedAddresses the allowed addresses
191 * @param lportTag the lport tag
192 * @param action add/modify/remove action
193 * @param addOrRemove addorRemove
195 protected abstract void programFixedRules(BigInteger dpid, String dhcpMacAddress,
196 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove);
199 * Programs the acl custom rules.
201 * @param aclUuidList the list of acl uuid to be applied
202 * @param dpId the dpId
203 * @param lportTag the lport tag
204 * @param addOrRemove whether to delete or add flow
206 protected abstract void programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove);
209 * Programs the ace custom rule.
211 * @param dpId the dpId
212 * @param lportTag the lport tag
213 * @param addOrRemove whether to delete or add flow
214 * @param ace rule to be program
216 protected abstract void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace);
219 * Writes/remove the flow to/from the datastore.
220 * @param dpId the dpId
221 * @param tableId the tableId
222 * @param flowId the flowId
223 * @param priority the priority
224 * @param flowName the flow name
225 * @param idleTimeOut the idle timeout
226 * @param hardTimeOut the hard timeout
227 * @param cookie the cookie
228 * @param matches the list of matches to be writted
229 * @param instructions the list of instruction to be written.
230 * @param addOrRemove add or remove the entries.
232 protected void syncFlow(BigInteger dpId, short tableId, String flowId, int priority, String flowName,
233 int idleTimeOut, int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase> matches,
234 List<InstructionInfo> instructions, int addOrRemove) {
235 if (addOrRemove == NwConstants.DEL_FLOW) {
236 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId,flowId,
237 priority, flowName , idleTimeOut, hardTimeOut, cookie, matches, null);
238 LOG.trace("Removing Acl Flow DpnId {}, flowId {}", dpId, flowId);
239 mdsalManager.removeFlow(flowEntity);
241 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId,
242 priority, flowName, idleTimeOut, hardTimeOut, cookie, matches, instructions);
243 LOG.trace("Installing DpnId {}, flowId {}", dpId, flowId);
244 mdsalManager.installFlow(flowEntity);
249 * Gets the dispatcher table resubmit instructions based on ingress/egress
250 * service mode w.r.t switch.
252 * @param actionsInfos the actions infos
253 * @return the instructions for dispatcher table resubmit
255 protected List<InstructionInfo> getDispatcherTableResubmitInstructions(List<ActionInfo> actionsInfos) {
256 short dispatcherTableId = NwConstants.LPORT_DISPATCHER_TABLE;
257 if (ServiceModeEgress.class.equals(this.serviceMode)) {
258 dispatcherTableId = AclConstants.EGRESS_LPORT_DISPATCHER_TABLE;
261 List<InstructionInfo> instructions = new ArrayList<>();
262 actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
263 instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));