Merge "Fetch managed node (bridge) details and augment to operational data store"
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / SouthboundMapper.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.ovsdb.southbound;
9
10 import java.net.Inet4Address;
11 import java.net.Inet6Address;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14
15 import org.opendaylight.ovsdb.lib.OvsdbClient;
16 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
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.rev150105.OvsdbNodeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
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.NodeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class SouthboundMapper {
37     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
38
39     public static Node createNode(OvsdbClient client) {
40         NodeBuilder nodeBuilder = new NodeBuilder();
41         nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
42         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
43         return nodeBuilder.build();
44     }
45
46     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
47         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
48         ovsdbNodeBuilder.setIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
49         ovsdbNodeBuilder.setPort(new PortNumber(client.getConnectionInfo().getRemotePort()));
50         return ovsdbNodeBuilder.build();
51     }
52
53     public static IpAddress createIpAddress(InetAddress address) {
54         IpAddress ip = null;
55         if(address instanceof Inet4Address) {
56             ip = createIpAddress((Inet4Address)address);
57         } else if (address instanceof Inet6Address) {
58             ip = createIpAddress((Inet6Address)address);
59         }
60         return ip;
61     }
62
63     public static IpAddress createIpAddress(Inet4Address address) {
64         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
65         return new IpAddress(ipv4);
66     }
67
68     public static IpAddress createIpAddress(Inet6Address address) {
69         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
70         return new IpAddress(ipv6);
71     }
72
73     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
74         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
75                 new PortNumber(client.getConnectionInfo().getRemotePort()));
76     }
77     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
78         InstanceIdentifier<Node> path = InstanceIdentifier
79                 .create(NetworkTopology.class)
80                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
81                 .child(Node.class,createNodeKey(ip,port));
82         LOG.info("Created ovsdb path: {}",path);
83         return path;
84     }
85
86     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
87         return new NodeKey(createNodeId(ip,port));
88     }
89
90     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
91         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
92                 new PortNumber(connectionInfo.getRemotePort()));
93     }
94
95     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, UUID managedNodeId) {
96         return new NodeId(createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
97                 new PortNumber(connectionInfo.getRemotePort())).getValue()
98                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+":"+managedNodeId.toString());
99     }
100
101     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
102         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + ":/" + new String(ip.getValue()) +
103                    ":" + port.getValue();
104         Uri uri = new Uri(uriString);
105         NodeId nodeId = new NodeId(uri);
106         return nodeId;
107     }
108
109     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
110         if(ip.getIpv4Address() != null) {
111             return InetAddress.getByName(ip.getIpv4Address().getValue());
112         } else if(ip.getIpv6Address() != null) {
113             return InetAddress.getByName(ip.getIpv6Address().getValue());
114         } else {
115             throw new UnknownHostException("IP Address has no value");
116         }
117     }
118 }