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.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
21 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
22 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
23 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
24 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
36 ClusteredDataTreeChangeListener<Acl> {
38 private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
39 private final AclServiceManager aclServiceManager;
40 private final DataBroker dataBroker;
43 public AclEventListener(final AclServiceManager aclServiceManager, DataBroker dataBroker) {
44 super(Acl.class, AclEventListener.class);
45 this.aclServiceManager = aclServiceManager;
46 this.dataBroker = dataBroker;
50 // TODO new interface Lifecyle
52 LOG.info("{} start", getClass().getSimpleName());
53 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
58 // TODO make AsyncDataTreeChangeListenerBase implement new interface Lifecyle
59 public void close() throws Exception {
64 protected InstanceIdentifier<Acl> getWildCardPath() {
65 return InstanceIdentifier
66 .create(AccessLists.class)
71 protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
72 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
76 protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
77 List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
78 // find and update added ace rules in acl
79 List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
80 updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
81 if (interfaceList != null && AclClusterUtil.isEntityOwner()) {
82 updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
84 // find and update deleted ace rules in acl
85 List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
86 if (interfaceList != null && AclClusterUtil.isEntityOwner()) {
87 updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
89 updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
93 private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
94 if (null != aceList && !aceList.isEmpty()) {
95 LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
96 for (AclInterface port : interfaceList) {
97 for (Ace aceRule : aceList) {
98 aclServiceManager.notifyAce(port, action, aceRule);
105 protected void add(InstanceIdentifier<Acl> key, Acl acl) {
106 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
109 private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
110 if (null == aceList) {
113 for (Ace ace : aceList) {
114 SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
115 if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
116 if (action == AclServiceManager.Action.ADD) {
117 AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
119 AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
126 protected AclEventListener getDataTreeChangeListener() {
130 private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
131 if (updatedAcl == null) {
134 List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
135 if (currentAcl == null) {
136 return updatedAceList;
138 List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
139 for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
140 Ace ace1 = iterator.next();
141 for (Ace ace2 : currentAceList) {
142 if (ace1.getRuleName().equals(ace2.getRuleName())) {
147 return updatedAceList;