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