2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
16 import org.opendaylight.ovsdb.lib.OvsdbClient;
17 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
18 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
19 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Preconditions;
44 import com.google.common.collect.ImmutableBiMap;
46 public class HwvtepSouthboundMapper {
47 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
48 private static final String N_CONNECTIONS_STR = "n_connections";
50 private static NodeId createNodeId(HwvtepConnectionInstance client) {
51 NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
52 return key.getNodeId();
56 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
57 InstanceIdentifier<Node> nodePath = InstanceIdentifier
58 .create(NetworkTopology.class)
59 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
60 .child(Node.class,new NodeKey(nodeId));
64 public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
65 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
66 new PortNumber(client.getConnectionInfo().getRemotePort()));
69 private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
70 String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
71 + new String(ip.getValue()) + ":" + port.getValue();
72 Uri uri = new Uri(uriString);
73 NodeId nodeId = new NodeId(uri);
74 InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
75 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
76 .child(Node.class,new NodeKey(nodeId));
77 LOG.debug("Created ovsdb path: {}",path);
81 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
82 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
83 return nodeKey.getNodeId();
86 public static IpAddress createIpAddress(InetAddress address) {
88 if (address instanceof Inet4Address) {
89 ip = createIpAddress((Inet4Address)address);
90 } else if (address instanceof Inet6Address) {
91 ip = createIpAddress((Inet6Address)address);
96 public static IpAddress createIpAddress(Inet4Address address) {
97 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
98 return new IpAddress(ipv4);
101 public static IpAddress createIpAddress(Inet6Address address) {
102 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
103 return new IpAddress(ipv6);
106 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
107 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
108 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
109 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
110 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
111 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
112 return connectionInfoBuilder.build();
115 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
116 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
117 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
118 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
119 return connectionInfoBuilder.build();
122 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
123 if (ip.getIpv4Address() != null) {
124 return InetAddress.getByName(ip.getIpv4Address().getValue());
125 } else if (ip.getIpv6Address() != null) {
126 return InetAddress.getByName(ip.getIpv6Address().getValue());
128 throw new UnknownHostException("IP Address has no value");
132 public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
133 InstanceIdentifier<Node> iid = null;
134 String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
135 HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
136 NodeId nodeId = new NodeId(new Uri(nodeString));
137 NodeKey nodeKey = new NodeKey(nodeId);
138 TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
139 iid = InstanceIdentifier.builder(NetworkTopology.class)
140 .child(Topology.class, topoKey)
141 .child(Node.class,nodeKey)
146 public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
147 PhysicalSwitch pSwitch) {
148 InstanceIdentifier<Node> iid = null;
149 String nodeString = client.getNodeKey().getNodeId().getValue() + "/physicalswitch/" + pSwitch.getName();
150 NodeId nodeId = new NodeId(new Uri(nodeString));
151 NodeKey nodeKey = new NodeKey(nodeId);
152 iid =InstanceIdentifier.builder(NetworkTopology.class)
153 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
154 .child(Node.class, nodeKey).build();
158 public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
159 LogicalSwitch lSwitch) {
160 InstanceIdentifier<Node> iid = null;
161 String nodeString = client.getNodeKey().getNodeId().getValue() + "/logicalswitch/" + lSwitch.getName();
162 NodeId nodeId = new NodeId(new Uri(nodeString));
163 NodeKey nodeKey = new NodeKey(nodeId);
164 iid =InstanceIdentifier.builder(NetworkTopology.class)
165 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
166 .child(Node.class, nodeKey).build();
170 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
171 Preconditions.checkNotNull(type);
172 if (type.isEmpty()) {
173 return EncapsulationTypeVxlanOverIpv4.class;
175 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper =
176 HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
177 return mapper.get(type);
181 public static InstanceIdentifier<TerminationPoint> createInstanceIdentifier(InstanceIdentifier<Node> nodeIid,
182 PhysicalLocator physicalLocator) {
183 return nodeIid.child(TerminationPoint.class, getTerminationPointKey(physicalLocator));
186 public static TerminationPointKey getTerminationPointKey(PhysicalLocator pLoc) {
187 TerminationPointKey tpKey = null;
188 if(pLoc.getEncapsulationTypeColumn().getData() != null &&
189 pLoc.getDstIpColumn().getData() != null) {
190 String tpKeyStr = pLoc.getEncapsulationTypeColumn().getData()+':'+pLoc.getDstIpColumn().getData();
191 tpKey = new TerminationPointKey(new TpId(tpKeyStr));