NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / ha / SnatNodeEventListener.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 package org.opendaylight.netvirt.natservice.ha;
9
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;
27
28 /**
29  * CentralizedSwitchChangeListener adds/removes the switches to scheduler pool when a switch is
30  * added/removed.
31  */
32 @Singleton
33 public class SnatNodeEventListener  extends AbstractClusteredAsyncDataTreeChangeListener<Node> {
34     private static final Logger LOG = LoggerFactory.getLogger(SnatNodeEventListener.class);
35     private final NatSwitchCache  centralizedSwitchCache;
36
37     @Inject
38     public SnatNodeEventListener(final DataBroker dataBroker,
39             final NatSwitchCache centralizedSwitchCache) {
40
41         super(dataBroker, DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier
42                 .create(Nodes.class).child(Node.class)),
43                 Executors.newSingleThreadExecutor());
44         this.centralizedSwitchCache = centralizedSwitchCache;
45     }
46
47     @Override
48     @PreDestroy
49     public void close() {
50         super.close();
51         org.opendaylight.infrautils.utils.concurrent.Executors.shutdownAndAwaitTermination(getExecutorService());
52     }
53
54     @Override
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);
60     }
61
62     @Override
63     public void update(Node dataObjectModificationBefore,
64             Node dataObjectModificationAfter) {
65         /*Do Nothing */
66     }
67
68     @Override
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);
74     }
75 }