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;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.mdsalutil.ActionInfo;
17 import org.opendaylight.genius.mdsalutil.ActionType;
18 import org.opendaylight.genius.mdsalutil.FlowEntity;
19 import org.opendaylight.genius.mdsalutil.InstructionInfo;
20 import org.opendaylight.genius.mdsalutil.InstructionType;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.netvirt.aclservice.api.AclServiceListener;
26 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
27 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
28 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
29 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public abstract class AbstractAclServiceImpl implements AclServiceListener {
40 private static final Logger LOG = LoggerFactory.getLogger(AbstractAclServiceImpl.class);
42 protected final IMdsalApiManager mdsalManager;
43 protected final DataBroker dataBroker;
44 protected final Class<? extends ServiceModeBase> serviceMode;
45 protected final AclDataUtil aclDataUtil;
46 protected final AclServiceUtils aclServiceUtils;
49 * Initialize the member variables.
54 * the data broker instance.
56 * the mdsal manager instance.
59 * @param aclServiceUtils
60 * the acl service util.
62 public AbstractAclServiceImpl(Class<? extends ServiceModeBase> serviceMode, DataBroker dataBroker,
63 IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil, AclServiceUtils aclServiceUtils) {
64 this.dataBroker = dataBroker;
65 this.mdsalManager = mdsalManager;
66 this.serviceMode = serviceMode;
67 this.aclDataUtil = aclDataUtil;
68 this.aclServiceUtils = aclServiceUtils;
72 public boolean applyAcl(AclInterface port) {
74 LOG.error("port cannot be null");
77 if (port.getSecurityGroups() == null) {
78 LOG.error("port security groups cannot be null");
81 BigInteger dpId = port.getDpId();
82 if (dpId == null || port.getLPortTag() == null) {
83 LOG.error("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
86 programAclWithAllowedAddress(dpId, port.getAllowedAddressPairs(), port.getLPortTag(), port.getSecurityGroups(),
87 Action.ADD, NwConstants.ADD_FLOW, port.getInterfaceId());
89 bindService(port.getInterfaceId());
94 public boolean updateAcl(AclInterface portBefore, AclInterface portAfter) {
95 boolean result = true;
96 boolean isPortSecurityEnable = portAfter.getPortSecurityEnabled();
97 boolean isPortSecurityEnableBefore = portBefore.getPortSecurityEnabled();
98 // if port security is changed, apply/remove Acls
99 if (isPortSecurityEnableBefore != isPortSecurityEnable) {
100 if (isPortSecurityEnable) {
101 result = applyAcl(portAfter);
103 result = removeAcl(portAfter);
105 } else if (isPortSecurityEnable) {
106 // Acls has been updated, find added/removed Acls and act accordingly.
107 processInterfaceUpdate(portBefore, portAfter);
113 private void processInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
114 BigInteger dpId = portAfter.getDpId();
115 List<AllowedAddressPairs> addedAllowedAddressPairs =
116 AclServiceUtils.getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(),
117 portBefore.getAllowedAddressPairs());
118 List<AllowedAddressPairs> deletedAllowedAddressPairs =
119 AclServiceUtils.getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(),
120 portAfter.getAllowedAddressPairs());
121 if (addedAllowedAddressPairs != null && !addedAllowedAddressPairs.isEmpty()) {
122 programAclWithAllowedAddress(dpId, addedAllowedAddressPairs, portAfter.getLPortTag(),
123 portAfter.getSecurityGroups(), Action.UPDATE, NwConstants.ADD_FLOW, portAfter.getInterfaceId());
125 if (deletedAllowedAddressPairs != null && !deletedAllowedAddressPairs.isEmpty()) {
126 programAclWithAllowedAddress(dpId, deletedAllowedAddressPairs, portAfter.getLPortTag(),
127 portAfter.getSecurityGroups(), Action.UPDATE, NwConstants.DEL_FLOW, portAfter.getInterfaceId());
130 List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(portAfter.getSecurityGroups(),
131 portBefore.getSecurityGroups());
132 List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(portBefore.getSecurityGroups(),
133 portAfter.getSecurityGroups());
134 if (deletedAcls != null && !deletedAcls.isEmpty()) {
135 updateCustomRules(dpId, portAfter.getLPortTag(), deletedAcls, NwConstants.DEL_FLOW,
136 portAfter.getInterfaceId(), portAfter.getAllowedAddressPairs());
138 if (addedAcls != null && !addedAcls.isEmpty()) {
139 updateCustomRules(dpId, portAfter.getLPortTag(), addedAcls, NwConstants.ADD_FLOW,
140 portAfter.getInterfaceId(), portAfter.getAllowedAddressPairs());
144 private void updateCustomRules(BigInteger dpId, int lportTag, List<Uuid> aclUuidList, int action,
145 String portId, List<AllowedAddressPairs> syncAllowedAddresses) {
146 programAclRules(aclUuidList, dpId, lportTag, action, portId);
147 syncRemoteAclRules(aclUuidList, action, portId, syncAllowedAddresses);
150 private void syncRemoteAclRules(List<Uuid> aclUuidList, int action, String currentPortId,
151 List<AllowedAddressPairs> syncAllowedAddresses) {
152 if (aclUuidList == null) {
153 LOG.warn("security groups are null");
156 for (Uuid remoteAclId : aclUuidList) {
157 Set<AclInterface> portSet = aclDataUtil.getRemoteAclInterfaces(remoteAclId);
158 if (portSet == null) {
161 for (AclInterface port : portSet) {
162 if (currentPortId.equals(port.getInterfaceId())) {
165 List<Ace> remoteAceList = AclServiceUtils.getAceWithRemoteAclId(dataBroker, port, remoteAclId);
166 for (Ace ace : remoteAceList) {
167 programAceRule(port.getDpId(), port.getLPortTag(), action, ace, port.getInterfaceId(),
168 syncAllowedAddresses);
174 private void programAclWithAllowedAddress(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
175 int lportTag, List<Uuid> aclUuidList, Action action, int addOrRemove,
177 programGeneralFixedRules(dpId, "", allowedAddresses, lportTag, action, addOrRemove);
178 programSpecificFixedRules(dpId, "", allowedAddresses, lportTag, portId, action, addOrRemove);
179 if (action == Action.ADD || action == Action.REMOVE) {
180 programAclRules(aclUuidList, dpId, lportTag, addOrRemove, portId);
182 syncRemoteAclRules(aclUuidList, addOrRemove, portId, allowedAddresses);
187 public boolean removeAcl(AclInterface port) {
188 BigInteger dpId = port.getDpId();
190 LOG.error("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
193 programAclWithAllowedAddress(dpId, port.getAllowedAddressPairs(), port.getLPortTag(), port.getSecurityGroups(),
194 Action.REMOVE, NwConstants.DEL_FLOW, port.getInterfaceId());
196 unbindService(port.getInterfaceId());
201 public boolean applyAce(AclInterface port, Ace ace) {
202 if (!port.isPortSecurityEnabled()) {
205 programAceRule(port.getDpId(), port.getLPortTag(), NwConstants.ADD_FLOW, ace,
206 port.getInterfaceId(), null);
211 public boolean removeAce(AclInterface port, Ace ace) {
212 if (!port.isPortSecurityEnabled()) {
215 programAceRule(port.getDpId(), port.getLPortTag(), NwConstants.DEL_FLOW, ace,
216 port.getInterfaceId(), null);
223 * @param interfaceName
226 protected abstract void bindService(String interfaceName);
231 * @param interfaceName
234 protected abstract void unbindService(String interfaceName);
237 * Program the default anti-spoofing rules.
239 * @param dpid the dpid
240 * @param dhcpMacAddress the dhcp mac address.
241 * @param allowedAddresses the allowed addresses
242 * @param lportTag the lport tag
243 * @param action add/modify/remove action
244 * @param addOrRemove addorRemove
246 protected abstract void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
247 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove);
250 * Program the default specific rules.
252 * @param dpid the dpid
253 * @param dhcpMacAddress the dhcp mac address.
254 * @param allowedAddresses the allowed addresses
255 * @param lportTag the lport tag
256 * @param portId the port id
257 * @param action add/modify/remove action
258 * @param addOrRemove addorRemove
260 protected abstract void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
261 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove);
264 * Programs the acl custom rules.
266 * @param aclUuidList the list of acl uuid to be applied
267 * @param dpId the dpId
268 * @param lportTag the lport tag
269 * @param addOrRemove whether to delete or add flow
270 * @param portId the port id
271 * @return program succeeded
273 protected abstract boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove,
277 * Programs the ace custom rule.
279 * @param dpId the dpId
280 * @param lportTag the lport tag
281 * @param addOrRemove whether to delete or add flow
282 * @param ace rule to be program
283 * @param portId the port id
284 * @param syncAllowedAddresses the allowed addresses
286 protected abstract void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
287 List<AllowedAddressPairs> syncAllowedAddresses);
290 * Writes/remove the flow to/from the datastore.
309 * the list of matches to be writted
310 * @param instructions
311 * the list of instruction to be written.
313 * add or remove the entries.
315 protected void syncFlow(BigInteger dpId, short tableId, String flowId, int priority, String flowName,
316 int idleTimeOut, int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase> matches,
317 List<InstructionInfo> instructions, int addOrRemove) {
318 if (addOrRemove == NwConstants.DEL_FLOW) {
319 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority, flowName, idleTimeOut,
320 hardTimeOut, cookie, matches, null);
321 LOG.trace("Removing Acl Flow DpnId {}, flowId {}", dpId, flowId);
322 mdsalManager.removeFlow(flowEntity);
324 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority, flowName, idleTimeOut,
325 hardTimeOut, cookie, matches, instructions);
326 LOG.trace("Installing DpnId {}, flowId {}", dpId, flowId);
327 mdsalManager.installFlow(flowEntity);
332 * Gets the dispatcher table resubmit instructions based on ingress/egress
333 * service mode w.r.t switch.
335 * @param actionsInfos the actions infos
336 * @return the instructions for dispatcher table resubmit
338 protected List<InstructionInfo> getDispatcherTableResubmitInstructions(List<ActionInfo> actionsInfos) {
339 short dispatcherTableId = NwConstants.LPORT_DISPATCHER_TABLE;
340 if (ServiceModeEgress.class.equals(this.serviceMode)) {
341 dispatcherTableId = NwConstants.EGRESS_LPORT_DISPATCHER_TABLE;
344 List<InstructionInfo> instructions = new ArrayList<>();
345 actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
346 instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
350 protected String getOperAsString(int flowOper) {
353 case NwConstants.ADD_FLOW:
356 case NwConstants.DEL_FLOW:
359 case NwConstants.MOD_FLOW: