2 * Copyright (c) 2015 Cisco Systems, Inc. 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
8 package org.opendaylight.ovsdb.southbound;
10 import java.net.Inet4Address;
11 import java.net.Inet6Address;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
21 import org.opendaylight.ovsdb.lib.OvsdbClient;
22 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
27 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.common.base.Joiner;
56 import com.google.common.base.Preconditions;
57 import com.google.common.base.Splitter;
58 import com.google.common.collect.ImmutableBiMap;
60 public class SouthboundMapper {
61 private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
63 private static NodeId createNodeId(OvsdbConnectionInstance client) {
64 NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
65 return key.getNodeId();
69 public static IpAddress createIpAddress(InetAddress address) {
71 if (address instanceof Inet4Address) {
72 ip = createIpAddress((Inet4Address)address);
73 } else if (address instanceof Inet6Address) {
74 ip = createIpAddress((Inet6Address)address);
79 public static IpAddress createIpAddress(Inet4Address address) {
80 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
81 return new IpAddress(ipv4);
84 public static IpAddress createIpAddress(Inet6Address address) {
85 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
86 return new IpAddress(ipv6);
89 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
90 InstanceIdentifier<Node> nodePath = InstanceIdentifier
91 .create(NetworkTopology.class)
92 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
93 .child(Node.class,new NodeKey(nodeId));
97 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbConnectionInstance client,Bridge bridge) {
98 InstanceIdentifier<Node> iid;
99 if (bridge.getExternalIdsColumn() != null
100 && bridge.getExternalIdsColumn().getData() != null
101 && bridge.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
102 String iidString = bridge.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
103 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
105 String nodeString = client.getNodeKey().getNodeId().getValue()
106 + "/bridge/" + bridge.getName();
107 NodeId nodeId = new NodeId(new Uri(nodeString));
108 NodeKey nodeKey = new NodeKey(nodeId);
109 iid = InstanceIdentifier.builder(NetworkTopology.class)
110 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
111 .child(Node.class,nodeKey)
117 public static InstanceIdentifier<Node> createInstanceIdentifier(
118 OvsdbConnectionInstance client, Controller controller, String bridgeName) {
119 InstanceIdentifier<Node> iid;
120 if (controller.getExternalIdsColumn() != null
121 && controller.getExternalIdsColumn().getData() != null
122 && controller.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
123 String iidString = controller.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
124 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
126 // TODO retrieve bridge name
127 String nodeString = client.getNodeKey().getNodeId().getValue()
128 + "/bridge/" + bridgeName;
129 NodeId nodeId = new NodeId(new Uri(nodeString));
130 NodeKey nodeKey = new NodeKey(nodeId);
131 iid = InstanceIdentifier.builder(NetworkTopology.class)
132 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
133 .child(Node.class,nodeKey)
139 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
140 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
141 return nodeKey.getNodeId();
144 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
145 if (ip.getIpv4Address() != null) {
146 return InetAddress.getByName(ip.getIpv4Address().getValue());
147 } else if (ip.getIpv6Address() != null) {
148 return InetAddress.getByName(ip.getIpv6Address().getValue());
150 throw new UnknownHostException("IP Address has no value");
154 public static DatapathId createDatapathId(Bridge bridge) {
155 Preconditions.checkNotNull(bridge);
156 if (bridge.getDatapathIdColumn() == null) {
159 return createDatapathId(bridge.getDatapathIdColumn().getData());
163 public static DatapathId createDatapathId(Set<String> dpids) {
164 Preconditions.checkNotNull(dpids);
165 if (dpids.isEmpty()) {
168 String[] dpidArray = new String[dpids.size()];
169 dpids.toArray(dpidArray);
170 return createDatapathId(dpidArray[0]);
174 public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
175 String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
177 if (mdsalbridge.getDatapathType() != null) {
178 if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
179 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
181 throw new IllegalArgumentException("Unknown datapath type "
182 + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
188 public static Class<? extends DatapathTypeBase> createDatapathType(String type) {
189 Preconditions.checkNotNull(type);
190 if (type.isEmpty()) {
191 return DatapathTypeSystem.class;
193 ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
194 SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
195 return mapper.get(type);
199 public static DatapathId createDatapathId(String dpid) {
200 Preconditions.checkNotNull(dpid);
202 if (dpid.matches("^[0-9a-fA-F]{16}")) {
203 Splitter splitter = Splitter.fixedLength(2);
204 Joiner joiner = Joiner.on(":");
205 datapath = new DatapathId(joiner.join(splitter.split(dpid)));
207 datapath = new DatapathId(dpid);
212 public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
213 Set<String> protocols = new HashSet<String>();
214 if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
215 for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
216 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
217 protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
219 throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
226 public static Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
227 Preconditions.checkNotNull(type);
228 return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
231 public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
232 Preconditions.checkNotNull(mdsaltype);
233 ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
234 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
235 return mapper.get(mdsaltype);
238 public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
239 Set<String> protocols = null;
241 protocols = bridge.getProtocolsColumn().getData();
242 } catch (SchemaVersionMismatchException e) {
243 LOG.warn("protocols not supported by this version of ovsdb", e);
245 List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
246 if (protocols != null && protocols.size() > 0) {
247 ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
248 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
249 for (String protocol : protocols) {
250 if (protocol != null && mapper.get(protocol) != null) {
251 protocolList.add(new ProtocolEntryBuilder().
252 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
260 * Create the {@link ControllerEntry} list given an OVSDB {@link Bridge}
261 * and {@link Controller} rows.
263 * @param bridge the {@link Bridge} to update
264 * @param updatedControllerRows the list of {@link Controller} controllers with updates
265 * @return list of {@link ControllerEntry} entries
267 public static List<ControllerEntry> createControllerEntries(Bridge bridge,
268 Map<UUID, Controller> updatedControllerRows) {
270 LOG.debug("createControllerEntries Bridge: {}\n, updatedControllerRows: {}",
271 bridge, updatedControllerRows);
272 final Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
273 final List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
274 for (UUID controllerUUID : controllerUUIDs ) {
275 final Controller controller = updatedControllerRows.get(controllerUUID);
276 addControllerEntries(controllerEntries, controller);
278 LOG.debug("controllerEntries: {}", controllerEntries);
279 return controllerEntries;
283 * Create the {@link ControllerEntry} list given an MDSAL {@link Node} bridge
284 * and {@link Controller} rows.
286 * @param bridgeNode the {@link Node} to update
287 * @param updatedControllerRows the list of {@link Controller} controllers with updates
288 * @return list of {@link ControllerEntry} entries
290 public static List<ControllerEntry> createControllerEntries(Node bridgeNode,
291 Map<UUID, Controller> updatedControllerRows) {
293 LOG.debug("createControllerEntries Bridge 2: {}\n, updatedControllerRows: {}",
294 bridgeNode, updatedControllerRows);
295 final List<ControllerEntry> controllerEntriesCreated = new ArrayList<ControllerEntry>();
296 final OvsdbBridgeAugmentation ovsdbBridgeAugmentation =
297 bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
298 if (ovsdbBridgeAugmentation == null) {
299 return controllerEntriesCreated;
302 final List<ControllerEntry> controllerEntries = ovsdbBridgeAugmentation.getControllerEntry();
303 if (controllerEntries != null) {
304 for (ControllerEntry controllerEntry : controllerEntries) {
305 final Controller controller = updatedControllerRows.get(
306 new UUID(controllerEntry.getControllerUuid().getValue()));
307 addControllerEntries(controllerEntriesCreated, controller);
310 LOG.debug("controllerEntries: {}", controllerEntriesCreated);
311 return controllerEntriesCreated;
315 * Add the OVSDB {@link Controller} updates to the MDSAL {@link ControllerEntry} list.
317 * @param controllerEntries the list of {@link ControllerEntry} to update
318 * @param controller the updated OVSDB {@link Controller}
320 public static void addControllerEntries(List<ControllerEntry> controllerEntries,
321 final Controller controller) {
323 if (controller != null && controller.getTargetColumn() != null) {
324 final String targetString = controller.getTargetColumn().getData();
325 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid uuid =
326 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
327 .ietf.yang.types.rev130715.Uuid(controller.getUuid().toString());
329 controllerEntries.add(new ControllerEntryBuilder()
330 .setTarget(new Uri(targetString))
331 .setIsConnected(controller.getIsConnectedColumn().getData())
332 .setControllerUuid(uuid).build());
336 public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
337 List<ControllerEntry> controllerEntries = omn.getControllerEntry();
338 Map<UUID,Controller> controllerMap = new HashMap<UUID,Controller>();
339 if (controllerEntries != null && !controllerEntries.isEmpty()) {
340 for (ControllerEntry controllerEntry : controllerEntries) {
341 String controllerNamedUUID = "Controller_" + getRandomUUID();
342 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
343 controller.setTarget(controllerEntry.getTarget().getValue());
344 controllerMap.put(new UUID(controllerNamedUUID), controller);
347 return controllerMap;
350 public static String getRandomUUID() {
351 return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
353 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
354 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
355 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
356 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
357 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
358 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
359 return connectionInfoBuilder.build();
362 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
363 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
364 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
365 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
366 return connectionInfoBuilder.build();