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.IetfInetUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.common.base.Preconditions;
52 import com.google.common.collect.ImmutableBiMap;
53 import com.google.common.net.InetAddresses;
55 public class HwvtepSouthboundMapper {
56 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
57 private static final String N_CONNECTIONS_STR = "n_connections";
59 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
60 return InstanceIdentifier
61 .create(NetworkTopology.class)
62 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
63 .child(Node.class,new NodeKey(nodeId));
66 public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
67 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
68 new PortNumber(client.getConnectionInfo().getRemotePort()));
71 private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
72 String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
73 + String.valueOf(ip.getValue()) + ":" + port.getValue();
74 Uri uri = new Uri(uriString);
75 NodeId nodeId = new NodeId(uri);
76 InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
77 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
78 .child(Node.class,new NodeKey(nodeId));
79 LOG.debug("Created ovsdb path: {}",path);
83 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
84 NodeKey nodeKey = iid.firstKeyOf(Node.class);
85 return nodeKey.getNodeId();
88 public static IpAddress createIpAddress(InetAddress address) {
90 if (address instanceof Inet4Address) {
91 ip = createIpAddress((Inet4Address)address);
92 } else if (address instanceof Inet6Address) {
93 ip = createIpAddress((Inet6Address)address);
98 public static IpAddress createIpAddress(Inet4Address address) {
99 return IetfInetUtil.INSTANCE.ipAddressFor(address);
102 public static IpAddress createIpAddress(Inet6Address address) {
103 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
104 return new IpAddress(ipv6);
107 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
108 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
109 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
110 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
111 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
112 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
113 return connectionInfoBuilder.build();
116 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
117 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
118 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
119 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
120 return connectionInfoBuilder.build();
123 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
124 if (ip.getIpv4Address() != null) {
125 return InetAddresses.forString(ip.getIpv4Address().getValue());
126 } else if (ip.getIpv6Address() != null) {
127 return InetAddress.getByName(ip.getIpv6Address().getValue());
129 throw new UnknownHostException("IP Address has no value");
133 public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
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 return InstanceIdentifier.builder(NetworkTopology.class)
140 .child(Topology.class, topoKey)
141 .child(Node.class, nodeKey)
145 public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
146 PhysicalSwitch pSwitch) {
147 //TODO: Clean this up
148 return createInstanceIdentifier(client, new HwvtepNodeName(pSwitch.getName()));
151 public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
152 HwvtepNodeName psName) {
153 NodeKey nodeKey = new NodeKey(createManagedNodeId(client, psName));
154 return InstanceIdentifier.builder(NetworkTopology.class)
155 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
156 .child(Node.class, nodeKey).build();
159 public static NodeId createManagedNodeId(HwvtepConnectionInstance client, HwvtepNodeName psName) {
160 String nodeString = client.getNodeKey().getNodeId().getValue()
161 + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/" + psName.getValue();
162 NodeId nodeId = new NodeId(new Uri(nodeString));
166 public static InstanceIdentifier<LogicalSwitches> createInstanceIdentifier(HwvtepConnectionInstance client,
167 LogicalSwitch lSwitch) {
168 InstanceIdentifier<LogicalSwitches> iid = null;
169 iid = InstanceIdentifier.builder(NetworkTopology.class)
170 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
171 .child(Node.class, client.getNodeKey()).augmentation(HwvtepGlobalAugmentation.class)
172 .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))
174 /* TODO: Will this work instead to make it simpler?
175 iid = client.getInstanceIdentifier().builder()
176 .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))).build()
181 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
182 Preconditions.checkNotNull(type);
183 if (type.isEmpty()) {
184 return EncapsulationTypeVxlanOverIpv4.class;
186 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper =
187 HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
188 return mapper.get(type);
192 public static InstanceIdentifier<TerminationPoint> createInstanceIdentifier(InstanceIdentifier<Node> nodeIid,
193 PhysicalLocator physicalLocator) {
194 return nodeIid.child(TerminationPoint.class, getTerminationPointKey(physicalLocator));
197 public static TerminationPointKey getTerminationPointKey(PhysicalLocator pLoc) {
198 TerminationPointKey tpKey = null;
199 if(pLoc.getEncapsulationTypeColumn().getData() != null &&
200 pLoc.getDstIpColumn().getData() != null) {
201 String tpKeyStr = pLoc.getEncapsulationTypeColumn().getData()+':'+pLoc.getDstIpColumn().getData();
202 tpKey = new TerminationPointKey(new TpId(tpKeyStr));
207 public static String getRandomUUID() {
208 return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
211 public static InstanceIdentifier<VlanBindings> createInstanceIdentifier(HwvtepConnectionInstance client,
212 InstanceIdentifier<TerminationPoint> tpPath, VlanBindings vBindings) {
213 return tpPath.augmentation(HwvtepPhysicalPortAugmentation.class) .child(VlanBindings.class,
214 new VlanBindingsKey(vBindings.getKey()));