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