Merge "Topology manager - new util class"
[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 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
11
12 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import com.google.common.base.Optional;
16 import java.util.Collections;
17 import java.util.List;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
24
25 public class TopologyManagerUtil {
26
27     private static final Logger LOG = LoggerFactory.getLogger(TopologyManagerUtil.class);
28
29     static void removeAffectedLinks(final NodeId id, final ReadWriteTransaction transaction, InstanceIdentifier<Topology> topology) {
30         Optional<Topology> topologyOptional = Optional.absent();
31         try {
32             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
33         } catch (ReadFailedException e) {
34             LOG.error("Error reading topology data for topology {}", 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.error("Error reading topology data for topology {}", topology, e);
62         }
63         if (topologyOptional.isPresent()) {
64             removeAffectedLinks(id, topologyOptional, transaction, topology);
65         }
66     }
67
68     static void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction, final InstanceIdentifier<Topology> topology) {
69         if (!topologyOptional.isPresent()) {
70             return;
71         }
72
73         List<Link> linkList = topologyOptional.get().getLink() != null
74                 ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
75         for (Link link : linkList) {
76             if (id.equals(link.getSource().getSourceTp()) ||
77                     id.equals(link.getDestination().getDestTp())) {
78                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
79             }
80         }
81     }
82
83     static InstanceIdentifier<Link> linkPath(final Link link, final InstanceIdentifier<Topology> topology) {
84         return topology.child(Link.class, link.getKey());
85     }
86
87
88 }