32be9a22d54146b51f12005e69589e0f9f0a292b
[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         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));
50         return nodePath;
51     }
52
53     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
54         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
55                         new PortNumber(client.getConnectionInfo().getRemotePort()));
56     }
57
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);
67         return path;
68     }
69
70     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
71         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
72         return nodeKey.getNodeId();
73     }
74
75     public static IpAddress createIpAddress(InetAddress address) {
76         IpAddress ip = null;
77         if (address instanceof Inet4Address) {
78             ip = createIpAddress((Inet4Address)address);
79         } else if (address instanceof Inet6Address) {
80             ip = createIpAddress((Inet6Address)address);
81         }
82         return ip;
83     }
84
85     public static IpAddress createIpAddress(Inet4Address address) {
86         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
87         return new IpAddress(ipv4);
88     }
89
90     public static IpAddress createIpAddress(Inet6Address address) {
91         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
92         return new IpAddress(ipv6);
93     }
94
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();
102     }
103
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();
109     }
110
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());
116         } else {
117             throw new UnknownHostException("IP Address has no value");
118         }
119     }
120
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);
127         } else {
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)
136                             .build();
137         }
138         return iid;
139     }
140 }