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