/* * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ 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.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.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.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; 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 createInstanceIdentifier(NodeId nodeId) { InstanceIdentifier nodePath = InstanceIdentifier .create(NetworkTopology.class) .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)) .child(Node.class,new NodeKey(nodeId)); return nodePath; } public static InstanceIdentifier createInstanceIdentifier (OvsdbClient client) { return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()), new PortNumber(client.getConnectionInfo().getRemotePort())); } private static InstanceIdentifier createInstanceIdentifier(IpAddress ip, PortNumber port) { String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" + new String(ip.getValue()) + ":" + port.getValue(); Uri uri = new Uri(uriString); NodeId nodeId = new NodeId(uri); InstanceIdentifier path = InstanceIdentifier.create(NetworkTopology.class) .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)) .child(Node.class,new NodeKey(nodeId)); LOG.debug("Created ovsdb path: {}",path); return path; } public static NodeId createManagedNodeId(InstanceIdentifier iid) { NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class); return nodeKey.getNodeId(); } public static IpAddress createIpAddress(InetAddress address) { IpAddress ip = null; if (address instanceof Inet4Address) { ip = createIpAddress((Inet4Address)address); } else if (address instanceof Inet6Address) { ip = createIpAddress((Inet6Address)address); } return ip; } public static IpAddress createIpAddress(Inet4Address address) { Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress()); return new IpAddress(ipv4); } public static IpAddress createIpAddress(Inet6Address address) { Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress()); return new IpAddress(ipv6); } public static ConnectionInfo createConnectionInfo(OvsdbClient client) { ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder(); connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress())); connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort())); connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress())); connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort())); return connectionInfoBuilder.build(); } public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) { ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder(); connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp()); connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort()); return connectionInfoBuilder.build(); } public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException { if (ip.getIpv4Address() != null) { return InetAddress.getByName(ip.getIpv4Address().getValue()); } else if (ip.getIpv6Address() != null) { return InetAddress.getByName(ip.getIpv6Address().getValue()); } else { throw new UnknownHostException("IP Address has no value"); } } public static InstanceIdentifier getInstanceIdentifier(Global global) { InstanceIdentifier iid = null; 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(); return iid; } public static InstanceIdentifier createInstanceIdentifier(HwvtepConnectionInstance client, PhysicalSwitch pSwitch) { InstanceIdentifier iid = null; String nodeString = client.getNodeKey().getNodeId().getValue() + "/physicalswitch/" + pSwitch.getName(); NodeId nodeId = new NodeId(new Uri(nodeString)); NodeKey nodeKey = new NodeKey(nodeId); iid =InstanceIdentifier.builder(NetworkTopology.class) .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)) .child(Node.class, nodeKey).build(); return iid; } public static InstanceIdentifier createInstanceIdentifier(HwvtepConnectionInstance client, LogicalSwitch lSwitch) { InstanceIdentifier iid = null; String nodeString = client.getNodeKey().getNodeId().getValue() + "/logicalswitch/" + lSwitch.getName(); NodeId nodeId = new NodeId(new Uri(nodeString)); NodeKey nodeKey = new NodeKey(nodeId); iid =InstanceIdentifier.builder(NetworkTopology.class) .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)) .child(Node.class, nodeKey).build(); return iid; } public static Class createEncapsulationType(String type) { Preconditions.checkNotNull(type); if (type.isEmpty()) { return EncapsulationTypeVxlanOverIpv4.class; } else { ImmutableBiMap> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse(); return mapper.get(type); } } public static InstanceIdentifier createInstanceIdentifier(InstanceIdentifier 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; } }