Fix transaction manager closing.
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / TopologyManagerUtil.java
1 /**
2  * Copyright (c) 2015 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 com.google.common.base.Optional;
11 import java.util.Collections;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 class TopologyManagerUtil {
25
26     private static final Logger LOG = LoggerFactory.getLogger(TopologyManagerUtil.class);
27
28     private TopologyManagerUtil() {}
29
30     static void removeAffectedLinks(final NodeId id, final TransactionChainManager manager, InstanceIdentifier<Topology> topology) {
31         Optional<Topology> topologyOptional = Optional.absent();
32         try {
33             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
34         } catch (ReadFailedException e) {
35             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
36             LOG.debug("Error reading topology data for topology.. ", e);
37         }
38         if (topologyOptional.isPresent()) {
39             removeAffectedLinks(id, topologyOptional, manager, topology);
40         }
41     }
42
43     private static void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional, TransactionChainManager manager, final InstanceIdentifier<Topology> topology) {
44         if (!topologyOptional.isPresent()) {
45             return;
46         }
47
48         List<Link> linkList = topologyOptional.get().getLink() != null ?
49                 topologyOptional.get().getLink() : Collections.emptyList();
50         for (Link link : linkList) {
51             if (id.equals(link.getSource().getSourceNode()) ||
52                     id.equals(link.getDestination().getDestNode())) {
53                 manager.addDeleteOperationTotTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
54             }
55         }
56     }
57
58     static void removeAffectedLinks(final TpId id, final TransactionChainManager manager, final InstanceIdentifier<Topology> topology) {
59         Optional<Topology> topologyOptional = Optional.absent();
60         try {
61             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
62         } catch (ReadFailedException e) {
63             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
64             LOG.debug("Error reading topology data for topology..", e);
65         }
66         if (topologyOptional.isPresent()) {
67             removeAffectedLinks(id, topologyOptional, manager, topology);
68         }
69     }
70
71     private static void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional, TransactionChainManager manager, final InstanceIdentifier<Topology> topology) {
72         if (!topologyOptional.isPresent()) {
73             return;
74         }
75
76         List<Link> linkList = topologyOptional.get().getLink() != null
77                 ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
78         for (Link link : linkList) {
79             if (id.equals(link.getSource().getSourceTp()) ||
80                     id.equals(link.getDestination().getDestTp())) {
81                 manager.addDeleteOperationTotTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
82             }
83         }
84     }
85
86     static InstanceIdentifier<Link> linkPath(final Link link, final InstanceIdentifier<Topology> topology) {
87         return topology.child(Link.class, link.getKey());
88     }
89
90
91 }