Migrate to serviceutils/tools and serviceutils/srm
[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.PostConstruct;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
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.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Singleton
33 public class AclElanInterfaceListener extends AsyncDataTreeChangeListenerBase<ElanInterface, AclElanInterfaceListener>
34         implements ClusteredDataTreeChangeListener<ElanInterface>, RecoverableListener {
35     private static final Logger LOG = LoggerFactory.getLogger(AclElanInterfaceListener.class);
36
37     private final AclServiceManager aclServiceManager;
38     private final AclClusterUtil aclClusterUtil;
39     private final DataBroker dataBroker;
40     private final AclInterfaceCache aclInterfaceCache;
41
42     @Inject
43     public AclElanInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
44             DataBroker dataBroker, AclInterfaceCache aclInterfaceCache,
45             ServiceRecoveryRegistry serviceRecoveryRegistry) {
46         super(ElanInterface.class, AclElanInterfaceListener.class);
47         this.aclServiceManager = aclServiceManager;
48         this.aclClusterUtil = aclClusterUtil;
49         this.dataBroker = dataBroker;
50         this.aclInterfaceCache = aclInterfaceCache;
51         serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
52     }
53
54     @Override
55     @PostConstruct
56     public void init() {
57         LOG.info("{} start", getClass().getSimpleName());
58         registerListener();
59     }
60
61     @Override
62     public void registerListener() {
63         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
64     }
65
66     @Override
67     protected InstanceIdentifier<ElanInterface> getWildCardPath() {
68         return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
69     }
70
71     @Override
72     protected void remove(InstanceIdentifier<ElanInterface> key, ElanInterface dataObjectModification) {
73         // do nothing
74     }
75
76     @Override
77     protected void update(InstanceIdentifier<ElanInterface> key, ElanInterface dataObjectModificationBefore,
78             ElanInterface dataObjectModificationAfter) {
79         // do nothing
80     }
81
82     @Override
83     protected 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                 builder.elanId(elanInfo.getElanTag());
90                 return true;
91             }
92
93             return false;
94         });
95
96         if (aclInterface == null) {
97             LOG.debug("On Add event, ignore if AclInterface was not found in cache or was not updated");
98             return;
99         }
100
101         if (aclInterface.getDpId() != null && aclClusterUtil.isEntityOwner()) {
102             // Notify ADD flows, if InterfaceStateListener has processed before ELAN-ID getting populated
103             LOG.debug("On add event, notify ACL service manager to BIND/ADD ACL for interface: {}", aclInterface);
104             aclServiceManager.notify(aclInterface, null, Action.BIND);
105             aclServiceManager.notify(aclInterface, null, Action.ADD);
106         }
107     }
108
109     @Override
110     protected AclElanInterfaceListener getDataTreeChangeListener() {
111         return this;
112     }
113 }