Multiple fixes in various modules
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / RouterInterfaceListener.java
1 /*\r
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.netvirt.vpnmanager;\r
9 \r
10 import com.google.common.util.concurrent.ListenableFuture;\r
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;\r
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;\r
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;\r
16 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;\r
17 import org.opendaylight.genius.mdsalutil.MDSALUtil;\r
18 import org.opendaylight.netvirt.vpnmanager.utilities.InterfaceUtils;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;\r
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;\r
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
24 import org.slf4j.Logger;\r
25 import org.slf4j.LoggerFactory;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 import java.util.concurrent.Callable;\r
30 \r
31 public class RouterInterfaceListener extends AbstractDataChangeListener<Interfaces> {\r
32     private static final Logger LOG = LoggerFactory.getLogger(RouterInterfaceListener.class);\r
33     private ListenerRegistration<DataChangeListener> listenerRegistration;\r
34     private DataBroker broker;\r
35     private VpnInterfaceManager vpnInterfaceManager;\r
36 \r
37     public RouterInterfaceListener(final DataBroker db) {\r
38         super(Interfaces.class);\r
39         broker = db;\r
40         registerListener(db);\r
41     }\r
42 \r
43     void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {\r
44         this.vpnInterfaceManager = vpnInterfaceManager;\r
45     }\r
46 \r
47     private void registerListener(final DataBroker db) {\r
48         try {\r
49             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,\r
50                     getWildCardPath(), RouterInterfaceListener.this, DataChangeScope.SUBTREE);\r
51         } catch (final Exception e) {\r
52             LOG.error("Router interface DataChange listener registration fail !", e);\r
53         }\r
54     }\r
55 \r
56     private InstanceIdentifier<?> getWildCardPath() {\r
57         return InstanceIdentifier.create(RouterInterfacesMap.class).child(RouterInterfaces.class).child(Interfaces.class);\r
58     }\r
59 \r
60     @Override\r
61     protected void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {\r
62         LOG.trace("Add event - key: {}, value: {}", identifier, interfaceInfo);\r
63         final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();\r
64         final String interfaceName = interfaceInfo.getInterfaceId();\r
65         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =\r
66                 InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);\r
67         if (interfaceState != null) {\r
68             DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();\r
69             dataStoreCoordinator.enqueueJob(interfaceName,\r
70                     new Callable<List<ListenableFuture<Void>>>() {\r
71                         @Override\r
72                         public List<ListenableFuture<Void>> call() throws Exception {\r
73                             WriteTransaction writeTxn = broker.newWriteOnlyTransaction();\r
74                             LOG.debug("Handling interface {} in router {} add scenario", interfaceName, routerId);\r
75                             writeTxn.put(LogicalDatastoreType.CONFIGURATION,\r
76                                     VpnUtil.getRouterInterfaceId(interfaceName),\r
77                                     VpnUtil.getRouterInterface(interfaceName, routerId), true);\r
78                             vpnInterfaceManager.addToNeutronRouterDpnsMap(routerId, interfaceName, writeTxn);\r
79                             List<ListenableFuture<Void>> futures = new ArrayList<>();\r
80                             futures.add(writeTxn.submit());\r
81                             return futures;\r
82                         }\r
83                     });\r
84         } else {\r
85             LOG.warn("Interface {} not yet operational to handle router interface add event in router {}", interfaceName, routerId);\r
86         }\r
87     }\r
88 \r
89     @Override\r
90     protected void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {\r
91         LOG.trace("Remove event - key: {}, value: {}", identifier, interfaceInfo);\r
92         final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();\r
93         final String interfaceName = interfaceInfo.getInterfaceId();\r
94         DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();\r
95         dataStoreCoordinator.enqueueJob(interfaceName,\r
96                 new Callable<List<ListenableFuture<Void>>>() {\r
97                     @Override\r
98                     public List<ListenableFuture<Void>> call() throws Exception {\r
99                         WriteTransaction writeTxn = broker.newWriteOnlyTransaction();\r
100                         vpnInterfaceManager.removeFromNeutronRouterDpnsMap(routerId, interfaceName, writeTxn);\r
101                         List<ListenableFuture<Void>> futures = new ArrayList<>();\r
102                         futures.add(writeTxn.submit());\r
103                         return futures;\r
104                     }\r
105                 });\r
106     }\r
107 \r
108     @Override\r
109     protected void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {\r
110         LOG.trace("Update event - key: {}, original: {}, update: {}", identifier, original, update);\r
111     }\r
112 \r
113 }\r