f99fb50c06050e915dd8bd23b526f810c541c9a2
[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     private static final Logger LOG = LoggerFactory.getLogger(TopologyManagerUtil.class);
24
25     private TopologyManagerUtil() {
26         // Hidden on purpose
27     }
28
29     static void removeAffectedLinks(final NodeId id, final TransactionChainManager manager,
30                                     final InstanceIdentifier<Topology> topology) {
31         final Optional<Topology> topologyOptional;
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             return;
38         }
39         if (topologyOptional.isPresent()) {
40             for (Link link : topologyOptional.orElseThrow().nonnullLink().values()) {
41                 if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
42                     manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
43                 }
44             }
45         }
46     }
47
48     static void removeAffectedLinks(final TpId id, final TransactionChainManager manager,
49                                     final InstanceIdentifier<Topology> topology) {
50         final Optional<Topology> topologyOptional;
51         try {
52             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).get();
53         } catch (InterruptedException | ExecutionException e) {
54             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
55             LOG.debug("Error reading topology data for topology..", e);
56             return;
57         }
58         if (topologyOptional.isPresent()) {
59             for (Link link : topologyOptional.orElseThrow().nonnullLink().values()) {
60                 if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
61                     manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
62                 }
63             }
64         }
65     }
66
67     static InstanceIdentifier<Link> linkPath(final Link link, final InstanceIdentifier<Topology> topology) {
68         return topology.child(Link.class, link.key());
69     }
70 }