Merge changes from topic 'blueprint'
[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.binding.api.ReadWriteTransaction;
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.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 public class TopologyManagerUtil {
25
26     private static final Logger LOG = LoggerFactory.getLogger(TopologyManagerUtil.class);
27
28     static void removeAffectedLinks(final NodeId id, final ReadWriteTransaction transaction, InstanceIdentifier<Topology> topology) {
29         Optional<Topology> topologyOptional = Optional.absent();
30         try {
31             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
32         } catch (ReadFailedException e) {
33             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
34             LOG.debug("Error reading topology data for topology.. ", e);
35         }
36         if (topologyOptional.isPresent()) {
37             removeAffectedLinks(id, topologyOptional, transaction, topology);
38         }
39     }
40
41     static void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction, final InstanceIdentifier<Topology> topology) {
42         if (!topologyOptional.isPresent()) {
43             return;
44         }
45
46         List<Link> linkList = topologyOptional.get().getLink() != null ?
47                 topologyOptional.get().getLink() : Collections.<Link> emptyList();
48         for (Link link : linkList) {
49             if (id.equals(link.getSource().getSourceNode()) ||
50                     id.equals(link.getDestination().getDestNode())) {
51                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
52             }
53         }
54     }
55
56     static void removeAffectedLinks(final TpId id, final ReadWriteTransaction transaction, final InstanceIdentifier<Topology> topology) {
57         Optional<Topology> topologyOptional = Optional.absent();
58         try {
59             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
60         } catch (ReadFailedException e) {
61             LOG.warn("Error reading topology data for topology {}: {}", topology, e.getMessage());
62             LOG.debug("Error reading topology data for topology..", e);
63         }
64         if (topologyOptional.isPresent()) {
65             removeAffectedLinks(id, topologyOptional, transaction, topology);
66         }
67     }
68
69     static void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction, final InstanceIdentifier<Topology> topology) {
70         if (!topologyOptional.isPresent()) {
71             return;
72         }
73
74         List<Link> linkList = topologyOptional.get().getLink() != null
75                 ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
76         for (Link link : linkList) {
77             if (id.equals(link.getSource().getSourceTp()) ||
78                     id.equals(link.getDestination().getDestTp())) {
79                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
80             }
81         }
82     }
83
84     static InstanceIdentifier<Link> linkPath(final Link link, final InstanceIdentifier<Topology> topology) {
85         return topology.child(Link.class, link.getKey());
86     }
87
88
89 }