NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclElanInterfaceListener.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.aclservice.listeners;
9
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;
32
33 @Singleton
34 public class AclElanInterfaceListener extends AbstractAsyncDataTreeChangeListener<ElanInterface>
35         implements ClusteredDataTreeChangeListener<ElanInterface>, RecoverableListener {
36     private static final Logger LOG = LoggerFactory.getLogger(AclElanInterfaceListener.class);
37
38     private final AclServiceManager aclServiceManager;
39     private final AclClusterUtil aclClusterUtil;
40     private final DataBroker dataBroker;
41     private final AclInterfaceCache aclInterfaceCache;
42
43     @Inject
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);
55     }
56
57     public void init() {
58         LOG.info("{} start", getClass().getSimpleName());
59     }
60
61     @Override
62     public void registerListener() {
63         super.register();
64     }
65
66     @Override
67     public void deregisterListener() {
68         super.close();
69     }
70
71     @Override
72     public void remove(InstanceIdentifier<ElanInterface> key, ElanInterface dataObjectModification) {
73         // do nothing
74     }
75
76     @Override
77     public void update(InstanceIdentifier<ElanInterface> key, ElanInterface dataObjectModificationBefore,
78             ElanInterface dataObjectModificationAfter) {
79         // do nothing
80     }
81
82     @Override
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(),
88                         dataBroker);
89                 if (elanInfo != null) {
90                     builder.elanId(elanInfo.getElanTag().toJava());
91                 }
92                 return true;
93             }
94
95             return false;
96         });
97
98         if (aclInterface == null) {
99             LOG.debug("On Add event, ignore if AclInterface was not found in cache or was not updated");
100             return;
101         }
102
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);
108         }
109     }
110
111     @Override
112     @PreDestroy
113     public void close() {
114         super.close();
115         Executors.shutdownAndAwaitTermination(getExecutorService());
116     }
117 }