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