Merge "Bug 4641 - openstack.net-virt-sfc-features fail to find artifacts"
[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.ovsdb.schema.hardwarevtep.PhysicalSwitch;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
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 HwvtepSouthboundMapper {
37     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
38     private static final String N_CONNECTIONS_STR = "n_connections";
39
40     private static NodeId createNodeId(HwvtepConnectionInstance client) {
41         NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
42         return key.getNodeId();
43
44     }
45
46     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
47         InstanceIdentifier<Node> nodePath = InstanceIdentifier
48                 .create(NetworkTopology.class)
49                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
50                 .child(Node.class,new NodeKey(nodeId));
51         return nodePath;
52     }
53
54     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
55         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
56                         new PortNumber(client.getConnectionInfo().getRemotePort()));
57     }
58
59     private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
60         String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
61                 + new String(ip.getValue()) + ":" + port.getValue();
62         Uri uri = new Uri(uriString);
63         NodeId nodeId = new NodeId(uri);
64         InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
65                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
66                         .child(Node.class,new NodeKey(nodeId));
67         LOG.debug("Created ovsdb path: {}",path);
68         return path;
69     }
70
71     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
72         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
73         return nodeKey.getNodeId();
74     }
75
76     public static IpAddress createIpAddress(InetAddress address) {
77         IpAddress ip = null;
78         if (address instanceof Inet4Address) {
79             ip = createIpAddress((Inet4Address)address);
80         } else if (address instanceof Inet6Address) {
81             ip = createIpAddress((Inet6Address)address);
82         }
83         return ip;
84     }
85
86     public static IpAddress createIpAddress(Inet4Address address) {
87         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
88         return new IpAddress(ipv4);
89     }
90
91     public static IpAddress createIpAddress(Inet6Address address) {
92         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
93         return new IpAddress(ipv6);
94     }
95
96     public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
97         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
98         connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
99         connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
100         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
101         connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
102         return connectionInfoBuilder.build();
103     }
104
105     public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
106         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
107         connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
108         connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
109         return connectionInfoBuilder.build();
110     }
111
112     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
113         if (ip.getIpv4Address() != null) {
114             return InetAddress.getByName(ip.getIpv4Address().getValue());
115         } else if (ip.getIpv6Address() != null) {
116             return InetAddress.getByName(ip.getIpv6Address().getValue());
117         } else {
118             throw new UnknownHostException("IP Address has no value");
119         }
120     }
121
122     public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
123         InstanceIdentifier<Node> iid = null;
124         String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
125                         HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
126             NodeId nodeId = new NodeId(new Uri(nodeString));
127             NodeKey nodeKey = new NodeKey(nodeId);
128             TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
129             iid = InstanceIdentifier.builder(NetworkTopology.class)
130                             .child(Topology.class, topoKey)
131                             .child(Node.class,nodeKey)
132                             .build();
133         return iid;
134     }
135
136     public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
137                     PhysicalSwitch pSwitch) {
138         InstanceIdentifier<Node> iid = null;
139         String nodeString = client.getNodeKey().getNodeId().getValue() + "/physicalswitch/" + pSwitch.getName();
140         NodeId nodeId = new NodeId(new Uri(nodeString));
141         NodeKey nodeKey = new NodeKey(nodeId);
142         iid =InstanceIdentifier.builder(NetworkTopology.class)
143                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
144                         .child(Node.class, nodeKey).build();
145         return iid;
146     }
147 }