2 * Copyright (c) 2017 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 javax.annotation.PreDestroy;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.infrautils.utils.concurrent.Executors;
14 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
18 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
19 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
20 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
21 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
22 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
23 import org.opendaylight.serviceutils.srm.RecoverableListener;
24 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
25 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class AclElanInterfaceListener extends AbstractAsyncDataTreeChangeListener<ElanInterface>
35 implements ClusteredDataTreeChangeListener<ElanInterface>, RecoverableListener {
36 private static final Logger LOG = LoggerFactory.getLogger(AclElanInterfaceListener.class);
38 private final AclServiceManager aclServiceManager;
39 private final AclClusterUtil aclClusterUtil;
40 private final DataBroker dataBroker;
41 private final AclInterfaceCache aclInterfaceCache;
44 public AclElanInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
45 DataBroker dataBroker, AclInterfaceCache aclInterfaceCache,
46 ServiceRecoveryRegistry serviceRecoveryRegistry) {
47 super(dataBroker, LogicalDatastoreType.CONFIGURATION,
48 InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class),
49 Executors.newListeningSingleThreadExecutor("AclElanInterfaceListener", LOG));
50 this.aclServiceManager = aclServiceManager;
51 this.aclClusterUtil = aclClusterUtil;
52 this.dataBroker = dataBroker;
53 this.aclInterfaceCache = aclInterfaceCache;
54 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
58 LOG.info("{} start", getClass().getSimpleName());
62 public void registerListener() {
67 public void deregisterListener() {
72 public void remove(InstanceIdentifier<ElanInterface> key, ElanInterface dataObjectModification) {
77 public void update(InstanceIdentifier<ElanInterface> key, ElanInterface dataObjectModificationBefore,
78 ElanInterface dataObjectModificationAfter) {
83 public void add(InstanceIdentifier<ElanInterface> key, ElanInterface elanInterface) {
84 String interfaceId = elanInterface.getName();
85 AclInterface aclInterface = aclInterfaceCache.updateIfPresent(interfaceId, (prevAclInterface, builder) -> {
86 if (prevAclInterface.getElanId() == null) {
87 ElanInstance elanInfo = AclServiceUtils.getElanInstanceByName(elanInterface.getElanInstanceName(),
89 if (elanInfo != null) {
90 builder.elanId(elanInfo.getElanTag().toJava());
98 if (aclInterface == null) {
99 LOG.debug("On Add event, ignore if AclInterface was not found in cache or was not updated");
103 if (aclInterface.getDpId() != null && aclClusterUtil.isEntityOwner()) {
104 // Notify ADD flows, if InterfaceStateListener has processed before ELAN-ID getting populated
105 LOG.debug("On add event, notify ACL service manager to BIND/ADD ACL for interface: {}", aclInterface);
106 aclServiceManager.notify(aclInterface, null, Action.BIND);
107 aclServiceManager.notify(aclInterface, null, Action.ADD);
113 public void close() {
115 Executors.shutdownAndAwaitTermination(getExecutorService());