Use config for hwvtepsouthbound
[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 java.net.UnknownHostException;
15
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;
34
35 public class HwvtepSouthboundMapper {
36     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
37     private static final String N_CONNECTIONS_STR = "n_connections";
38
39     private static NodeId createNodeId(HwvtepConnectionInstance client) {
40         NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
41         return key.getNodeId();
42
43     }
44
45     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
46         return InstanceIdentifier
47                 .create(NetworkTopology.class)
48                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
49                 .child(Node.class,new NodeKey(nodeId));
50     }
51
52     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
53         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
54                         new PortNumber(client.getConnectionInfo().getRemotePort()));
55     }
56
57     private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
58         String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
59                 + new String(ip.getValue()) + ":" + port.getValue();
60         Uri uri = new Uri(uriString);
61         NodeId nodeId = new NodeId(uri);
62         InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
63                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
64                         .child(Node.class,new NodeKey(nodeId));
65         LOG.debug("Created ovsdb path: {}",path);
66         return path;
67     }
68
69     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
70         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
71         return nodeKey.getNodeId();
72     }
73
74     public static IpAddress createIpAddress(InetAddress address) {
75         IpAddress ip = null;
76         if (address instanceof Inet4Address) {
77             ip = createIpAddress((Inet4Address)address);
78         } else if (address instanceof Inet6Address) {
79             ip = createIpAddress((Inet6Address)address);
80         }
81         return ip;
82     }
83
84     public static IpAddress createIpAddress(Inet4Address address) {
85         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
86         return new IpAddress(ipv4);
87     }
88
89     public static IpAddress createIpAddress(Inet6Address address) {
90         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
91         return new IpAddress(ipv6);
92     }
93
94     public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
95         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
96         connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
97         connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
98         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
99         connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
100         return connectionInfoBuilder.build();
101     }
102
103     public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
104         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
105         connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
106         connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
107         return connectionInfoBuilder.build();
108     }
109
110     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
111         if (ip.getIpv4Address() != null) {
112             return InetAddress.getByName(ip.getIpv4Address().getValue());
113         } else if (ip.getIpv6Address() != null) {
114             return InetAddress.getByName(ip.getIpv6Address().getValue());
115         } else {
116             throw new UnknownHostException("IP Address has no value");
117         }
118     }
119
120     public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
121         InstanceIdentifier<Node> iid = null;
122         if (global.getManagersColumn() != null
123                 && global.getManagersColumn().getData() != null) {
124             String iidString = global.getManagersColumn().getData().iterator().next().toString();
125             iid = (InstanceIdentifier<Node>) HwvtepSouthboundUtil.deserializeInstanceIdentifier(iidString);
126         } else {
127             String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
128                             HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
129             NodeId nodeId = new NodeId(new Uri(nodeString));
130             NodeKey nodeKey = new NodeKey(nodeId);
131             TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
132             iid = InstanceIdentifier.builder(NetworkTopology.class)
133                             .child(Topology.class, topoKey)
134                             .child(Node.class,nodeKey)
135                             .build();
136         }
137         return iid;
138     }
139 }