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