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.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;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
31 public class NeutronRouterChangeListener extends AbstractDataChangeListener<Router> implements AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
34 private ListenerRegistration<DataChangeListener> listenerRegistration;
35 private final DataBroker broker;
36 private NeutronvpnManager nvpnManager;
39 public NeutronRouterChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
42 nvpnManager = nVpnMgr;
47 public void close() throws Exception {
48 if (listenerRegistration != null) {
50 listenerRegistration.close();
51 } catch (final Exception e) {
52 LOG.error("Error when cleaning up DataChangeListener.", e);
54 listenerRegistration = null;
56 LOG.info("N_Router listener Closed");
60 private void registerListener(final DataBroker db) {
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);
72 protected void add(InstanceIdentifier<Router> identifier, Router input) {
73 if (LOG.isTraceEnabled()) {
74 LOG.trace("Adding Router : key: " + identifier + ", value=" + input);
76 // Create internal VPN
77 nvpnManager.createL3Vpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
81 protected void remove(InstanceIdentifier<Router> identifier, Router input) {
82 if (LOG.isTraceEnabled()) {
83 LOG.trace("Removing router : key: " + identifier + ", value=" + input);
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());
94 nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
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=" +
103 Uuid routerId = update.getUuid();
104 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
105 // internal vpn always present in case external vpn not found
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>();
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<String> iterator = newRoutes.iterator();
130 while (iterator.hasNext()) {
131 String route = iterator.next();
132 if (oldRoutes.remove(route)) {
136 nvpnManager.addAdjacencyforExtraRoute(newRoutes, true, null);
137 if (!oldRoutes.isEmpty()) {
138 nvpnManager.removeAdjacencyforExtraRoute(oldRoutes);