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