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