77cbcdb8a4a7be6aa96beb8dd54a3b6c5fd2bf8b
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundMapper.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
10
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;
32
33 public class HwvtepSouthboundMapper {
34     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
35     private static final String N_CONNECTIONS_STR = "n_connections";
36
37     private static NodeId createNodeId(HwvtepConnectionInstance client) {
38         NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
39         return key.getNodeId();
40
41     }
42
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));
48         return nodePath;
49     }
50
51     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
52         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
53                         new PortNumber(client.getConnectionInfo().getRemotePort()));
54     }
55
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);
65         return path;
66     }
67
68     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
69         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
70         return nodeKey.getNodeId();
71     }
72
73     public static IpAddress createIpAddress(InetAddress address) {
74         IpAddress ip = null;
75         if (address instanceof Inet4Address) {
76             ip = createIpAddress((Inet4Address)address);
77         } else if (address instanceof Inet6Address) {
78             ip = createIpAddress((Inet6Address)address);
79         }
80         return ip;
81     }
82
83     public static IpAddress createIpAddress(Inet4Address address) {
84         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
85         return new IpAddress(ipv4);
86     }
87
88     public static IpAddress createIpAddress(Inet6Address address) {
89         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
90         return new IpAddress(ipv6);
91     }
92
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();
100     }
101
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();
107     }
108
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);
115         } else {
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)
124                             .build();
125         }
126         return iid;
127     }
128 }