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