BGPCEP-710: Create Network Topology Loader
[bgpcep.git] / data-change-counter / src / main / java / org / opendaylight / protocol / data / change / counter / TopologyDataChangeCounterDeployer.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.protocol.data.change.counter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Map;
15 import javax.annotation.Nonnull;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgpcep.data.change.counter.config.rev170424.DataChangeCounterConfig;
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 TopologyDataChangeCounterDeployer implements DataTreeChangeListener<DataChangeCounterConfig>,
30     AutoCloseable {
31     private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounterDeployer.class);
32     private static final InstanceIdentifier<DataChangeCounterConfig> DATA_CHANGE_COUNTER_IID =
33         InstanceIdentifier.builder(DataChangeCounterConfig.class).build();
34     private final DataBroker dataBroker;
35     @GuardedBy("this")
36     private final Map<String, TopologyDataChangeCounter> counters = new HashMap<>();
37     private ListenerRegistration<TopologyDataChangeCounterDeployer> registration;
38
39     public TopologyDataChangeCounterDeployer(final DataBroker dataBroker) {
40         this.dataBroker = requireNonNull(dataBroker);
41     }
42
43     public synchronized void register() {
44         this.registration = this.dataBroker.registerDataTreeChangeListener(
45             new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, DATA_CHANGE_COUNTER_IID), this);
46         LOG.info("Data change counter Deployer initiated");
47     }
48
49
50     @Override
51     public synchronized void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<DataChangeCounterConfig>> changes) {
52         for (final DataTreeModification<DataChangeCounterConfig> dataTreeModification : changes) {
53             final DataObjectModification<DataChangeCounterConfig> rootNode = dataTreeModification.getRootNode();
54             switch (dataTreeModification.getRootNode().getModificationType()) {
55                 case DELETE:
56                     deleteCounterChange(rootNode.getDataBefore().getCounterId());
57                     break;
58                 case SUBTREE_MODIFIED:
59                 case WRITE:
60                     final DataChangeCounterConfig change = rootNode.getDataAfter();
61                     chandleCounterChange(change.getCounterId(), change.getTopologyName());
62                     break;
63             }
64         }
65     }
66
67     public synchronized void deleteCounterChange(final String counterId) {
68         final TopologyDataChangeCounter oldCounter = this.counters.remove(counterId);
69         if (oldCounter != null) {
70             LOG.info("Data change counter Deployer deleted: {}", counterId);
71             oldCounter.close();
72         }
73     }
74
75     public synchronized void chandleCounterChange(final String counterId, final String topologyName) {
76         deleteCounterChange(counterId);
77         LOG.info("Data change counter Deployer created: {} / {}", counterId, topologyName);
78
79         final TopologyDataChangeCounter counter = new TopologyDataChangeCounter(this.dataBroker, counterId, topologyName);
80         this.counters.put(counterId, counter);
81     }
82
83     @Override
84     public synchronized void close() {
85         LOG.info("Closing Data change counter Deployer");
86
87         if (this.registration != null) {
88             this.registration.close();
89             this.registration = null;
90         }
91     }
92 }