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 org.opendaylight.ovsdb.lib.OvsdbClient;
15 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
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.network.topology.Topology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class HwvtepSouthboundMapper {
34 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
35 private static final String N_CONNECTIONS_STR = "n_connections";
37 private static NodeId createNodeId(HwvtepConnectionInstance client) {
38 NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
39 return key.getNodeId();
43 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
44 InstanceIdentifier<Node> nodePath = InstanceIdentifier
45 .create(NetworkTopology.class)
46 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
47 .child(Node.class,new NodeKey(nodeId));
51 public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
52 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
53 new PortNumber(client.getConnectionInfo().getRemotePort()));
56 private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
57 String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
58 + new String(ip.getValue()) + ":" + port.getValue();
59 Uri uri = new Uri(uriString);
60 NodeId nodeId = new NodeId(uri);
61 InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
62 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
63 .child(Node.class,new NodeKey(nodeId));
64 LOG.debug("Created ovsdb path: {}",path);
68 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
69 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
70 return nodeKey.getNodeId();
73 public static IpAddress createIpAddress(InetAddress address) {
75 if (address instanceof Inet4Address) {
76 ip = createIpAddress((Inet4Address)address);
77 } else if (address instanceof Inet6Address) {
78 ip = createIpAddress((Inet6Address)address);
83 public static IpAddress createIpAddress(Inet4Address address) {
84 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
85 return new IpAddress(ipv4);
88 public static IpAddress createIpAddress(Inet6Address address) {
89 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
90 return new IpAddress(ipv6);
93 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
94 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
95 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
96 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
97 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
98 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
99 return connectionInfoBuilder.build();
102 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
103 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
104 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
105 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
106 return connectionInfoBuilder.build();
109 public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
110 InstanceIdentifier<Node> iid = null;
111 if (global.getManagersColumn() != null
112 && global.getManagersColumn().getData() != null) {
113 String iidString = global.getManagersColumn().getData().iterator().next().toString();
114 iid = (InstanceIdentifier<Node>) HwvtepSouthboundUtil.deserializeInstanceIdentifier(iidString);
116 String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
117 HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
118 NodeId nodeId = new NodeId(new Uri(nodeString));
119 NodeKey nodeKey = new NodeKey(nodeId);
120 TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
121 iid = InstanceIdentifier.builder(NetworkTopology.class)
122 .child(Topology.class, topoKey)
123 .child(Node.class,nodeKey)