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.rev130715.IetfInetUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.common.base.Preconditions;
55 import com.google.common.collect.ImmutableBiMap;
56 import com.google.common.net.InetAddresses;
58 public class HwvtepSouthboundMapper {
59 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
60 private static final String N_CONNECTIONS_STR = "n_connections";
62 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
63 return InstanceIdentifier
64 .create(NetworkTopology.class)
65 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
66 .child(Node.class,new NodeKey(nodeId));
69 public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
70 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
71 new PortNumber(client.getConnectionInfo().getRemotePort()));
74 private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
75 String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
76 + String.valueOf(ip.getValue()) + ":" + port.getValue();
77 Uri uri = new Uri(uriString);
78 NodeId nodeId = new NodeId(uri);
79 InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
80 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
81 .child(Node.class,new NodeKey(nodeId));
82 LOG.debug("Created ovsdb path: {}",path);
86 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
87 NodeKey nodeKey = iid.firstKeyOf(Node.class);
88 return nodeKey.getNodeId();
91 public static IpAddress createIpAddress(InetAddress address) {
93 if (address instanceof Inet4Address) {
94 ip = createIpAddress((Inet4Address)address);
95 } else if (address instanceof Inet6Address) {
96 ip = createIpAddress((Inet6Address)address);
101 public static IpAddress createIpAddress(Inet4Address address) {
102 return IetfInetUtil.INSTANCE.ipAddressFor(address);
105 public static IpAddress createIpAddress(Inet6Address address) {
106 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
107 return new IpAddress(ipv6);
110 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
111 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
112 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
113 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
114 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
115 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
116 return connectionInfoBuilder.build();
119 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
120 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
121 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
122 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
123 return connectionInfoBuilder.build();
126 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
127 if (ip.getIpv4Address() != null) {
128 return InetAddresses.forString(ip.getIpv4Address().getValue());
129 } else if (ip.getIpv6Address() != null) {
130 return InetAddress.getByName(ip.getIpv6Address().getValue());
132 throw new UnknownHostException("IP Address has no value");
136 public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
137 String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
138 HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
139 NodeId nodeId = new NodeId(new Uri(nodeString));
140 NodeKey nodeKey = new NodeKey(nodeId);
141 TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
142 return InstanceIdentifier.builder(NetworkTopology.class)
143 .child(Topology.class, topoKey)
144 .child(Node.class, nodeKey)
148 public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
149 PhysicalSwitch pSwitch) {
150 //TODO: Clean this up
151 return createInstanceIdentifier(client, new HwvtepNodeName(pSwitch.getName()));
154 public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
155 HwvtepNodeName psName) {
156 NodeKey nodeKey = new NodeKey(createManagedNodeId(client, psName));
157 return InstanceIdentifier.builder(NetworkTopology.class)
158 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
159 .child(Node.class, nodeKey).build();
162 public static NodeId createManagedNodeId(HwvtepConnectionInstance client, HwvtepNodeName psName) {
163 String nodeString = client.getNodeKey().getNodeId().getValue()
164 + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/" + psName.getValue();
165 NodeId nodeId = new NodeId(new Uri(nodeString));
169 public static InstanceIdentifier<LogicalSwitches> createInstanceIdentifier(HwvtepConnectionInstance client,
170 LogicalSwitch lSwitch) {
171 InstanceIdentifier<LogicalSwitches> iid = null;
172 iid = InstanceIdentifier.builder(NetworkTopology.class)
173 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
174 .child(Node.class, client.getNodeKey()).augmentation(HwvtepGlobalAugmentation.class)
175 .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))
177 /* TODO: Will this work instead to make it simpler?
178 iid = client.getInstanceIdentifier().builder()
179 .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))).build()
184 public static InstanceIdentifier<Tunnels> createInstanceIdentifier( InstanceIdentifier<Node> nodeIid,
185 InstanceIdentifier<TerminationPoint> localTpIid, InstanceIdentifier<TerminationPoint> remoteTpIid) {
187 TunnelsKey tunnelsKey = new TunnelsKey(new HwvtepPhysicalLocatorRef(localTpIid),
188 new HwvtepPhysicalLocatorRef(remoteTpIid));
189 InstanceIdentifier<Tunnels> tunnelInstanceId = nodeIid.builder().augmentation(PhysicalSwitchAugmentation.class)
190 .child(Tunnels.class, tunnelsKey).build();
191 return tunnelInstanceId;
194 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
195 Preconditions.checkNotNull(type);
196 if (type.isEmpty()) {
197 return EncapsulationTypeVxlanOverIpv4.class;
199 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper =
200 HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
201 return mapper.get(type);
205 public static InstanceIdentifier<TerminationPoint> createInstanceIdentifier(InstanceIdentifier<Node> nodeIid,
206 PhysicalLocator physicalLocator) {
207 return nodeIid.child(TerminationPoint.class, getTerminationPointKey(physicalLocator));
210 public static TerminationPointKey getTerminationPointKey(PhysicalLocator pLoc) {
211 TerminationPointKey tpKey = null;
212 if(pLoc.getEncapsulationTypeColumn().getData() != null &&
213 pLoc.getDstIpColumn().getData() != null) {
214 String tpKeyStr = pLoc.getEncapsulationTypeColumn().getData()+':'+pLoc.getDstIpColumn().getData();
215 tpKey = new TerminationPointKey(new TpId(tpKeyStr));
220 public static String getRandomUUID() {
221 return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
224 public static InstanceIdentifier<VlanBindings> createInstanceIdentifier(HwvtepConnectionInstance client,
225 InstanceIdentifier<TerminationPoint> tpPath, VlanBindings vBindings) {
226 return tpPath.augmentation(HwvtepPhysicalPortAugmentation.class) .child(VlanBindings.class,
227 new VlanBindingsKey(vBindings.getKey()));
231 public static InstanceIdentifier<Node> createInstanceIdentifier() {
232 InstanceIdentifier<Node> path = InstanceIdentifier
233 .create(NetworkTopology.class)
234 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))