Remove redundant names in paths
[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 primarySwitchId = origRouterToNaptSwitch.getPrimarySwitchId();
76         Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, origRouterToNaptSwitch.getRouterName());
77         natDataUtil.updateRouterMap(router);
78         snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
79     }
80
81     @Override
82     protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
83         LOG.debug("Adding {}", routerToNaptSwitch);
84         BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
85         Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerToNaptSwitch.getRouterName());
86         natDataUtil.addtoRouterMap(router);
87         snatServiceManger.notify(router, primarySwitchId, null, SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
88     }
89
90     @Override
91     protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {
92         return this;
93     }
94 }