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
8 package org.opendaylight.netvirt.natservice.ha;
10 import java.math.BigInteger;
11 import java.util.concurrent.Executors;
13 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.binding.api.DataTreeIdentifier;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.mdsalutil.MDSALUtil;
20 import org.opendaylight.genius.tools.mdsal.listener.AbstractClusteredAsyncDataTreeChangeListener;
21 import org.opendaylight.netvirt.natservice.api.CentralizedSwitchScheduler;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * CentralizedSwitchChangeListener adds/removes the switches to scheduler pool when a switch is
34 public class SnatNodeEventListener extends AbstractClusteredAsyncDataTreeChangeListener<Node> {
35 private static final Logger LOG = LoggerFactory.getLogger(SnatNodeEventListener.class);
36 private final CentralizedSwitchScheduler centralizedSwitchScheduler;
39 public SnatNodeEventListener(final DataBroker dataBroker,
40 final CentralizedSwitchScheduler centralizedSwitchScheduler) {
42 super(dataBroker,new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier
43 .create(Nodes.class).child(Node.class)),
44 Executors.newSingleThreadExecutor());
45 this.centralizedSwitchScheduler = centralizedSwitchScheduler;
49 public void remove(Node dataObjectModification) {
50 NodeKey nodeKey = dataObjectModification.key();
51 BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
52 LOG.info("Dpn removed {}", dpnId);
53 centralizedSwitchScheduler.removeSwitch(dpnId);
57 public void update(Node dataObjectModificationBefore,
58 Node dataObjectModificationAfter) {
63 public void add(Node dataObjectModification) {
64 NodeKey nodeKey = dataObjectModification.key();
65 BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
66 LOG.info("Dpn added {}", dpnId);
67 centralizedSwitchScheduler.addSwitch(dpnId);