Added Removal of bridges from operational 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     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
88     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
89         InstanceIdentifier<Node> nodePath = InstanceIdentifier
90                 .create(NetworkTopology.class)
91                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
92                 .child(Node.class,new NodeKey(nodeId));
93         return nodePath;
94     }
95
96     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,UUID uuid) {
97         return createInstanceIdentifier(createManagedNodeId(key, uuid));
98     }
99
100     public static InstanceIdentifier<Node> createInstanceIndentifier(OvsdbClientKey key) {
101         return createInstanceIdentifier(key.getIp(), key.getPort());
102     }
103
104     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
105         InstanceIdentifier<Node> path = InstanceIdentifier
106                 .create(NetworkTopology.class)
107                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
108                 .child(Node.class,createNodeKey(ip,port));
109         LOG.info("Created ovsdb path: {}",path);
110         return path;
111     }
112
113     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
114         return new NodeKey(createNodeId(ip,port));
115     }
116
117     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
118         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
119                 new PortNumber(connectionInfo.getRemotePort()));
120     }
121
122     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, UUID managedNodeId) {
123         return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
124                 new PortNumber(connectionInfo.getRemotePort()),
125                 managedNodeId);
126     }
127
128     public static NodeId createManagedNodeId(OvsdbClientKey key, UUID managedModeId) {
129         return createManagedNodeId(key.getIp(),key.getPort(),managedModeId);
130     }
131
132     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, UUID managedModeId) {
133         return new NodeId(createNodeId(ip,port)
134                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+":"+managedModeId.toString());
135     }
136
137     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
138         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + ":/" + new String(ip.getValue()) +
139                    ":" + port.getValue();
140         Uri uri = new Uri(uriString);
141         NodeId nodeId = new NodeId(uri);
142         return nodeId;
143     }
144
145     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
146         if(ip.getIpv4Address() != null) {
147             return InetAddress.getByName(ip.getIpv4Address().getValue());
148         } else if(ip.getIpv6Address() != null) {
149             return InetAddress.getByName(ip.getIpv6Address().getValue());
150         } else {
151             throw new UnknownHostException("IP Address has no value");
152         }
153     }
154 }