2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
16 import org.opendaylight.ovsdb.lib.OvsdbClient;
17 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
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.TopologyKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class HwvtepSouthboundMapper {
36 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
37 private static final String N_CONNECTIONS_STR = "n_connections";
39 private static NodeId createNodeId(HwvtepConnectionInstance client) {
40 NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
41 return key.getNodeId();
45 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
46 InstanceIdentifier<Node> nodePath = InstanceIdentifier
47 .create(NetworkTopology.class)
48 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
49 .child(Node.class,new NodeKey(nodeId));
53 public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
54 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
55 new PortNumber(client.getConnectionInfo().getRemotePort()));
58 private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
59 String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
60 + new String(ip.getValue()) + ":" + port.getValue();
61 Uri uri = new Uri(uriString);
62 NodeId nodeId = new NodeId(uri);
63 InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
64 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
65 .child(Node.class,new NodeKey(nodeId));
66 LOG.debug("Created ovsdb path: {}",path);
70 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
71 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
72 return nodeKey.getNodeId();
75 public static IpAddress createIpAddress(InetAddress address) {
77 if (address instanceof Inet4Address) {
78 ip = createIpAddress((Inet4Address)address);
79 } else if (address instanceof Inet6Address) {
80 ip = createIpAddress((Inet6Address)address);
85 public static IpAddress createIpAddress(Inet4Address address) {
86 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
87 return new IpAddress(ipv4);
90 public static IpAddress createIpAddress(Inet6Address address) {
91 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
92 return new IpAddress(ipv6);
95 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
96 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
97 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
98 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
99 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
100 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
101 return connectionInfoBuilder.build();
104 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
105 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
106 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
107 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
108 return connectionInfoBuilder.build();
111 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
112 if (ip.getIpv4Address() != null) {
113 return InetAddress.getByName(ip.getIpv4Address().getValue());
114 } else if (ip.getIpv6Address() != null) {
115 return InetAddress.getByName(ip.getIpv6Address().getValue());
117 throw new UnknownHostException("IP Address has no value");
121 public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
122 InstanceIdentifier<Node> iid = null;
123 if (global.getManagersColumn() != null
124 && global.getManagersColumn().getData() != null) {
125 String iidString = global.getManagersColumn().getData().iterator().next().toString();
126 iid = (InstanceIdentifier<Node>) HwvtepSouthboundUtil.deserializeInstanceIdentifier(iidString);
128 String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
129 HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
130 NodeId nodeId = new NodeId(new Uri(nodeString));
131 NodeKey nodeKey = new NodeKey(nodeId);
132 TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
133 iid = InstanceIdentifier.builder(NetworkTopology.class)
134 .child(Topology.class, topoKey)
135 .child(Node.class,nodeKey)