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