Fixup Augmentable and Identifiable methods changing
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanInterfaceConfigListener.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.elan.internal;
9
10 import com.google.common.base.Optional;
11 import java.util.Collections;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
20 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
21 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
22 import org.opendaylight.netvirt.elan.utils.ElanConstants;
23 import org.opendaylight.netvirt.elan.utils.ElanUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
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 ElanInterfaceConfigListener
34     extends AsyncDataTreeChangeListenerBase<Interface, ElanInterfaceConfigListener> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceConfigListener.class);
37
38     private final DataBroker dataBroker;
39     private final ManagedNewTransactionRunner txRunner;
40     private final ElanInterfaceManager elanInterfaceManager;
41     private final JobCoordinator jobCoordinator;
42     private final ElanInterfaceCache elanInterfaceCache;
43
44     @Inject
45     public ElanInterfaceConfigListener(DataBroker dataBroker, ElanInterfaceManager elanInterfaceManager,
46             JobCoordinator jobCoordinator, ElanInterfaceCache elanInterfaceCache) {
47         super(Interface.class, ElanInterfaceConfigListener.class);
48         this.dataBroker = dataBroker;
49         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
50         this.elanInterfaceManager = elanInterfaceManager;
51         this.jobCoordinator = jobCoordinator;
52         this.elanInterfaceCache = elanInterfaceCache;
53     }
54
55     @Override
56     @PostConstruct
57     public void init() {
58         LOG.info("ElanInterfaceConfigListener init");
59         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
60     }
61
62     @Override
63     protected InstanceIdentifier<Interface> getWildCardPath() {
64         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
65     }
66
67     @Override
68     protected void remove(InstanceIdentifier<Interface> key, Interface intrf) {
69         // Sometimes elan service is not unbound on the interface when the user does nova delete followed
70         // by neutron port delete since interface config is deleted a bit later. so adding logic to
71         // unbind service for interface config removal.
72         if (intrf == null || intrf.augmentation(IfL2vlan.class) == null) {
73             LOG.debug("The interface {} is not a L2 interface. Ignoring it", intrf);
74             return;
75         }
76
77         String interfaceName = intrf.getName();
78         Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
79         if (!elanInterface.isPresent()) {
80             LOG.debug("There is no ELAN service for interface {}. Ignoring it", interfaceName);
81             return;
82         }
83         jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(interfaceName),
84             () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
85                 LOG.debug("unbinding elan service on interface {} for its config removal", interfaceName);
86                 elanInterfaceManager.unbindService(interfaceName, tx);
87             })), ElanConstants.JOB_MAX_RETRIES);
88     }
89
90     @Override
91     protected void update(InstanceIdentifier<Interface> key, Interface dataObjectModificationBefore,
92             Interface dataObjectModificationAfter) {
93         // Not required to handle this event
94     }
95
96     @Override
97     protected void add(InstanceIdentifier<Interface> key, Interface dataObjectModification) {
98         // Not required to handle this event
99     }
100
101     @Override
102     protected ElanInterfaceConfigListener getDataTreeChangeListener() {
103         return this;
104     }
105
106 }