2193a13576b0d9958dd98bbca8939b533ac94f5e
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / TopologyManagerUtil.java
1 /*
2  * Copyright (c) 2015, 2017 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 java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
13 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 final class TopologyManagerUtil {
23
24     private static final Logger LOG = LoggerFactory.getLogger(TopologyManagerUtil.class);
25
26     private TopologyManagerUtil() {
27     }
28
29     static void removeAffectedLinks(final NodeId id, final TransactionChainManager manager,
30                                     InstanceIdentifier<Topology> topology) {
31         Optional<Topology> topologyOptional = Optional.empty();
32         try {
33             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).get();
34         } catch (InterruptedException | ExecutionException 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,
44                                             TransactionChainManager manager,
45                                             final InstanceIdentifier<Topology> topology) {
46         if (!topologyOptional.isPresent()) {
47             return;
48         }
49
50         for (Link link : topologyOptional.get().nonnullLink()) {
51             if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
52                 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
53             }
54         }
55     }
56
57     static void removeAffectedLinks(final TpId id, final TransactionChainManager manager,
58                                     final InstanceIdentifier<Topology> topology) {
59         Optional<Topology> topologyOptional = Optional.empty();
60         try {
61             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).get();
62         } catch (InterruptedException | ExecutionException 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,
72                                             TransactionChainManager manager,
73                                             final InstanceIdentifier<Topology> topology) {
74         if (!topologyOptional.isPresent()) {
75             return;
76         }
77
78         for (Link link : topologyOptional.get().nonnullLink()) {
79             if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
80                 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
81             }
82         }
83     }
84
85     static InstanceIdentifier<Link> linkPath(final Link link, final InstanceIdentifier<Topology> topology) {
86         return topology.child(Link.class, link.key());
87     }
88
89
90 }