a936f019c5e65f4d638aeacf38d261fa15119c6c
[bgpcep.git] / data-change-counter / src / main / java / org / opendaylight / controller / config / yang / bgpcep / data / change / counter / DataChangeCounterImplModule.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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
9 package org.opendaylight.controller.config.yang.bgpcep.data.change.counter;
10
11 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.protocol.data.change.counter.TopologyDataChangeCounter;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21
22 public class DataChangeCounterImplModule extends org.opendaylight.controller.config.yang.bgpcep.data.change.counter.AbstractDataChangeCounterImplModule {
23
24     public DataChangeCounterImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
25             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
26         super(identifier, dependencyResolver);
27     }
28
29     public DataChangeCounterImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
30             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
31             final org.opendaylight.controller.config.yang.bgpcep.data.change.counter.DataChangeCounterImplModule oldModule,
32             final java.lang.AutoCloseable oldInstance) {
33         super(identifier, dependencyResolver, oldModule, oldInstance);
34     }
35
36     @Override
37     public void customValidation() {
38         JmxAttributeValidationException.checkNotNull(getCounterId(), "value is not set.", counterIdJmxAttribute);
39         JmxAttributeValidationException.checkNotNull(getTopologyName(), "value is not set.", topologyNameJmxAttribute);
40     }
41
42     @Override
43     public java.lang.AutoCloseable createInstance() {
44         final TopologyDataChangeCounter counter = new TopologyDataChangeCounter(getDataProviderDependency(), getCounterId());
45         final InstanceIdentifier<Topology> topoIId = InstanceIdentifier.builder(NetworkTopology.class)
46                 .child(Topology.class, new TopologyKey(new TopologyId(getTopologyName()))).build();
47         final ListenerRegistration<TopologyDataChangeCounter> registration = getDataProviderDependency().registerDataTreeChangeListener(
48                 new DataTreeIdentifier<Topology>(LogicalDatastoreType.OPERATIONAL, topoIId), counter);
49         return new DataChangeCounterCloseable(counter, registration);
50     }
51
52     private static final class DataChangeCounterCloseable implements AutoCloseable {
53
54         private final TopologyDataChangeCounter inner;
55         private final ListenerRegistration<TopologyDataChangeCounter> registration;
56
57         public DataChangeCounterCloseable(final TopologyDataChangeCounter inner,
58                 final ListenerRegistration<TopologyDataChangeCounter> registration) {
59             this.inner = inner;
60             this.registration = registration;
61         }
62
63         @Override
64         public void close() {
65             this.registration.close();
66             this.inner.close();
67         }
68     }
69
70 }