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