Implement finding a primary based on the shard name and do basic wiring of Distribute...
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / topology / TopologyMapping.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.controller.sal.compatibility.topology;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import java.util.List;
13 import java.util.Set;
14 import java.util.concurrent.CopyOnWriteArrayList;
15
16 import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
17 import org.opendaylight.controller.sal.compatibility.NodeMapping;
18 import org.opendaylight.controller.sal.core.ConstructionException;
19 import org.opendaylight.controller.sal.core.Edge;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.core.Property;
23 import org.opendaylight.controller.sal.core.UpdateType;
24 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.base.Function;
35 import com.google.common.collect.FluentIterable;
36
37 public final class TopologyMapping {
38     private static final Logger LOG = LoggerFactory.getLogger(TopologyMapping.class);
39
40     private TopologyMapping() {
41         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
42     }
43
44     public static List<TopoEdgeUpdate> toADEdgeUpdates(final Topology topology,final TypeSafeDataReader reader) {
45         final List<TopoEdgeUpdate> result = new CopyOnWriteArrayList<>();
46         return FluentIterable.from(topology.getLink()).transform(
47                 new Function<Link, TopoEdgeUpdate>() {
48                     @Override
49                     public TopoEdgeUpdate apply(final Link input) {
50                         try {
51                             return toTopoEdgeUpdate(toAdEdge(input, topology), reader);
52                         } catch (ConstructionException e) {
53                             throw new IllegalArgumentException(String.format("Failed to construct edge update for {}", input), e);
54                         }
55                     }}
56                 ).copyInto(result);
57     }
58
59     public static Edge toAdEdge(final Link link, final Topology topology) throws ConstructionException {
60         final NodeConnector adSrc = toADNodeConnector(link.getSource().getSourceTp(), link.getSource().getSourceNode());
61         final NodeConnector adDst = toADNodeConnector(link.getDestination().getDestTp(), link.getDestination().getDestNode());
62         return new Edge(adSrc, adDst);
63     }
64
65     public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e, final TypeSafeDataReader reader) {
66         return toTopoEdgeUpdate(e, UpdateType.ADDED, reader);
67     }
68
69     public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e,final UpdateType type,final TypeSafeDataReader reader) {
70         return new TopoEdgeUpdate(e, toAdEdgeProperties(e, reader), type);
71     }
72
73     public static Set<Property> toAdEdgeProperties(final Edge e,final TypeSafeDataReader reader) {
74         final NodeConnectorRef ncref = NodeMapping.toNodeConnectorRef(e.getTailNodeConnector());
75         if(ncref == null) {
76             LOG.debug("Edge {} ncref {}",e,ncref);
77             return null;
78         }
79
80         @SuppressWarnings("unchecked")
81         final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector> ncInstanceId =
82         (InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector>) ncref.getValue();
83         if(ncInstanceId == null) {
84             LOG.debug("Edge {} ncref {}",e,ncref);
85             return null;
86         }
87
88         final  org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc = reader.readOperationalData(ncInstanceId);
89         if(nc == null) {
90             return null;
91         }
92         return NodeMapping.toADNodeConnectorProperties(nc);
93     }
94
95     public static String toADNodeId(final NodeId nodeId) {
96         return nodeId.getValue();
97     }
98
99     public static NodeConnector toADNodeConnector(final TpId source, final NodeId nodeId) throws ConstructionException {
100         checkNotNull(source);
101         return new NodeConnector(NodeMapping.MD_SAL_TYPE, toADNodeConnectorId(source), toADNode(nodeId));
102     }
103
104     public static String toADNodeConnectorId(final TpId nodeConnectorId) {
105         return nodeConnectorId.getValue();
106     }
107
108     public static Node toADNode(final NodeId nodeId) throws ConstructionException {
109         checkNotNull(nodeId);
110         return new Node(NodeMapping.MD_SAL_TYPE, toADNodeId(nodeId));
111     }
112 }