Bug 5010: NeutronVpn: Internal VPN delete/recreate redesign + related fixes
[vpnservice.git] / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / vpnservice / neutronvpn / NeutronRouterChangeListener.java
1 /*
2  * Copyright (c) 2016 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.vpnservice.neutronvpn;
9
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30
31 public class NeutronRouterChangeListener extends AbstractDataChangeListener<Router> implements AutoCloseable {
32     private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
33
34     private ListenerRegistration<DataChangeListener> listenerRegistration;
35     private final DataBroker broker;
36     private NeutronvpnManager nvpnManager;
37
38
39     public NeutronRouterChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
40         super(Router.class);
41         broker = db;
42         nvpnManager = nVpnMgr;
43         registerListener(db);
44     }
45
46     @Override
47     public void close() throws Exception {
48         if (listenerRegistration != null) {
49             try {
50                 listenerRegistration.close();
51             } catch (final Exception e) {
52                 LOG.error("Error when cleaning up DataChangeListener.", e);
53             }
54             listenerRegistration = null;
55         }
56         LOG.info("N_Router listener Closed");
57     }
58
59
60     private void registerListener(final DataBroker db) {
61         try {
62             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
63                     InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class),
64                     NeutronRouterChangeListener.this, DataChangeScope.SUBTREE);
65         } catch (final Exception e) {
66             LOG.error("Neutron Manager Router DataChange listener registration fail!", e);
67             throw new IllegalStateException("Neutron Manager Router DataChange listener registration failed.", e);
68         }
69     }
70
71     @Override
72     protected void add(InstanceIdentifier<Router> identifier, Router input) {
73         if (LOG.isTraceEnabled()) {
74             LOG.trace("Adding Router : key: " + identifier + ", value=" + input);
75         }
76         // Create internal VPN
77         nvpnManager.createL3Vpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
78     }
79
80     @Override
81     protected void remove(InstanceIdentifier<Router> identifier, Router input) {
82         if (LOG.isTraceEnabled()) {
83             LOG.trace("Removing router : key: " + identifier + ", value=" + input);
84         }
85         Uuid routerId = input.getUuid();
86         // fetch subnets associated to router
87         List<Interfaces> routerInterfaces = input.getInterfaces();
88         List<Uuid> routerSubnetIds = new ArrayList<Uuid>();
89         if (routerInterfaces != null) {
90             for (Interfaces rtrIf : routerInterfaces) {
91                 routerSubnetIds.add(rtrIf.getSubnetId());
92             }
93         }
94         nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
95     }
96
97     @Override
98     protected void update(InstanceIdentifier<Router> identifier, Router original, Router update) {
99         if (LOG.isTraceEnabled()) {
100             LOG.trace("Updating Router : key: " + identifier + ", original value=" + original + ", update value=" +
101                     update);
102         }
103         Uuid routerId = update.getUuid();
104         Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
105         // internal vpn always present in case external vpn not found
106         if (vpnId == null) {
107             vpnId = routerId;
108         }
109         List<Interfaces> oldInterfaces = (original.getInterfaces() != null) ? original.getInterfaces() : new
110                 ArrayList<Interfaces>();
111         List<Interfaces> newInterfaces = (update.getInterfaces() != null) ? update.getInterfaces() : new
112                 ArrayList<Interfaces>();
113         List<String> oldRoutes = (original.getRoutes() != null) ? original.getRoutes() : new ArrayList<String>();
114         List<String> newRoutes = (update.getRoutes() != null) ? update.getRoutes() : new ArrayList<String>();
115
116         if (!oldInterfaces.equals(newInterfaces)) {
117             for (Interfaces intrf : newInterfaces) {
118                 if (!oldInterfaces.remove(intrf)) {
119                     // add new subnet
120                     nvpnManager.addSubnetToVpn(vpnId, intrf.getSubnetId());
121                 }
122             }
123             //clear remaining old subnets
124             for (Interfaces intrf : oldInterfaces) {
125                 nvpnManager.removeSubnetFromVpn(vpnId, intrf.getSubnetId());
126             }
127         }
128         if (!oldRoutes.equals(newRoutes)) {
129             Iterator<String> iterator = newRoutes.iterator();
130             while (iterator.hasNext()) {
131                 String route = iterator.next();
132                 if (oldRoutes.remove(route)) {
133                     iterator.remove();
134                 }
135             }
136             nvpnManager.addAdjacencyforExtraRoute(newRoutes, true, null);
137             if (!oldRoutes.isEmpty()) {
138                 nvpnManager.removeAdjacencyforExtraRoute(oldRoutes);
139             }
140         }
141     }
142 }