NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanDpnInterfacesListener.java
1 /*
2  * Copyright (c) 2017 Ericsson 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
9 package org.opendaylight.netvirt.elan.internal;
10
11 import static java.util.Collections.emptyList;
12
13 import java.util.List;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
18 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
19 import org.opendaylight.infrautils.utils.concurrent.Executors;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
23 import org.opendaylight.netvirt.elan.utils.ElanUtils;
24 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.Uint64;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Singleton
35 public class ElanDpnInterfacesListener
36         extends AbstractAsyncDataTreeChangeListener<DpnInterfaces> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfacesListener.class);
39     private final DataBroker dataBroker;
40     private final IInterfaceManager interfaceManager;
41     private final ElanServiceProvider elanService;
42     private final JobCoordinator jobCoordinator;
43     private final ElanInstanceCache elanInstanceCache;
44
45     @Inject
46     public ElanDpnInterfacesListener(final DataBroker dataBroker, final IInterfaceManager interfaceManager,
47                                      final ElanServiceProvider elanService, final JobCoordinator jobCoordinator,
48                                      final ElanInstanceCache elanInstanceCache) {
49         super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ElanDpnInterfaces.class)
50                 .child(ElanDpnInterfacesList.class).child(DpnInterfaces.class),
51                 Executors.newListeningSingleThreadExecutor("ElanDpnInterfacesListener", LOG));
52         this.dataBroker = dataBroker;
53         this.interfaceManager = interfaceManager;
54         this.elanService = elanService;
55         this.jobCoordinator = jobCoordinator;
56         this.elanInstanceCache = elanInstanceCache;
57     }
58
59     public void start() {
60         LOG.info("{} start", getClass().getSimpleName());
61     }
62
63     @Override
64     public void remove(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
65
66     }
67
68     @Override
69     public void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
70                           DpnInterfaces update) {
71         LOG.debug("received Dpninterfaces update event for dpn {}", update.getDpId());
72         Uint64 dpnId = update.getDpId();
73         String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
74         ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
75
76         if (elanInstance != null && !elanInstance.isExternal() && ElanUtils.isVlan(elanInstance)) {
77             List<String> interfaces = update.getInterfaces();
78             // trigger deletion for vlan provider intf on the DPN for the vlan provider network
79             if (interfaces != null && interfaces.size() == 1 && interfaceManager.isExternalInterface(
80                     interfaces.get(0))) {
81                 LOG.debug("deleting vlan prv intf for elan {}, dpn {}", elanInstanceName, dpnId);
82                 jobCoordinator.enqueueJob(dpnId.toString(), () -> {
83                     elanService.deleteExternalElanNetwork(elanInstance, dpnId);
84                     return emptyList();
85                 });
86             }
87         }
88     }
89
90     @Override
91     public void add(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
92         LOG.debug("received Dpninterfaces add event for dpn {}", dpnInterfaces.getDpId());
93         Uint64 dpnId = dpnInterfaces.getDpId();
94         String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
95         ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
96
97         // trigger creation of vlan provider intf for the vlan provider network
98         // on br-int patch port for this DPN
99         if (elanInstance != null && !elanInstance.isExternal() && ElanUtils.isVlan(elanInstance)) {
100             jobCoordinator.enqueueJob(dpnId.toString(), () -> {
101                 LOG.debug("creating vlan member intf for elan {}, dpn {}",
102                         elanInstance.getPhysicalNetworkName(), dpnId);
103                 elanService.createExternalElanNetwork(elanInstance, dpnId);
104                 return emptyList();
105             });
106         }
107     }
108
109     @Override
110     @PreDestroy
111     public void close() {
112         super.close();
113         Executors.shutdownAndAwaitTermination(getExecutorService());
114     }
115 }