MRI version bumpup for Aluminium
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronRouterChangeListener.java
1 /*
2  * Copyright © 2016, 2017 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 java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Optional;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.genius.mdsalutil.NwConstants;
20 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
21 import org.opendaylight.infrautils.utils.concurrent.Executors;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.link.states.InterVpnLinkState;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Singleton
38 public class NeutronRouterChangeListener extends AbstractAsyncDataTreeChangeListener<Router> {
39     private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
40     private final DataBroker dataBroker;
41     private final NeutronvpnManager nvpnManager;
42     private final NeutronvpnNatManager nvpnNatManager;
43     private final NeutronSubnetGwMacResolver gwMacResolver;
44     private final NeutronvpnUtils neutronvpnUtils;
45     private final JobCoordinator jobCoordinator;
46
47     @Inject
48     public NeutronRouterChangeListener(final DataBroker dataBroker, final NeutronvpnManager neutronvpnManager,
49                                        final NeutronvpnNatManager neutronvpnNatManager,
50                                        final NeutronSubnetGwMacResolver gwMacResolver,
51                                        final NeutronvpnUtils neutronvpnUtils,
52                                        final JobCoordinator jobCoordinator) {
53         super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
54                 .child(Routers.class).child(Router.class),
55                 Executors.newSingleThreadExecutor("NeutronRouterChangeListener", LOG));
56         this.dataBroker = dataBroker;
57         nvpnManager = neutronvpnManager;
58         nvpnNatManager = neutronvpnNatManager;
59         this.gwMacResolver = gwMacResolver;
60         this.neutronvpnUtils = neutronvpnUtils;
61         this.jobCoordinator = jobCoordinator;
62     }
63
64     public void init() {
65         LOG.info("{} init", getClass().getSimpleName());
66     }
67
68     @Override
69     @PreDestroy
70     public void close() {
71         super.close();
72         Executors.shutdownAndAwaitTermination(getExecutorService());
73     }
74
75     @Override
76     public void add(InstanceIdentifier<Router> identifier, Router input) {
77         LOG.trace("Adding Router : key: {}, value={}", identifier, input);
78         neutronvpnUtils.addToRouterCache(input);
79         // Create internal VPN
80         nvpnManager.createL3InternalVpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
81         jobCoordinator.enqueueJob(input.getUuid().toString(), () -> {
82             nvpnNatManager.handleExternalNetworkForRouter(null, input);
83             return Collections.emptyList();
84         });
85         gwMacResolver.sendArpRequestsToExtGateways(input);
86     }
87
88     @Override
89     public void remove(InstanceIdentifier<Router> identifier, Router input) {
90         LOG.trace("Removing router : key: {}, value={}", identifier, input);
91         Uuid routerId = input.getUuid();
92         // Handle router deletion for the NAT service
93         /*External Router and networks is handled before deleting the internal VPN, as there is dependency
94         on vpn operational data to release Lport tag in case of L3VPN over VxLAN*/
95         if (input.getExternalGatewayInfo() != null) {
96             Uuid extNetId = input.getExternalGatewayInfo().getExternalNetworkId();
97             List<ExternalFixedIps> externalFixedIps
98                     = new ArrayList<ExternalFixedIps>(input.getExternalGatewayInfo().getExternalFixedIps().values());
99             jobCoordinator.enqueueJob(input.getUuid().toString(), () -> {
100                 nvpnNatManager.removeExternalNetworkFromRouter(extNetId, input, externalFixedIps);
101                 return Collections.emptyList();
102             });
103         }
104         //NOTE: Pass an empty routerSubnetIds list, as router interfaces
105         //will be removed from VPN by invocations from NeutronPortChangeListener
106         List<Uuid> routerSubnetIds = new ArrayList<>();
107         nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
108
109         neutronvpnUtils.removeFromRouterCache(input);
110         nvpnNatManager.removeNeutronRouterDpns(input);
111     }
112
113     @Override
114     public void update(InstanceIdentifier<Router> identifier, Router original, Router update) {
115         LOG.trace("Updating Router : key: {}, original value={}, update value={}", identifier, original, update);
116         neutronvpnUtils.addToRouterCache(update);
117         Uuid routerId = update.getUuid();
118         neutronvpnUtils.addToRouterCache(update);
119         Uuid vpnId = neutronvpnUtils.getVpnForRouter(routerId, true);
120         // internal vpn always present in case external vpn not found
121         if (vpnId == null) {
122             vpnId = routerId;
123         }
124         List<Routes> oldRoutes = new ArrayList<>(original.nonnullRoutes().values());
125         List<Routes> newRoutes = new ArrayList<>(update.nonnullRoutes().values());
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
135             LOG.debug("Updating Router : AddRoutes {}, DeleteRoutes {}", newRoutes, oldRoutes);
136             if (!oldRoutes.isEmpty()) {
137                 handleChangedRoutes(vpnId, oldRoutes, NwConstants.DEL_FLOW);
138             }
139
140             //After initial extra-route configuration using cmd-"neutron router-update RouterA destination=IP-A,
141             // nexthop=prefix-A",if another update is done using command - "neutron router-update RouterA
142             // destination=IP-A,nexthop=prefix-B",neutron router listener calls update on prefix-A as well as prefix-B.
143             // On prefix-A , secondary adj (IP-A) is removed ,where as its added on prefix-B. This back-to-back update
144             // creates race-condition in Vrf Engine ,leading inconsistencies in l3nexthop, VpnExtraRoute,
145             // VpnInterfaceOp DS. Hence a temporary fix of 2sec delay is introduced in neutron.
146             // A better fix/design need to be thought to avoid race condition
147             try {
148                 Thread.sleep(2000); // sleep for 2sec
149             } catch (java.lang.InterruptedException e) {
150                 LOG.error("Exception while sleeping", e);
151             }
152
153             if (!newRoutes.isEmpty()) {
154                 handleChangedRoutes(vpnId, newRoutes, NwConstants.ADD_FLOW);
155             }
156         }
157
158         jobCoordinator.enqueueJob(update.getUuid().toString(), () -> {
159             nvpnNatManager.handleExternalNetworkForRouter(original, update);
160             return Collections.emptyList();
161         });
162         gwMacResolver.sendArpRequestsToExtGateways(update);
163     }
164
165     private void handleChangedRoutes(Uuid vpnName, List<Routes> routes, int addedOrRemoved) {
166         // Some routes may point to an InterVpnLink's endpoint, lets treat them differently
167         List<Routes> interVpnLinkRoutes = new ArrayList<>();
168         List<Routes> otherRoutes = new ArrayList<>();
169         HashMap<String, InterVpnLink> nexthopsXinterVpnLinks = new HashMap<>();
170         for (Routes route : routes) {
171             String nextHop = route.getNexthop().stringValue();
172             // Nexthop is another VPN?
173             Optional<InterVpnLink> interVpnLink = neutronvpnUtils.getInterVpnLinkByEndpointIp(nextHop);
174             if (interVpnLink.isPresent()) {
175                 Optional<InterVpnLinkState> interVpnLinkState =
176                         neutronvpnUtils.getInterVpnLinkState(interVpnLink.get().getName());
177                 if (interVpnLinkState.isPresent() && interVpnLinkState.get().getState()
178                         == InterVpnLinkState.State.Active) {
179                     interVpnLinkRoutes.add(route);
180                     nexthopsXinterVpnLinks.put(nextHop, interVpnLink.get());
181                 } else {
182                     LOG.error("Failed installing route to {}. Reason: InterVPNLink {} is not Active",
183                             route.getDestination().stringValue(), interVpnLink.get().getName());
184                 }
185             } else {
186                 otherRoutes.add(route);
187             }
188         }
189
190         if (addedOrRemoved == NwConstants.ADD_FLOW) {
191             nvpnManager.addInterVpnRoutes(vpnName, interVpnLinkRoutes, nexthopsXinterVpnLinks);
192             nvpnManager.updateVpnInterfaceWithExtraRouteAdjacency(vpnName, otherRoutes);
193         } else {
194             nvpnManager.removeAdjacencyforExtraRoute(vpnName, otherRoutes);
195             nvpnManager.removeInterVpnRoutes(vpnName, interVpnLinkRoutes, nexthopsXinterVpnLinks);
196         }
197     }
198 }