2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.neutronvpn;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
32 public class NeutronRouterChangeListener extends AbstractDataChangeListener<Router> implements AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
35 private ListenerRegistration<DataChangeListener> listenerRegistration;
36 private final DataBroker broker;
37 private NeutronvpnManager nvpnManager;
40 public NeutronRouterChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
43 nvpnManager = nVpnMgr;
48 public void close() throws Exception {
49 if (listenerRegistration != null) {
51 listenerRegistration.close();
52 } catch (final Exception e) {
53 LOG.error("Error when cleaning up DataChangeListener.", e);
55 listenerRegistration = null;
57 LOG.info("N_Router listener Closed");
61 private void registerListener(final DataBroker db) {
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);
73 protected void add(InstanceIdentifier<Router> identifier, Router input) {
74 if (LOG.isTraceEnabled()) {
75 LOG.trace("Adding Router : key: " + identifier + ", value=" + input);
77 // Create internal VPN
78 nvpnManager.createL3Vpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
82 protected void remove(InstanceIdentifier<Router> identifier, Router input) {
83 if (LOG.isTraceEnabled()) {
84 LOG.trace("Removing router : key: " + identifier + ", value=" + input);
86 Uuid routerId = input.getUuid();
87 // fetch subnets associated to router
88 List<Interfaces> routerInterfaces = input.getInterfaces();
89 List<Uuid> routerSubnetIds = new ArrayList<Uuid>();
90 if (routerInterfaces != null) {
91 for (Interfaces rtrIf : routerInterfaces) {
92 routerSubnetIds.add(rtrIf.getSubnetId());
95 nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
99 protected void update(InstanceIdentifier<Router> identifier, Router original, Router update) {
100 if (LOG.isTraceEnabled()) {
101 LOG.trace("Updating Router : key: " + identifier + ", original value=" + original + ", update value=" +
104 Uuid routerId = update.getUuid();
105 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
106 // internal vpn always present in case external vpn not found
110 List<Interfaces> oldInterfaces = (original.getInterfaces() != null) ? original.getInterfaces() : new
111 ArrayList<Interfaces>();
112 List<Interfaces> newInterfaces = (update.getInterfaces() != null) ? update.getInterfaces() : new
113 ArrayList<Interfaces>();
114 List<Routes> oldRoutes = (original.getRoutes() != null) ? original.getRoutes() : new ArrayList<Routes>();
115 List<Routes> newRoutes = (update.getRoutes() != null) ? update.getRoutes() : new ArrayList<Routes>();
116 if (!oldInterfaces.equals(newInterfaces)) {
117 for (Interfaces intrf : newInterfaces) {
118 if (!oldInterfaces.remove(intrf)) {
120 nvpnManager.addSubnetToVpn(vpnId, intrf.getSubnetId());
123 //clear remaining old subnets
124 for (Interfaces intrf : oldInterfaces) {
125 nvpnManager.removeSubnetFromVpn(vpnId, intrf.getSubnetId());
128 if (!oldRoutes.equals(newRoutes)) {
129 Iterator<Routes> iterator = newRoutes.iterator();
130 while (iterator.hasNext()) {
131 Routes route = iterator.next();
132 if (oldRoutes.remove(route)) {
136 nvpnManager.addAdjacencyforExtraRoute(newRoutes, true, null);
137 if (!oldRoutes.isEmpty()) {
138 nvpnManager.removeAdjacencyforExtraRoute(oldRoutes);