2 * Copyright (c) 2017 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 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;
29 * CentralizedSwitchChangeListener detect changes in switch:router mapping and
30 * update flows accordingly.
33 public class SnatCentralizedSwitchChangeListener
34 extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
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;
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;
53 LOG.info("{} init", getClass().getSimpleName());
54 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
58 protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
59 return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
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);
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);
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);
91 protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {