Fix - creating parent node at VPP
[honeycomb.git] / vbd / impl / src / main / java / io / fd / honeycomb / vbd / impl / VirtualBridgeDomainManager.java
1 /*
2  * Copyright (c) 2016 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 io.fd.honeycomb.vbd.impl;
10
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vbridge.topology.rev160129.TopologyTypesVbridgeAugment;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vbridge.topology.rev160129.network.topology.topology.topology.types.VbridgeTopology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Tip for the Virtual Bridge Domain implementation. This class is instantiated when the application is started
29  * and {@link #close()}d when it is shut down.
30  */
31 public final class VirtualBridgeDomainManager implements AutoCloseable {
32     private static final Logger LOG = LoggerFactory.getLogger(VirtualBridgeDomainManager.class);
33     private static final DataTreeIdentifier<VbridgeTopology> LISTEN_TREE =
34             new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
35                     InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class).child(TopologyTypes.class)
36                     .augmentation(TopologyTypesVbridgeAugment.class).child(VbridgeTopology.class).build());
37
38     private final ListenerRegistration<TopologyMonitor> reg;
39     private boolean closed;
40
41     private VirtualBridgeDomainManager(final ListenerRegistration<TopologyMonitor> reg) {
42         this.reg = Preconditions.checkNotNull(reg);
43     }
44
45     public static VirtualBridgeDomainManager create(@Nonnull final DataBroker dataBroker,@Nonnull MountPointService mountService) {
46         final ListenerRegistration<TopologyMonitor> reg =
47                 dataBroker.registerDataTreeChangeListener(LISTEN_TREE, new TopologyMonitor(dataBroker, mountService));
48
49         return new VirtualBridgeDomainManager(reg);
50     }
51
52     @Override
53     public void close() {
54         if (!closed) {
55             LOG.debug("Virtual Bridge Domain manager shut down started");
56
57             final TopologyMonitor monitor = reg.getInstance();
58             reg.close();
59             LOG.debug("Topology monitor {} unregistered", monitor);
60             monitor.close();
61
62             closed = true;
63             LOG.debug("Virtual Bridge Domain manager shut down completed");
64         }
65     }
66 }