Merge "removed unused import 'org.opendaylight.yang.gen.v1.urn.ietf.params.xml. ns...
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundMapper.java
index 77cbcdb8a4a7be6aa96beb8dd54a3b6c5fd2bf8b..b98cc0d400d67a4abbf10123e5bb7b026db4780b 100644 (file)
@@ -11,41 +11,59 @@ package org.opendaylight.ovsdb.hwvtepsouthbound;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
+
 import org.opendaylight.ovsdb.lib.OvsdbClient;
 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
+import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
+import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
+import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableBiMap;
+import com.google.common.net.InetAddresses;
+
 public class HwvtepSouthboundMapper {
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
     private static final String N_CONNECTIONS_STR = "n_connections";
 
-    private static NodeId createNodeId(HwvtepConnectionInstance client) {
-        NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
-        return key.getNodeId();
-
-    }
-
     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
-        InstanceIdentifier<Node> nodePath = InstanceIdentifier
+        return InstanceIdentifier
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
                 .child(Node.class,new NodeKey(nodeId));
-        return nodePath;
     }
 
     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
@@ -55,7 +73,7 @@ public class HwvtepSouthboundMapper {
 
     private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
         String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
-                + new String(ip.getValue()) + ":" + port.getValue();
+                + String.valueOf(ip.getValue()) + ":" + port.getValue();
         Uri uri = new Uri(uriString);
         NodeId nodeId = new NodeId(uri);
         InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
@@ -66,7 +84,7 @@ public class HwvtepSouthboundMapper {
     }
 
     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
-        NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
+        NodeKey nodeKey = iid.firstKeyOf(Node.class);
         return nodeKey.getNodeId();
     }
 
@@ -81,8 +99,7 @@ public class HwvtepSouthboundMapper {
     }
 
     public static IpAddress createIpAddress(Inet4Address address) {
-        Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
-        return new IpAddress(ipv4);
+        return IetfInetUtil.INSTANCE.ipAddressFor(address);
     }
 
     public static IpAddress createIpAddress(Inet6Address address) {
@@ -106,23 +123,108 @@ public class HwvtepSouthboundMapper {
         return connectionInfoBuilder.build();
     }
 
-    public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
-        InstanceIdentifier<Node> iid = null;
-        if (global.getManagersColumn() != null
-                && global.getManagersColumn().getData() != null) {
-            String iidString = global.getManagersColumn().getData().iterator().next().toString();
-            iid = (InstanceIdentifier<Node>) HwvtepSouthboundUtil.deserializeInstanceIdentifier(iidString);
+    public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
+        if (ip.getIpv4Address() != null) {
+            return InetAddresses.forString(ip.getIpv4Address().getValue());
+        } else if (ip.getIpv6Address() != null) {
+            return InetAddress.getByName(ip.getIpv6Address().getValue());
         } else {
-            String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
-                            HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
-            NodeId nodeId = new NodeId(new Uri(nodeString));
-            NodeKey nodeKey = new NodeKey(nodeId);
-            TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
-            iid = InstanceIdentifier.builder(NetworkTopology.class)
-                            .child(Topology.class, topoKey)
-                            .child(Node.class,nodeKey)
-                            .build();
+            throw new UnknownHostException("IP Address has no value");
         }
+    }
+
+    public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
+        String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
+                HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
+        NodeId nodeId = new NodeId(new Uri(nodeString));
+        NodeKey nodeKey = new NodeKey(nodeId);
+        TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
+        return InstanceIdentifier.builder(NetworkTopology.class)
+                .child(Topology.class, topoKey)
+                .child(Node.class, nodeKey)
+                .build();
+    }
+
+    public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
+                    PhysicalSwitch pSwitch) {
+        //TODO: Clean this up
+        return createInstanceIdentifier(client, new HwvtepNodeName(pSwitch.getName()));
+    }
+
+    public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
+                    HwvtepNodeName psName) {
+        NodeKey nodeKey = new NodeKey(createManagedNodeId(client, psName));
+        return InstanceIdentifier.builder(NetworkTopology.class)
+                        .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
+                        .child(Node.class, nodeKey).build();
+    }
+
+    public static NodeId createManagedNodeId(HwvtepConnectionInstance client, HwvtepNodeName psName) {
+        String nodeString = client.getNodeKey().getNodeId().getValue()
+                        + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/" + psName.getValue();
+        NodeId nodeId = new NodeId(new Uri(nodeString));
+        return nodeId;
+    }
+
+    public static InstanceIdentifier<LogicalSwitches> createInstanceIdentifier(HwvtepConnectionInstance client,
+                    LogicalSwitch lSwitch) {
+        InstanceIdentifier<LogicalSwitches> iid = null;
+        iid = InstanceIdentifier.builder(NetworkTopology.class)
+                        .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
+                        .child(Node.class, client.getNodeKey()).augmentation(HwvtepGlobalAugmentation.class)
+                        .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))
+                        .build();
+        /* TODO: Will this work instead to make it simpler?
+        iid = client.getInstanceIdentifier().builder()
+            .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))).build()
+         */
         return iid;
     }
+
+    public static InstanceIdentifier<Tunnels> createInstanceIdentifier( InstanceIdentifier<Node> nodeIid,
+                    InstanceIdentifier<TerminationPoint> localTpIid, InstanceIdentifier<TerminationPoint> remoteTpIid) {
+
+        TunnelsKey tunnelsKey = new TunnelsKey(new HwvtepPhysicalLocatorRef(localTpIid),
+                                               new HwvtepPhysicalLocatorRef(remoteTpIid));
+        InstanceIdentifier<Tunnels> tunnelInstanceId = nodeIid.builder().augmentation(PhysicalSwitchAugmentation.class)
+                                                       .child(Tunnels.class, tunnelsKey).build();
+        return tunnelInstanceId;
+    }
+
+    public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
+        Preconditions.checkNotNull(type);
+        if (type.isEmpty()) {
+            return EncapsulationTypeVxlanOverIpv4.class;
+        } else {
+            ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper =
+                    HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
+            return mapper.get(type);
+        }
+    }
+
+    public static InstanceIdentifier<TerminationPoint> createInstanceIdentifier(InstanceIdentifier<Node> nodeIid,
+                    PhysicalLocator physicalLocator) {
+        return nodeIid.child(TerminationPoint.class, getTerminationPointKey(physicalLocator));
+    }
+
+    public static TerminationPointKey getTerminationPointKey(PhysicalLocator pLoc) {
+        TerminationPointKey tpKey = null;
+        if(pLoc.getEncapsulationTypeColumn().getData() != null &&
+                        pLoc.getDstIpColumn().getData() != null) {
+            String tpKeyStr = pLoc.getEncapsulationTypeColumn().getData()+':'+pLoc.getDstIpColumn().getData();
+            tpKey = new TerminationPointKey(new TpId(tpKeyStr));
+        }
+        return tpKey;
+    }
+
+    public static String getRandomUUID() {
+        return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
+    }
+
+    public static InstanceIdentifier<VlanBindings> createInstanceIdentifier(HwvtepConnectionInstance client,
+            InstanceIdentifier<TerminationPoint> tpPath, VlanBindings vBindings) {
+                return tpPath.augmentation(HwvtepPhysicalPortAugmentation.class) .child(VlanBindings.class,
+                        new VlanBindingsKey(vBindings.getKey()));
+
+    }
 }