7afe7bbcddcd40307419c268af1f0c3318f40c17
[netvirt.git] / vpnservice / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.neutronvpn;
9
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
20 import org.opendaylight.genius.mdsalutil.NwConstants;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.link.states.InterVpnLinkState;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class NeutronRouterChangeListener extends AbstractDataChangeListener<Router> implements AutoCloseable {
34     private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
35     private ListenerRegistration<DataChangeListener> listenerRegistration;
36     private final DataBroker dataBroker;
37     private final NeutronvpnManager nvpnManager;
38     private final NeutronvpnNatManager nvpnNatManager;
39     private final NeutronSubnetGwMacResolver gwMacResolver;
40
41     public NeutronRouterChangeListener(final DataBroker dataBroker, final NeutronvpnManager nVpnMgr,
42                                        final NeutronvpnNatManager nVpnNatMgr, NeutronSubnetGwMacResolver gwMacResolver) {
43         super(Router.class);
44         this.dataBroker = dataBroker;
45         nvpnManager = nVpnMgr;
46         nvpnNatManager = nVpnNatMgr;
47         this.gwMacResolver = gwMacResolver;
48     }
49
50     public void start() {
51         LOG.info("{} start", getClass().getSimpleName());
52         listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
53                 getWildCardPath(), this, DataChangeScope.SUBTREE);
54     }
55
56     private InstanceIdentifier<Router> getWildCardPath() {
57         return InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class);
58     }
59
60     @Override
61     public void close() throws Exception {
62         if (listenerRegistration != null) {
63             listenerRegistration.close();
64             listenerRegistration = null;
65         }
66         LOG.info("{} close", getClass().getSimpleName());
67     }
68
69     @Override
70     protected void add(InstanceIdentifier<Router> identifier, Router input) {
71         if (LOG.isTraceEnabled()) {
72             LOG.trace("Adding Router : key: " + identifier + ", value=" + input);
73         }
74         // Create internal VPN
75         nvpnManager.createL3InternalVpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
76         NeutronvpnUtils.addToRouterCache(input);
77         gwMacResolver.sendArpRequestsToExtGateways(input);
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         //NOTE: Pass an empty routerSubnetIds list, as router interfaces
87         //will be removed from VPN by invocations from NeutronPortChangeListener
88         List<Uuid> routerSubnetIds = new ArrayList<>();
89         nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
90         NeutronvpnUtils.removeFromRouterCache(input);
91
92         // Handle router deletion for the NAT service
93         if (input.getExternalGatewayInfo() != null) {
94             Uuid extNetId = input.getExternalGatewayInfo().getExternalNetworkId();
95             nvpnNatManager.removeExternalNetworkFromRouter(extNetId, input);
96         }
97
98     }
99
100     @Override
101     protected void update(InstanceIdentifier<Router> identifier, Router original, Router update) {
102         LOG.trace("Updating Router : key: {}, original value={}, update value={}", identifier, original, update);
103         Uuid routerId = update.getUuid();
104         Uuid vpnId = NeutronvpnUtils.getVpnForRouter(dataBroker, routerId, true);
105         // internal vpn always present in case external vpn not found
106         if (vpnId == null) {
107             vpnId = routerId;
108         }
109         List<Routes> oldRoutes = (original.getRoutes() != null) ? original.getRoutes() : new ArrayList<Routes>();
110         List<Routes> newRoutes = (update.getRoutes() != null) ? update.getRoutes() : new ArrayList<Routes>();
111         if (!oldRoutes.equals(newRoutes)) {
112             Iterator<Routes> iterator = newRoutes.iterator();
113             while (iterator.hasNext()) {
114                 Routes route = iterator.next();
115                 if (oldRoutes.remove(route)) {
116                     iterator.remove();
117                 }
118             }
119
120             handleChangedRoutes(vpnId, newRoutes, NwConstants.ADD_FLOW);
121
122             if (!oldRoutes.isEmpty()) {
123                 handleChangedRoutes(vpnId, oldRoutes, NwConstants.DEL_FLOW);
124             }
125         }
126
127         nvpnNatManager.handleExternalNetworkForRouter(original, update);
128         gwMacResolver.sendArpRequestsToExtGateways(update);
129     }
130
131     private void handleChangedRoutes(Uuid vpnName, List<Routes> routes, int addedOrRemoved) {
132         // Some routes may point to an InterVpnLink's endpoint, lets treat them differently
133         List<Routes> interVpnLinkRoutes = new ArrayList<Routes>();
134         List<Routes> otherRoutes = new ArrayList<Routes>();
135         HashMap<String, InterVpnLink> nexthopsXinterVpnLinks = new HashMap<String, InterVpnLink>();
136         for ( Routes route : routes ) {
137             String nextHop = String.valueOf(route.getNexthop().getValue());
138             // Nexthop is another VPN?
139             Optional<InterVpnLink> interVpnLink = NeutronvpnUtils.getInterVpnLinkByEndpointIp(dataBroker, nextHop);
140             if ( interVpnLink.isPresent() ) {
141                 Optional<InterVpnLinkState> interVpnLinkState =
142                         NeutronvpnUtils.getInterVpnLinkState(dataBroker, interVpnLink.get().getName());
143                 if ( interVpnLinkState.isPresent() && interVpnLinkState.get().getState() == InterVpnLinkState.State.Active) {
144                     interVpnLinkRoutes.add(route);
145                     nexthopsXinterVpnLinks.put(nextHop, interVpnLink.get());
146                 } else {
147                     LOG.warn("Failed installing route to {}. Reason: InterVPNLink {} is not Active",
148                             String.valueOf(route.getDestination().getValue()), interVpnLink.get().getName());
149                 }
150             } else {
151                 otherRoutes.add(route);
152             }
153         }
154
155         if ( addedOrRemoved == NwConstants.ADD_FLOW ) {
156             nvpnManager.addInterVpnRoutes(vpnName, interVpnLinkRoutes, nexthopsXinterVpnLinks);
157             nvpnManager.addAdjacencyforExtraRoute(vpnName, otherRoutes);
158         } else {
159             nvpnManager.removeAdjacencyforExtraRoute(vpnName, otherRoutes);
160             nvpnManager.removeInterVpnRoutes(vpnName, interVpnLinkRoutes, nexthopsXinterVpnLinks);
161         }
162     }
163 }