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.api.utils.AclInterfaceCacheUtil;
29 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
30 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
31 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public abstract class AbstractAclServiceImpl implements AclServiceListener {
42 private static final Logger LOG = LoggerFactory.getLogger(AbstractAclServiceImpl.class);
44 protected final IMdsalApiManager mdsalManager;
45 protected final DataBroker dataBroker;
46 protected final Class<? extends ServiceModeBase> serviceMode;
49 * Initialize the member variables.
54 * the data broker instance.
56 * the mdsal manager instance.
58 public AbstractAclServiceImpl(Class<? extends ServiceModeBase> serviceMode, DataBroker dataBroker,
59 IMdsalApiManager mdsalManager) {
60 this.dataBroker = dataBroker;
61 this.mdsalManager = mdsalManager;
62 this.serviceMode = serviceMode;
66 public boolean applyAcl(AclInterface port) {
68 LOG.error("port cannot be null");
71 BigInteger dpId = port.getDpId();
72 if (dpId == null || port.getLPortTag() == null) {
73 LOG.error("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
77 programAclWithAllowedAddress(dpId, port.getAllowedAddressPairs(), port.getLPortTag(), port.getSecurityGroups(),
78 Action.ADD, NwConstants.ADD_FLOW, port.getInterfaceId());
82 // TODO: uncomment bindservice() when the acl flow programming is
84 // bindService(port.getName());
88 public boolean updateAcl(AclInterface portBefore, AclInterface portAfter) {
89 boolean result = false;
90 boolean isPortSecurityEnable = portAfter.getPortSecurityEnabled();
91 boolean isPortSecurityEnableBefore = portBefore.getPortSecurityEnabled();
92 // if port security is changed, apply/remove Acls
93 if (isPortSecurityEnableBefore != isPortSecurityEnable) {
94 if (isPortSecurityEnable) {
95 result = applyAcl(portAfter);
97 result = removeAcl(portAfter);
99 } else if (isPortSecurityEnable) {
100 // Acls has been updated, find added/removed Acls and act accordingly.
101 processInterfaceUpdate(portBefore, portAfter);
107 private void processInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
108 BigInteger dpId = portAfter.getDpId();
109 List<AllowedAddressPairs> addedAllowedAddressPairs =
110 AclServiceUtils.getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(),
111 portBefore.getAllowedAddressPairs());
112 List<AllowedAddressPairs> deletedAllowedAddressPairs =
113 AclServiceUtils.getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(),
114 portAfter.getAllowedAddressPairs());
115 if (addedAllowedAddressPairs != null && !addedAllowedAddressPairs.isEmpty()) {
116 programAclWithAllowedAddress(dpId, addedAllowedAddressPairs, portAfter.getLPortTag(),
117 portAfter.getSecurityGroups(), Action.UPDATE, NwConstants.ADD_FLOW, portAfter.getInterfaceId());
119 if (deletedAllowedAddressPairs != null && !deletedAllowedAddressPairs.isEmpty()) {
120 programAclWithAllowedAddress(dpId, deletedAllowedAddressPairs, portAfter.getLPortTag(),
121 portAfter.getSecurityGroups(), Action.UPDATE, NwConstants.DEL_FLOW, portAfter.getInterfaceId());
124 List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(portAfter.getSecurityGroups(),
125 portBefore.getSecurityGroups());
126 List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(portBefore.getSecurityGroups(),
127 portAfter.getSecurityGroups());
128 if (addedAcls != null && !addedAcls.isEmpty()) {
129 updateCustomRules(dpId, portAfter.getLPortTag(), addedAcls, NwConstants.ADD_FLOW,
130 portAfter.getInterfaceId(), portAfter.getAllowedAddressPairs());
132 if (deletedAcls != null && !deletedAcls.isEmpty()) {
133 updateCustomRules(dpId, portAfter.getLPortTag(), deletedAcls, NwConstants.DEL_FLOW,
134 portAfter.getInterfaceId(), portAfter.getAllowedAddressPairs());
138 private void updateCustomRules(BigInteger dpId, int lportTag, List<Uuid> aclUuidList, int action,
139 String portId, List<AllowedAddressPairs> syncAllowedAddresses) {
140 programAclRules(aclUuidList, dpId, lportTag, action, portId);
141 syncRemoteAclRules(aclUuidList, action, portId, syncAllowedAddresses);
144 private void syncRemoteAclRules(List<Uuid> aclUuidList, int action, String currentPortId,
145 List<AllowedAddressPairs> syncAllowedAddresses) {
146 for (Uuid remoteAclId : aclUuidList) {
147 Set<String> portSet = AclDataUtil.getRemoteAclInterfaces(remoteAclId);
148 if (portSet == null) {
151 for (String remotePortId : portSet) {
152 AclInterface port = AclInterfaceCacheUtil.getAclInterfaceFromCache(remotePortId);
153 if (currentPortId.equals(port.getInterfaceId())) {
156 List<Ace> remoteAceList = AclServiceUtils.getAceWithRemoteAclId(dataBroker, port, remoteAclId);
157 for (Ace ace : remoteAceList) {
158 programAceRule(port.getDpId(), port.getLPortTag(), action, ace, port.getInterfaceId(),
159 syncAllowedAddresses);
165 private void programAclWithAllowedAddress(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
166 int lportTag, List<Uuid> aclUuidList, Action action, int addOrRemove,
168 programFixedRules(dpId, "", allowedAddresses, lportTag, action, addOrRemove);
169 if (action == Action.ADD || action == Action.REMOVE) {
170 programAclRules(aclUuidList, dpId, lportTag, addOrRemove, portId);
172 syncRemoteAclRules(aclUuidList, addOrRemove, portId, allowedAddresses);
177 public boolean removeAcl(AclInterface port) {
178 BigInteger dpId = port.getDpId();
180 LOG.error("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
184 programAclWithAllowedAddress(dpId, port.getAllowedAddressPairs(), port.getLPortTag(), port.getSecurityGroups(),
185 Action.REMOVE, NwConstants.DEL_FLOW, port.getInterfaceId());
188 // TODO: uncomment unbindService() when the acl flow programming is
190 // unbindService(port.getName());
194 public boolean applyAce(AclInterface port, Ace ace) {
195 if (!port.isPortSecurityEnabled()) {
198 programAceRule(port.getDpId(), port.getLPortTag(), NwConstants.ADD_FLOW, ace,
199 port.getInterfaceId(), null);
204 public boolean removeAce(AclInterface port, Ace ace) {
205 if (!port.isPortSecurityEnabled()) {
208 programAceRule(port.getDpId(), port.getLPortTag(), NwConstants.DEL_FLOW, ace,
209 port.getInterfaceId(), null);
216 * @param interfaceName
219 protected abstract void bindService(String interfaceName);
224 * @param interfaceName
227 protected abstract void unbindService(String interfaceName);
230 * Program the default anti-spoofing rule and the conntrack rules.
232 * @param dpid the dpid
233 * @param dhcpMacAddress the dhcp mac address.
234 * @param allowedAddresses the allowed addresses
235 * @param lportTag the lport tag
236 * @param action add/modify/remove action
237 * @param addOrRemove addorRemove
239 protected abstract void programFixedRules(BigInteger dpid, String dhcpMacAddress,
240 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove);
243 * Programs the acl custom rules.
245 * @param aclUuidList the list of acl uuid to be applied
246 * @param dpId the dpId
247 * @param lportTag the lport tag
248 * @param addOrRemove whether to delete or add flow
250 protected abstract boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove,
254 * Programs the ace custom rule.
256 * @param dpId the dpId
257 * @param lportTag the lport tag
258 * @param addOrRemove whether to delete or add flow
259 * @param ace rule to be program
261 protected abstract void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
262 List<AllowedAddressPairs> syncAllowedAddresses);
265 * Writes/remove the flow to/from the datastore.
284 * the list of matches to be writted
285 * @param instructions
286 * the list of instruction to be written.
288 * add or remove the entries.
290 protected void syncFlow(BigInteger dpId, short tableId, String flowId, int priority, String flowName,
291 int idleTimeOut, int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase> matches,
292 List<InstructionInfo> instructions, int addOrRemove) {
293 if (addOrRemove == NwConstants.DEL_FLOW) {
294 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority, flowName, idleTimeOut,
295 hardTimeOut, cookie, matches, null);
296 LOG.trace("Removing Acl Flow DpnId {}, flowId {}", dpId, flowId);
297 mdsalManager.removeFlow(flowEntity);
299 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority, flowName, idleTimeOut,
300 hardTimeOut, cookie, matches, instructions);
301 LOG.trace("Installing DpnId {}, flowId {}", dpId, flowId);
302 mdsalManager.installFlow(flowEntity);
307 * Gets the dispatcher table resubmit instructions based on ingress/egress
308 * service mode w.r.t switch.
310 * @param actionsInfos the actions infos
311 * @return the instructions for dispatcher table resubmit
313 protected List<InstructionInfo> getDispatcherTableResubmitInstructions(List<ActionInfo> actionsInfos) {
314 short dispatcherTableId = NwConstants.LPORT_DISPATCHER_TABLE;
315 if (ServiceModeEgress.class.equals(this.serviceMode)) {
316 dispatcherTableId = AclConstants.EGRESS_LPORT_DISPATCHER_TABLE;
319 List<InstructionInfo> instructions = new ArrayList<>();
320 actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString(dispatcherTableId)}));
321 instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
325 protected String getOperAsString(int flowOper) {
328 case NwConstants.ADD_FLOW:
331 case NwConstants.DEL_FLOW:
334 case NwConstants.MOD_FLOW: