d2577e25df781d8c3698e2f5d958c6c50e02e732
[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 com.google.common.base.Optional;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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 final class TopologyManagerUtil {
25
26     private static final Logger LOG = LoggerFactory.getLogger(TopologyManagerUtil.class);
27
28     private TopologyManagerUtil() {
29     }
30
31     static void removeAffectedLinks(final NodeId id, final TransactionChainManager manager,
32                                     InstanceIdentifier<Topology> topology) {
33         Optional<Topology> topologyOptional = Optional.absent();
34         try {
35             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).get();
36         } catch (InterruptedException | ExecutionException e) {
37             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
38             LOG.debug("Error reading topology data for topology.. ", e);
39         }
40         if (topologyOptional.isPresent()) {
41             removeAffectedLinks(id, topologyOptional, manager, topology);
42         }
43     }
44
45     private static void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional,
46                                             TransactionChainManager manager,
47                                             final InstanceIdentifier<Topology> topology) {
48         if (!topologyOptional.isPresent()) {
49             return;
50         }
51
52         List<Link> linkList =
53                 topologyOptional.get().getLink() != null ? topologyOptional.get().getLink() : Collections.emptyList();
54         for (Link link : linkList) {
55             if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
56                 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
57             }
58         }
59     }
60
61     static void removeAffectedLinks(final TpId id, final TransactionChainManager manager,
62                                     final InstanceIdentifier<Topology> topology) {
63         Optional<Topology> topologyOptional = Optional.absent();
64         try {
65             topologyOptional = manager.readFromTransaction(LogicalDatastoreType.OPERATIONAL, topology).get();
66         } catch (InterruptedException | ExecutionException e) {
67             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
68             LOG.debug("Error reading topology data for topology..", e);
69         }
70         if (topologyOptional.isPresent()) {
71             removeAffectedLinks(id, topologyOptional, manager, topology);
72         }
73     }
74
75     private static void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional,
76                                             TransactionChainManager manager,
77                                             final InstanceIdentifier<Topology> topology) {
78         if (!topologyOptional.isPresent()) {
79             return;
80         }
81
82         List<Link> linkList = topologyOptional.get().getLink() != null ? topologyOptional.get()
83                 .getLink() : Collections.<Link>emptyList();
84         for (Link link : linkList) {
85             if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
86                 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
87             }
88         }
89     }
90
91     static InstanceIdentifier<Link> linkPath(final Link link, final InstanceIdentifier<Topology> topology) {
92         return topology.child(Link.class, link.key());
93     }
94
95
96 }