2 * Copyright (c) 2017, 2018 Red Hat, Inc. 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
9 package org.opendaylight.netvirt.natservice.ha;
11 import java.math.BigInteger;
13 import java.time.Duration;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
21 import org.opendaylight.netvirt.natservice.api.SnatServiceManager;
22 import org.opendaylight.netvirt.natservice.internal.NatConstants;
23 import org.opendaylight.netvirt.natservice.internal.NatUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * CentralizedSwitchChangeListener detect changes in switch:router mapping and
33 * update flows accordingly.
36 public class SnatCentralizedSwitchChangeListener
37 extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
39 private static final Logger LOG = LoggerFactory.getLogger(SnatCentralizedSwitchChangeListener.class);
40 private final DataBroker dataBroker;
41 private final SnatServiceManager snatServiceManger;
42 private final NatDataUtil natDataUtil;
43 private final DataTreeEventCallbackRegistrar eventCallbacks;
46 public SnatCentralizedSwitchChangeListener(final DataBroker dataBroker,
47 final SnatServiceManager snatServiceManger, NatDataUtil natDataUtil,
48 final DataTreeEventCallbackRegistrar dataTreeEventCallbackRegistrar) {
49 super(RouterToNaptSwitch.class, SnatCentralizedSwitchChangeListener.class);
50 this.dataBroker = dataBroker;
51 this.snatServiceManger = snatServiceManger;
52 this.natDataUtil = natDataUtil;
53 this.eventCallbacks = dataTreeEventCallbackRegistrar;
59 LOG.info("{} init", getClass().getSimpleName());
60 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
64 protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
65 return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
69 protected void remove(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
70 LOG.debug("Deleting {}", routerToNaptSwitch);
71 BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
72 Routers router = natDataUtil.getRouter(routerToNaptSwitch.getRouterName());
74 snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
75 natDataUtil.removeFromRouterMap(router);
80 protected void update(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch origRouterToNaptSwitch,
81 RouterToNaptSwitch updatedRouterToNaptSwitch) {
82 LOG.debug("Updating old {} new {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
83 BigInteger origPrimarySwitchId = origRouterToNaptSwitch.getPrimarySwitchId();
84 Routers origRouter = NatUtil.getRoutersFromConfigDS(dataBroker, origRouterToNaptSwitch.getRouterName());
85 if (origRouter != null) {
86 snatServiceManger.notify(origRouter, origPrimarySwitchId, null,
87 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
88 natDataUtil.removeFromRouterMap(origRouter);
90 BigInteger updatedPrimarySwitchId = updatedRouterToNaptSwitch.getPrimarySwitchId();
91 Routers updatedRouter = NatUtil.getRoutersFromConfigDS(dataBroker, updatedRouterToNaptSwitch.getRouterName());
92 if (updatedRouter != null) {
93 natDataUtil.updateRouterMap(updatedRouter);
94 snatServiceManger.notify(updatedRouter, updatedPrimarySwitchId, null,
95 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
100 protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
101 LOG.debug("Adding {}", routerToNaptSwitch);
102 BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
103 String routerName = routerToNaptSwitch.getRouterName();
104 Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
105 long vpnId = NatUtil.getVpnId(dataBroker, routerName);
106 if (vpnId == NatConstants.INVALID_ID) {
107 LOG.warn("VpnId not unavailable for router {} yet", routerName);
108 eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
109 NatUtil.getVpnInstanceToVpnIdIdentifier(routerName), (unused, newVpnId) -> {
110 handleAdd(routerName, router, primarySwitchId);
111 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
112 }, Duration.ofSeconds(5), iid -> {
113 LOG.error("VpnId not found for router {}", routerName);
117 handleAdd(routerName, router, primarySwitchId);
120 private void handleAdd(String routerName, Routers router, BigInteger primarySwitchId) {
121 if (router != null) {
122 natDataUtil.addtoRouterMap(router);
123 snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
125 LOG.error("Router {} not found for primarySwitch {}", routerName, primarySwitchId);
130 protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {