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.util.concurrent.Executors;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.genius.mdsalutil.MDSALUtil;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.netvirt.natservice.api.NatSwitchCache;
19 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.common.Uint64;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * CentralizedSwitchChangeListener adds/removes the switches to scheduler pool when a switch is
33 public class SnatNodeEventListener extends AbstractClusteredAsyncDataTreeChangeListener<Node> {
34 private static final Logger LOG = LoggerFactory.getLogger(SnatNodeEventListener.class);
35 private final NatSwitchCache centralizedSwitchCache;
38 public SnatNodeEventListener(final DataBroker dataBroker,
39 final NatSwitchCache centralizedSwitchCache) {
41 super(dataBroker, DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier
42 .create(Nodes.class).child(Node.class)),
43 Executors.newSingleThreadExecutor());
44 this.centralizedSwitchCache = centralizedSwitchCache;
51 org.opendaylight.infrautils.utils.concurrent.Executors.shutdownAndAwaitTermination(getExecutorService());
55 public void remove(Node dataObjectModification) {
56 NodeKey nodeKey = dataObjectModification.key();
57 Uint64 dpnId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
58 LOG.info("Dpn removed {}", dpnId);
59 centralizedSwitchCache.removeSwitch(dpnId);
63 public void update(Node dataObjectModificationBefore,
64 Node dataObjectModificationAfter) {
69 public void add(Node dataObjectModification) {
70 NodeKey nodeKey = dataObjectModification.key();
71 Uint64 dpnId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
72 LOG.info("Dpn added {}", dpnId);
73 centralizedSwitchCache.addSwitch(dpnId);