Make methods static
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / reconciliation / configuration / HwvtepReconciliationManager.java
1 /*
2  * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.ovsdb.hwvtepsouthbound.reconciliation.configuration;
9
10 import java.util.Collection;
11 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.DataObjectModification;
14 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
15 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.mdsal.binding.api.DataTreeModification;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionManager;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class HwvtepReconciliationManager implements ClusteredDataTreeChangeListener<Node>, AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(HwvtepReconciliationManager.class);
32     private final ListenerRegistration<HwvtepReconciliationManager> registration;
33     private final HwvtepConnectionManager hcm;
34
35     public HwvtepReconciliationManager(DataBroker db, HwvtepConnectionManager hcm) {
36         this.hcm = hcm;
37
38         final InstanceIdentifier<Node> iid = HwvtepSouthboundMapper.createInstanceIdentifier();
39         final DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, iid);
40         LOG.trace("Registering listener for path {}", treeId);
41         registration = db.registerDataTreeChangeListener(treeId, HwvtepReconciliationManager.this);
42     }
43
44     @Override
45     public void close() throws Exception {
46         if (registration != null) {
47             registration.close();
48         }
49     }
50
51     @Override
52     public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
53         processConnectedNodes(changes);
54         processDisconnectedNodes(changes);
55     }
56
57     private void processDisconnectedNodes(Collection<DataTreeModification<Node>> changes) {
58         for (DataTreeModification<Node> change : changes) {
59             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
60             final DataObjectModification<Node> mod = change.getRootNode();
61             Node deleted = getRemoved(mod);
62             if (deleted != null) {
63                 if (deleted.augmentation(HwvtepGlobalAugmentation.class) != null) {
64                     LOG.trace("Cancel config reconciliation for node {}", deleted.key());
65                     hcm.stopConfigurationReconciliation(key);
66                 }
67             }
68         }
69     }
70
71     private void processConnectedNodes(Collection<DataTreeModification<Node>> changes) {
72         for (DataTreeModification<Node> change : changes) {
73             DataObjectModification<Node> mod = change.getRootNode();
74             Node node = getCreated(mod);
75             if (node != null) {
76                 PhysicalSwitchAugmentation physicalSwitch = node.augmentation(PhysicalSwitchAugmentation.class);
77                 if (physicalSwitch != null) {
78                     HwvtepConnectionInstance connection =
79                             hcm.getConnectionInstance(physicalSwitch);
80                     if (connection != null) {
81                         LOG.info("HwvtepReconciliationManager Reconcile config for node {}, IP : {}", node.key(),
82                                 connection.getConnectionInfo().getRemoteAddress());
83                         hcm.reconcileConfigurations(connection, node);
84                     }
85                 }
86             }
87         }
88     }
89
90     private static Node getCreated(DataObjectModification<Node> mod) {
91         if (mod.getModificationType() == ModificationType.WRITE && mod.getDataBefore() == null) {
92             return mod.getDataAfter();
93         }
94         return null;
95     }
96
97     private static Node getRemoved(DataObjectModification<Node> mod) {
98         if (mod.getModificationType() == ModificationType.DELETE) {
99             return mod.getDataBefore();
100         }
101         return null;
102     }
103 }