Switch to command pattern and using transaction chains
[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     public static Node createNode(OvsdbClientKey key) {
46         NodeBuilder nodeBuilder = new NodeBuilder();
47         nodeBuilder.setNodeId(createNodeId(key.getIp(),key.getPort()));
48         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
49         return nodeBuilder.build();
50     }
51
52     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
53         return createOvsdbAugmentation(new OvsdbClientKey(client));
54     }
55
56     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClientKey key) {
57         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
58         ovsdbNodeBuilder.setIp(key.getIp());
59         ovsdbNodeBuilder.setPort(key.getPort());
60         return ovsdbNodeBuilder.build();
61     }
62
63     public static IpAddress createIpAddress(InetAddress address) {
64         IpAddress ip = null;
65         if(address instanceof Inet4Address) {
66             ip = createIpAddress((Inet4Address)address);
67         } else if (address instanceof Inet6Address) {
68             ip = createIpAddress((Inet6Address)address);
69         }
70         return ip;
71     }
72
73     public static IpAddress createIpAddress(Inet4Address address) {
74         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
75         return new IpAddress(ipv4);
76     }
77
78     public static IpAddress createIpAddress(Inet6Address address) {
79         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
80         return new IpAddress(ipv6);
81     }
82
83     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
84         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
85                 new PortNumber(client.getConnectionInfo().getRemotePort()));
86     }
87     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
88         InstanceIdentifier<Node> path = InstanceIdentifier
89                 .create(NetworkTopology.class)
90                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
91                 .child(Node.class,createNodeKey(ip,port));
92         LOG.info("Created ovsdb path: {}",path);
93         return path;
94     }
95
96     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
97         return new NodeKey(createNodeId(ip,port));
98     }
99
100     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
101         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
102                 new PortNumber(connectionInfo.getRemotePort()));
103     }
104
105     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, UUID managedNodeId) {
106         return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
107                 new PortNumber(connectionInfo.getRemotePort()),
108                 managedNodeId);
109     }
110
111     public static NodeId createManagedNodeId(OvsdbClientKey key, UUID managedModeId) {
112         return createManagedNodeId(key.getIp(),key.getPort(),managedModeId);
113     }
114
115     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, UUID managedModeId) {
116         return new NodeId(createNodeId(ip,port)
117                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+":"+managedModeId.toString());
118     }
119
120     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
121         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + ":/" + new String(ip.getValue()) +
122                    ":" + port.getValue();
123         Uri uri = new Uri(uriString);
124         NodeId nodeId = new NodeId(uri);
125         return nodeId;
126     }
127
128     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
129         if(ip.getIpv4Address() != null) {
130             return InetAddress.getByName(ip.getIpv4Address().getValue());
131         } else if(ip.getIpv6Address() != null) {
132             return InetAddress.getByName(ip.getIpv6Address().getValue());
133         } else {
134             throw new UnknownHostException("IP Address has no value");
135         }
136     }
137 }