2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.listeners;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
21 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
22 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
23 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
24 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
25 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
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.idmanager.rev160406.IdManagerService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
38 ClusteredDataTreeChangeListener<Acl> {
40 private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
42 private final AclServiceManager aclServiceManager;
43 private final AclClusterUtil aclClusterUtil;
44 private final DataBroker dataBroker;
45 private final AclDataUtil aclDataUtil;
46 private final IdManagerService idManager;
49 public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker,
50 AclDataUtil aclDataUtil, IdManagerService idManager) {
51 super(Acl.class, AclEventListener.class);
52 this.aclServiceManager = aclServiceManager;
53 this.aclClusterUtil = aclClusterUtil;
54 this.dataBroker = dataBroker;
55 this.aclDataUtil = aclDataUtil;
56 this.idManager = idManager;
62 LOG.info("{} start", getClass().getSimpleName());
63 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
67 protected InstanceIdentifier<Acl> getWildCardPath() {
68 return InstanceIdentifier
69 .create(AccessLists.class)
74 protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
75 updateAclFlowPriorityCache(acl.getAclName(), AclServiceManager.Action.REMOVE);
76 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
80 protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
81 String aclName = aclAfter.getAclName();
82 List<AclInterface> interfaceList = aclDataUtil.getInterfaceList(new Uuid(aclName));
83 // find and update added ace rules in acl
84 List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
85 updateRemoteAclCache(addedAceRules, aclName, AclServiceManager.Action.ADD);
86 if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
87 updateAceRules(interfaceList, aclName, addedAceRules, AclServiceManager.Action.ADD);
89 // find and update deleted ace rules in acl
90 List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
91 if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
92 updateAceRules(interfaceList, aclName, deletedAceRules, AclServiceManager.Action.REMOVE);
94 updateRemoteAclCache(deletedAceRules, aclName, AclServiceManager.Action.REMOVE);
98 private void updateAceRules(List<AclInterface> interfaceList, String aclName, List<Ace> aceList,
99 AclServiceManager.Action action) {
100 if (null != aceList && !aceList.isEmpty()) {
101 LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
102 for (AclInterface port : interfaceList) {
103 for (Ace aceRule : aceList) {
104 aclServiceManager.notifyAce(port, action, aclName, aceRule);
111 protected void add(InstanceIdentifier<Acl> key, Acl acl) {
112 updateAclFlowPriorityCache(acl.getAclName(), AclServiceManager.Action.ADD);
113 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
117 * Update remote acl cache.
119 * @param aceList the ace list
120 * @param aclName the acl name
121 * @param action the action
123 private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
124 if (null == aceList) {
127 for (Ace ace : aceList) {
128 SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
129 if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
130 if (action == AclServiceManager.Action.ADD) {
131 aclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
133 aclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
140 * Update acl flow priority cache.
142 * @param aclName the acl name
143 * @param action the action
145 private void updateAclFlowPriorityCache(String aclName, AclServiceManager.Action action) {
146 if (action == AclServiceManager.Action.ADD) {
147 Integer flowPriority =
148 AclServiceUtils.allocateId(this.idManager, AclConstants.ACL_FLOW_PRIORITY_POOL_NAME, aclName);
149 aclDataUtil.addAclFlowPriority(aclName, flowPriority);
151 AclServiceUtils.releaseId(this.idManager, AclConstants.ACL_FLOW_PRIORITY_POOL_NAME, aclName);
152 aclDataUtil.removeAclFlowPriority(aclName);
157 protected AclEventListener getDataTreeChangeListener() {
161 private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
162 if (updatedAcl == null) {
165 List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
166 if (currentAcl == null) {
167 return updatedAceList;
169 List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
170 for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
171 Ace ace1 = iterator.next();
172 for (Ace ace2 : currentAceList) {
173 if (ace1.getRuleName().equals(ace2.getRuleName())) {
178 return updatedAceList;