Merge "Added test for MouontPoints and URI"
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / topology / TopologyMapping.xtend
1 package org.opendaylight.controller.sal.compatibility.topology
2
3 import com.google.common.collect.FluentIterable
4 import java.util.Collections
5 import java.util.List
6 import java.util.concurrent.CopyOnWriteArrayList
7 import org.opendaylight.controller.sal.core.ConstructionException
8 import org.opendaylight.controller.sal.core.Edge
9 import org.opendaylight.controller.sal.core.Node
10 import org.opendaylight.controller.sal.core.NodeConnector
11 import org.opendaylight.controller.sal.core.UpdateType
12 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate
13 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link
17
18 import static com.google.common.base.Preconditions.*
19 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
20 import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector
23 import org.slf4j.LoggerFactory
24
25 class TopologyMapping {
26     private static val LOG = LoggerFactory.getLogger(TopologyMapping);
27     private new() {
28         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
29     }
30     
31     public static def toADEdgeUpdates(Topology topology,TypeSafeDataReader reader) {
32         val List<TopoEdgeUpdate> result = new CopyOnWriteArrayList<TopoEdgeUpdate>()
33         return FluentIterable.from(topology.link).transform[toAdEdge(topology).toTopoEdgeUpdate(reader)].copyInto(result)
34     }
35     
36     public static def toAdEdge(Link link,Topology topology) {
37         val adSrc = link.source.sourceTp.toADNodeConnector(link.source.sourceNode)
38         val adDst = link.destination.destTp.toADNodeConnector(link.destination.destNode)
39         return new Edge(adSrc,adDst); 
40     }
41     
42     public static def toTopoEdgeUpdate(Edge e,TypeSafeDataReader reader) {
43         return toTopoEdgeUpdate(e,UpdateType.ADDED,reader)
44     }
45     
46     public static def toTopoEdgeUpdate(Edge e,UpdateType type,TypeSafeDataReader reader) {
47         return new TopoEdgeUpdate(e,e.toAdEdgeProperties(reader),type)
48     }
49     
50     public static def toAdEdgeProperties(Edge e,TypeSafeDataReader reader) {
51         val ncref = e.tailNodeConnector.toNodeConnectorRef
52         if(ncref == null) {
53             LOG.debug("Edge {} ncref {}",e,ncref)
54             return null;
55         }
56         val ncInstanceId = (ncref.value as InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector>)
57         if(ncInstanceId == null) {
58             LOG.debug("Edge {} ncref {}",e,ncref)
59             return null;
60         }
61         val nc = reader.readOperationalData(ncInstanceId)
62         if(nc == null) {
63             return null;
64         }
65         return nc.toADNodeConnectorProperties     
66     }
67     
68     public static def toADNodeId(NodeId nodeId) {
69         checkNotNull(nodeId);
70         return nodeId.value
71     }
72     public static def toADNodeConnector(TpId source,NodeId nodeId) throws ConstructionException {
73         checkNotNull(source);
74         return new NodeConnector(MD_SAL_TYPE,source.toADNodeConnectorId,nodeId.toADNode)
75     }
76     
77     public static def toADNodeConnectorId(TpId nodeConnectorId) {
78         return nodeConnectorId.value
79     }
80     
81     public static def toADNode(NodeId nodeId) {
82         checkNotNull(nodeId);
83         return new Node(MD_SAL_TYPE,nodeId.toADNodeId);       
84     }
85 }