2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.bgpcep.bgp.topology.provider.config;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.function.Function;
15 import org.opendaylight.bgpcep.bgp.topology.provider.AbstractTopologyBuilder;
16 import org.opendaylight.bgpcep.bgp.topology.provider.spi.BgpTopologyDeployer;
17 import org.opendaylight.bgpcep.bgp.topology.provider.spi.TopologyReferenceSingletonService;
18 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yangtools.concepts.AbstractRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 final class TopologyReferenceSingletonServiceImpl implements TopologyReferenceSingletonService {
26 private static final Logger LOG = LoggerFactory.getLogger(TopologyReferenceSingletonServiceImpl.class);
27 private final AbstractTopologyBuilder<?> topologyBuilder;
28 private final AbstractRegistration serviceRegistration;
29 private final Topology configuration;
30 private final Function<Topology, Void> writeFunction;
32 TopologyReferenceSingletonServiceImpl(final AbstractTopologyBuilder<?> topologyBuilder, final BgpTopologyDeployer deployer,
33 final Topology configuration, final Function<Topology, Void> writeFunction) {
34 this.writeFunction = writeFunction;
35 this.configuration = Preconditions.checkNotNull(configuration);
36 this.topologyBuilder = Preconditions.checkNotNull(topologyBuilder);
37 this.serviceRegistration = deployer.registerService(this);
41 public InstanceIdentifier<Topology> getInstanceIdentifier() {
42 return this.topologyBuilder.getInstanceIdentifier();
47 this.serviceRegistration.close();
51 public void instantiateServiceInstance() {
52 LOG.info("Topology Singleton Service {} instantiated", getIdentifier());
53 this.topologyBuilder.start();
54 if (this.writeFunction != null) {
55 this.writeFunction.apply(this.configuration);
60 public ListenableFuture<Void> closeServiceInstance() {
61 LOG.info("Close Topology Singleton Service {}", getIdentifier());
62 this.topologyBuilder.close();
63 return Futures.immediateFuture(null);
67 public ServiceGroupIdentifier getIdentifier() {
68 return ServiceGroupIdentifier.create(getInstanceIdentifier().firstKeyOf(Topology.class).getTopologyId().getValue());
72 public Topology getConfiguration() {
73 return this.configuration;