Fix transaction manager closing.
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyExporter.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.openflowplugin.applications.topology.manager;
9
10 import static org.opendaylight.openflowplugin.applications.topology.manager.FlowCapableNodeMapping.toTopologyLink;
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener {
29
30     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
31     private final InstanceIdentifier<Topology> iiToTopology;
32     private final OperationProcessor processor;
33
34     FlowCapableTopologyExporter(final OperationProcessor processor,
35             final InstanceIdentifier<Topology> topology) {
36         this.processor = Preconditions.checkNotNull(processor);
37         this.iiToTopology = Preconditions.checkNotNull(topology);
38     }
39
40     @Override
41     public void onLinkDiscovered(final LinkDiscovered notification) {
42         processor.enqueueOperation(new TopologyOperation() {
43             @Override
44             public void applyOperation(final TransactionChainManager manager) {
45                 final Link link = toTopologyLink(notification);
46                 final InstanceIdentifier<Link> path = TopologyManagerUtil.linkPath(link, iiToTopology);
47                 manager.mergeToTransaction(LogicalDatastoreType.OPERATIONAL, path, link, true);
48             }
49
50             @Override
51             public String toString() {
52                 return "onLinkDiscovered";
53             }
54         });
55     }
56
57     @Override
58     public void onLinkOverutilized(final LinkOverutilized notification) {
59         // NOOP
60     }
61
62     @Override
63     public void onLinkRemoved(final LinkRemoved notification) {
64         processor.enqueueOperation(new TopologyOperation() {
65             @Override
66             public void applyOperation(final TransactionChainManager manager) {
67                 Optional<Link> linkOptional = Optional.absent();
68                 try {
69                     // read that checks if link exists (if we do not do this we might get an exception on delete)
70                     linkOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL,
71                             TopologyManagerUtil.linkPath(toTopologyLink(notification), iiToTopology)).checkedGet();
72                 } catch (ReadFailedException e) {
73                     LOG.warn("Error occurred when trying to read Link: {}", e.getMessage());
74                     LOG.debug("Error occurred when trying to read Link.. ", e);
75                 }
76                 if (linkOptional.isPresent()) {
77                     manager.addDeleteOperationTotTxChain(LogicalDatastoreType.OPERATIONAL,
78                             TopologyManagerUtil.linkPath(toTopologyLink(notification), iiToTopology));
79                 }
80             }
81
82             @Override
83             public String toString() {
84                 return "onLinkRemoved";
85             }
86         });
87     }
88
89     @Override
90     public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
91         // NOOP
92     }
93
94 }