826f9f839b52a35c0082c336f631031fe79ff456
[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.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMap;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31
32 public class NeutronRouterChangeListener extends AbstractDataChangeListener<Router> implements AutoCloseable {
33     private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
34
35     private ListenerRegistration<DataChangeListener> listenerRegistration;
36     private final DataBroker broker;
37     private NeutronvpnManager nvpnManager;
38
39
40     public NeutronRouterChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
41         super(Router.class);
42         broker = db;
43         nvpnManager = nVpnMgr;
44         registerListener(db);
45     }
46
47     @Override
48     public void close() throws Exception {
49         if (listenerRegistration != null) {
50             try {
51                 listenerRegistration.close();
52             } catch (final Exception e) {
53                 LOG.error("Error when cleaning up DataChangeListener.", e);
54             }
55             listenerRegistration = null;
56         }
57         LOG.info("N_Router listener Closed");
58     }
59
60
61     private void registerListener(final DataBroker db) {
62         try {
63             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
64                     InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class),
65                     NeutronRouterChangeListener.this, DataChangeScope.SUBTREE);
66         } catch (final Exception e) {
67             LOG.error("Neutron Manager Router DataChange listener registration fail!", e);
68             throw new IllegalStateException("Neutron Manager Router DataChange listener registration failed.", e);
69         }
70     }
71
72     @Override
73     protected void add(InstanceIdentifier<Router> identifier, Router input) {
74         if (LOG.isTraceEnabled()) {
75             LOG.trace("Adding Router : key: " + identifier + ", value=" + input);
76         }
77         // Create internal VPN
78         nvpnManager.createL3Vpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
79     }
80
81     @Override
82     protected void remove(InstanceIdentifier<Router> identifier, Router input) {
83         if (LOG.isTraceEnabled()) {
84             LOG.trace("Removing router : key: " + identifier + ", value=" + input);
85         }
86         // check if this router has internal-VPN
87         Uuid routerId = input.getUuid();
88         VpnMap vpnmap = NeutronvpnUtils.getVpnMap(broker, routerId);
89         if (vpnmap != null) {
90             // if yes, remove corresponding internal vpn
91             LOG.trace("removing internal-vpn for router {}", routerId);
92             nvpnManager.removeL3Vpn(routerId);
93         } else {
94             // if not, it is associated with some VPN
95             // remove VPN-router association
96             Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId);
97             LOG.trace("dissociating router {} from vpn {}", routerId, vpnId);
98             nvpnManager.dissociateRouterFromVpn(vpnId, routerId);
99         }
100
101     }
102
103     @Override
104     protected void update(InstanceIdentifier<Router> identifier, Router original, Router update) {
105         if (LOG.isTraceEnabled()) {
106             LOG.trace("Updating Router : key: " + identifier + ", original value=" + original + ", update value=" +
107                     update);
108         }
109         Uuid routerId = update.getUuid();
110         Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId);
111         List<Interfaces> oldInterfaces = (original.getInterfaces() != null) ? original.getInterfaces() : new
112                 ArrayList<Interfaces>();
113         List<Interfaces> newInterfaces = (update.getInterfaces() != null) ? update.getInterfaces() : new
114                 ArrayList<Interfaces>();
115         List<String> oldRoutes = (original.getRoutes() != null) ? original.getRoutes() : new ArrayList<String>();
116         List<String> newRoutes = (update.getRoutes() != null) ? update.getRoutes() : new ArrayList<String>();
117
118         if (!oldInterfaces.equals(newInterfaces)) {
119             for (Interfaces intrf : newInterfaces) {
120                 if (!oldInterfaces.remove(intrf)) {
121                     // add new subnet
122                     nvpnManager.addSubnetToVpn(vpnId, intrf.getSubnetId());
123                 }
124             }
125             //clear remaining old subnets
126             for (Interfaces intrf : oldInterfaces) {
127                 nvpnManager.removeSubnetFromVpn(vpnId, intrf.getSubnetId());
128             }
129         }
130         if (!oldRoutes.equals(newRoutes)) {
131             Iterator<String> iterator = newRoutes.iterator();
132             while (iterator.hasNext()) {
133                 String route = iterator.next();
134                 if (oldRoutes.remove(route)) {
135                     iterator.remove();
136                 }
137             }
138             nvpnManager.addAdjacencyforExtraRoute(newRoutes, true, null);
139             if (!oldRoutes.isEmpty()) {
140                 nvpnManager.removeAdjacencyforExtraRoute(oldRoutes);
141             }
142         }
143     }
144 }