lower log severity to warn where appropriate
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyExporter.java
1 /*
2  * Copyright (c) 2014 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 static org.opendaylight.openflowplugin.applications.topology.manager.FlowCapableNodeMapping.getNodeConnectorKey;
11 import static org.opendaylight.openflowplugin.applications.topology.manager.FlowCapableNodeMapping.getNodeKey;
12 import static org.opendaylight.openflowplugin.applications.topology.manager.FlowCapableNodeMapping.toTerminationPointId;
13 import static org.opendaylight.openflowplugin.applications.topology.manager.FlowCapableNodeMapping.toTopologyLink;
14 import static org.opendaylight.openflowplugin.applications.topology.manager.FlowCapableNodeMapping.toTopologyNodeId;
15
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
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.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener {
42
43     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
44     protected final InstanceIdentifier<Topology> iiToTopology;
45     private final OperationProcessor processor;
46
47     FlowCapableTopologyExporter(final OperationProcessor processor,
48             final InstanceIdentifier<Topology> topology) {
49         this.processor = Preconditions.checkNotNull(processor);
50         this.iiToTopology = Preconditions.checkNotNull(topology);
51     }
52
53     @Override
54     public void onLinkDiscovered(final LinkDiscovered notification) {
55         processor.enqueueOperation(new TopologyOperation() {
56             @Override
57             public void applyOperation(final ReadWriteTransaction transaction) {
58                 final Link link = toTopologyLink(notification);
59                 final InstanceIdentifier<Link> path = TopologyManagerUtil.linkPath(link, iiToTopology);
60                 transaction.merge(LogicalDatastoreType.OPERATIONAL, path, link, true);
61             }
62
63             @Override
64             public String toString() {
65                 return "onLinkDiscovered";
66             }
67         });
68     }
69
70     @Override
71     public void onLinkOverutilized(final LinkOverutilized notification) {
72         // NOOP
73     }
74
75     @Override
76     public void onLinkRemoved(final LinkRemoved notification) {
77         processor.enqueueOperation(new TopologyOperation() {
78             @Override
79             public void applyOperation(final ReadWriteTransaction transaction) {
80                 Optional<Link> linkOptional = Optional.absent();
81                 try {
82                     // read that checks if link exists (if we do not do this we might get an exception on delete)
83                     linkOptional = transaction.read(LogicalDatastoreType.OPERATIONAL,
84                             TopologyManagerUtil.linkPath(toTopologyLink(notification), iiToTopology)).checkedGet();
85                 } catch (ReadFailedException e) {
86                     LOG.warn("Error occured when trying to read Link: {}", e.getMessage());
87                     LOG.debug("Error occured when trying to read Link.. ", e);
88                 }
89                 if (linkOptional.isPresent()) {
90                     transaction.delete(LogicalDatastoreType.OPERATIONAL, TopologyManagerUtil.linkPath(toTopologyLink(notification), iiToTopology));
91                 }
92             }
93
94             @Override
95             public String toString() {
96                 return "onLinkRemoved";
97             }
98         });
99     }
100
101     @Override
102     public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
103         // NOOP
104     }
105
106     private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
107         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
108         NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
109         return iiToTopology.child(Node.class, nodeKey);
110     }
111
112     private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
113         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
114         NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
115         return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
116     }
117
118
119
120     private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
121         return iiToTopology.child(Node.class, new NodeKey(nodeId));
122     }
123
124     private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
125         NodeKey nodeKey = new NodeKey(nodeId);
126         TerminationPointKey tpKey = new TerminationPointKey(tpId);
127         return iiToTopology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
128     }
129
130 }