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.AclDataUtil;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
35 ClusteredDataTreeChangeListener<Acl> {
37 private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
38 private final AclServiceManager aclServiceManager;
39 private final AclClusterUtil aclClusterUtil;
40 private final DataBroker dataBroker;
43 public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker) {
44 super(Acl.class, AclEventListener.class);
45 this.aclServiceManager = aclServiceManager;
46 this.aclClusterUtil = aclClusterUtil;
47 this.dataBroker = dataBroker;
53 LOG.info("{} start", getClass().getSimpleName());
54 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
58 protected InstanceIdentifier<Acl> getWildCardPath() {
59 return InstanceIdentifier
60 .create(AccessLists.class)
65 protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
66 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
70 protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
71 List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
72 // find and update added ace rules in acl
73 List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
74 updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
75 if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
76 updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
78 // find and update deleted ace rules in acl
79 List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
80 if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
81 updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
83 updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
87 private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
88 if (null != aceList && !aceList.isEmpty()) {
89 LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
90 for (AclInterface port : interfaceList) {
91 for (Ace aceRule : aceList) {
92 aclServiceManager.notifyAce(port, action, aceRule);
99 protected void add(InstanceIdentifier<Acl> key, Acl acl) {
100 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
103 private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
104 if (null == aceList) {
107 for (Ace ace : aceList) {
108 SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
109 if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
110 if (action == AclServiceManager.Action.ADD) {
111 AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
113 AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
120 protected AclEventListener getDataTreeChangeListener() {
124 private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
125 if (updatedAcl == null) {
128 List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
129 if (currentAcl == null) {
130 return updatedAceList;
132 List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
133 for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
134 Ace ace1 = iterator.next();
135 for (Ace ace2 : currentAceList) {
136 if (ace1.getRuleName().equals(ace2.getRuleName())) {
141 return updatedAceList;