Fix for invalid vpnid in default snat flow
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / ha / SnatCentralizedSwitchChangeListener.java
1 /*
2  * Copyright (c) 2017, 2018 Red Hat, Inc. 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
9 package org.opendaylight.netvirt.natservice.ha;
10
11 import java.math.BigInteger;
12
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;
30
31 /**
32  * CentralizedSwitchChangeListener detect changes in switch:router mapping and
33  * update flows accordingly.
34  */
35 @Singleton
36 public class SnatCentralizedSwitchChangeListener
37         extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
38
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;
44
45     @Inject
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;
54     }
55
56     @Override
57     @PostConstruct
58     public void init() {
59         LOG.info("{} init", getClass().getSimpleName());
60         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
61     }
62
63     @Override
64     protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
65         return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
66     }
67
68     @Override
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());
73         if (router != null) {
74             snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
75             natDataUtil.removeFromRouterMap(router);
76         }
77     }
78
79     @Override
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);
89         }
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);
96         }
97     }
98
99     @Override
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);
114                 });
115             return;
116         }
117         handleAdd(routerName, router, primarySwitchId);
118     }
119
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);
124         } else {
125             LOG.error("Router {} not found for primarySwitch {}", routerName, primarySwitchId);
126         }
127     }
128
129     @Override
130     protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {
131         return this;
132     }
133 }