Enable SNAT flows on router update
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / ha / SnatCentralizedSwitchChangeListener.java
1 /*
2  * Copyright (c) 2017 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 javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.netvirt.natservice.api.SnatServiceManager;
20 import org.opendaylight.netvirt.natservice.internal.NatUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * CentralizedSwitchChangeListener detect changes in switch:router mapping and
30  * update flows accordingly.
31  */
32 @Singleton
33 public class SnatCentralizedSwitchChangeListener
34         extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(SnatCentralizedSwitchChangeListener.class);
37     private final DataBroker dataBroker;
38     private final SnatServiceManager snatServiceManger;
39     private final NatDataUtil natDataUtil;
40
41     @Inject
42     public SnatCentralizedSwitchChangeListener(final DataBroker dataBroker,
43             final SnatServiceManager snatServiceManger, NatDataUtil natDataUtil) {
44         super(RouterToNaptSwitch.class, SnatCentralizedSwitchChangeListener.class);
45         this.dataBroker = dataBroker;
46         this.snatServiceManger = snatServiceManger;
47         this.natDataUtil = natDataUtil;
48     }
49
50     @Override
51     @PostConstruct
52     public void init() {
53         LOG.info("{} init", getClass().getSimpleName());
54         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
55     }
56
57     @Override
58     protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
59         return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
60     }
61
62     @Override
63     protected void remove(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
64         LOG.debug("Deleting {}", routerToNaptSwitch);
65         BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
66         Routers router =   natDataUtil.getRouter(routerToNaptSwitch.getRouterName());
67         snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
68         natDataUtil.removeFromRouterMap(router);
69     }
70
71     @Override
72     protected void update(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch origRouterToNaptSwitch,
73             RouterToNaptSwitch updatedRouterToNaptSwitch) {
74         LOG.debug("Updating old {} new {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
75         BigInteger origPrimarySwitchId = origRouterToNaptSwitch.getPrimarySwitchId();
76         Routers origRouter = NatUtil.getRoutersFromConfigDS(dataBroker, origRouterToNaptSwitch.getRouterName());
77         snatServiceManger.notify(origRouter, origPrimarySwitchId, null,
78                 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
79         natDataUtil.removeFromRouterMap(origRouter);
80         BigInteger updatedPrimarySwitchId = updatedRouterToNaptSwitch.getPrimarySwitchId();
81         Routers updatedRouter = NatUtil.getRoutersFromConfigDS(dataBroker, updatedRouterToNaptSwitch.getRouterName());
82         natDataUtil.updateRouterMap(updatedRouter);
83         snatServiceManger.notify(updatedRouter, updatedPrimarySwitchId, null,
84                 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
85     }
86
87     @Override
88     protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
89         LOG.debug("Adding {}", routerToNaptSwitch);
90         BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
91         Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerToNaptSwitch.getRouterName());
92         natDataUtil.addtoRouterMap(router);
93         snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
94     }
95
96     @Override
97     protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {
98         return this;
99     }
100 }