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 AclClusterUtil aclClusterUtil;
41 private final DataBroker dataBroker;
44 public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker) {
45 super(Acl.class, AclEventListener.class);
46 this.aclServiceManager = aclServiceManager;
47 this.aclClusterUtil = aclClusterUtil;
48 this.dataBroker = dataBroker;
52 // TODO new interface Lifecyle
54 LOG.info("{} start", getClass().getSimpleName());
55 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
60 // TODO make AsyncDataTreeChangeListenerBase implement new interface Lifecyle
61 public void close() throws Exception {
66 protected InstanceIdentifier<Acl> getWildCardPath() {
67 return InstanceIdentifier
68 .create(AccessLists.class)
73 protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
74 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
78 protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
79 List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
80 // find and update added ace rules in acl
81 List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
82 updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
83 if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
84 updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
86 // find and update deleted ace rules in acl
87 List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
88 if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
89 updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
91 updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
95 private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
96 if (null != aceList && !aceList.isEmpty()) {
97 LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
98 for (AclInterface port : interfaceList) {
99 for (Ace aceRule : aceList) {
100 aclServiceManager.notifyAce(port, action, aceRule);
107 protected void add(InstanceIdentifier<Acl> key, Acl acl) {
108 updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
111 private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
112 if (null == aceList) {
115 for (Ace ace : aceList) {
116 SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
117 if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
118 if (action == AclServiceManager.Action.ADD) {
119 AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
121 AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
128 protected AclEventListener getDataTreeChangeListener() {
132 private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
133 if (updatedAcl == null) {
136 List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
137 if (currentAcl == null) {
138 return updatedAceList;
140 List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
141 for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
142 Ace ace1 = iterator.next();
143 for (Ace ace2 : currentAceList) {
144 if (ace1.getRuleName().equals(ace2.getRuleName())) {
149 return updatedAceList;