2 * Copyright (c) 2014 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
8 package org.opendaylight.openflowplugin.applications.topology.manager;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
17 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener.Component;
18 import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 class FlowCapableTopologyExporter {
30 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
32 private final InstanceIdentifier<Topology> iiToTopology;
33 private final OperationProcessor processor;
35 FlowCapableTopologyExporter(final OperationProcessor processor, final InstanceIdentifier<Topology> topology) {
36 this.processor = requireNonNull(processor);
37 iiToTopology = requireNonNull(topology);
40 CompositeListener toListener() {
41 return new CompositeListener(Set.of(
42 new Component(LinkDiscovered.class, (Listener<LinkDiscovered>) this::onLinkDiscovered),
43 new Component(LinkRemoved.class, (Listener<LinkRemoved>) this::onLinkRemoved)));
47 void onLinkDiscovered(final LinkDiscovered notification) {
48 processor.enqueueOperation(new OnLinkDiscovered(notification));
52 void onLinkRemoved(final LinkRemoved notification) {
53 processor.enqueueOperation(new OnLinkRemoved(notification));
56 private abstract static class AbstractLinkOperation implements TopologyOperation {
57 private final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.Link link;
59 AbstractLinkOperation(
60 final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.Link link) {
61 this.link = requireNonNull(link);
65 public final void applyOperation(final TransactionChainManager manager) {
66 applyOperation(manager, FlowCapableNodeMapping.toTopologyLink(link));
69 abstract void applyOperation(TransactionChainManager manager, Link link);
72 private final class OnLinkDiscovered extends AbstractLinkOperation {
73 OnLinkDiscovered(final LinkDiscovered notification) {
78 public void applyOperation(final TransactionChainManager manager, final Link link) {
79 manager.mergeToTransaction(LogicalDatastoreType.OPERATIONAL,
80 TopologyManagerUtil.linkPath(link, iiToTopology), link, true);
84 public String toString() {
85 return "onLinkDiscovered";
89 private final class OnLinkRemoved extends AbstractLinkOperation {
90 OnLinkRemoved(final LinkRemoved notification) {
95 public void applyOperation(final TransactionChainManager manager, final Link link) {
96 final var linkPath = TopologyManagerUtil.linkPath(link, iiToTopology);
98 Optional<Link> linkOptional = Optional.empty();
100 // read that checks if link exists (if we do not do this we might get an exception on delete)
101 linkOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, linkPath).get();
102 } catch (InterruptedException | ExecutionException e) {
103 LOG.warn("Error occurred when trying to read Link: {}", e.getMessage());
104 LOG.debug("Error occurred when trying to read Link.. ", e);
106 if (linkOptional.isPresent()) {
107 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath);
112 public String toString() {
113 return "onLinkRemoved";