2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
8 package org.opendaylight.bgpcep.pcep.topology.provider.config;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Optional;
13 import java.net.InetSocketAddress;
14 import java.util.HashMap;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
21 import org.opendaylight.controller.config.yang.pcep.topology.provider.PCEPTopologyProviderRuntimeRegistrator;
22 import org.opendaylight.protocol.concepts.KeyMapping;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
24 import org.osgi.service.blueprint.container.BlueprintContainer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class PCEPTopologyDeployerImpl implements PCEPTopologyDeployer, AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyDeployerImpl.class);
30 private static final long TIMEOUT_NS = TimeUnit.SECONDS.toNanos(5);
33 private final Map<TopologyId, PCEPTopologyProviderBean> pcepTopologyServices = new HashMap<>();
34 private final BlueprintContainer container;
36 public PCEPTopologyDeployerImpl(final BlueprintContainer container) {
37 this.container = requireNonNull(container);
41 public synchronized void createTopologyProvider(final TopologyId topologyId,
42 final InetSocketAddress inetSocketAddress, final short rpcTimeout, final KeyMapping keys,
43 final InstructionScheduler schedulerDependency,
44 final Optional<PCEPTopologyProviderRuntimeRegistrator> runtime) {
45 if (this.pcepTopologyServices.containsKey(topologyId)) {
46 LOG.warn("Topology Provider {} already exist. New instance won't be created", topologyId);
49 final PCEPTopologyProviderBean pcepTopologyProviderBean = (PCEPTopologyProviderBean) this.container
50 .getComponentInstance(PCEPTopologyProviderBean.class.getSimpleName());
51 this.pcepTopologyServices.put(topologyId, pcepTopologyProviderBean);
52 final PCEPTopologyConfigDependencies configDependencies = new PCEPTopologyConfigDependencies(inetSocketAddress,
53 keys, schedulerDependency, topologyId, runtime, rpcTimeout);
54 pcepTopologyProviderBean.start(configDependencies);
58 public synchronized void removeTopologyProvider(final TopologyId topologyID) {
59 final PCEPTopologyProviderBean service = this.pcepTopologyServices.remove(topologyID);
60 if (service != null) {
62 service.closeServiceInstance().get(TIMEOUT_NS, TimeUnit.NANOSECONDS);
63 } catch (InterruptedException | ExecutionException | TimeoutException e) {
64 LOG.error("Failed to close Topology Provider {}.", topologyID.getValue(), e);
72 public synchronized void close() throws Exception {
73 this.pcepTopologyServices.values().forEach(PCEPTopologyProviderBean::closeServiceInstance);
74 this.pcepTopologyServices.values().forEach(PCEPTopologyProviderBean::close);