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