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.OvsdbConnectionInfo;
23 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
26 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
27 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
28 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 import com.google.common.base.Joiner;
61 import com.google.common.base.Preconditions;
62 import com.google.common.base.Splitter;
63 import com.google.common.collect.ImmutableBiMap;
65 public class SouthboundMapper {
66 private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
68 public static Node createNode(OvsdbClient client) {
69 NodeBuilder nodeBuilder = new NodeBuilder();
70 nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
71 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
72 return nodeBuilder.build();
74 public static Node createNode(ConnectionInfo key) {
75 NodeBuilder nodeBuilder = new NodeBuilder();
76 nodeBuilder.setNodeId(createNodeId(key.getRemoteIp(),key.getRemotePort()));
77 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
78 return nodeBuilder.build();
81 public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
82 return createOvsdbAugmentation(createConnectionInfo(client));
85 public static OvsdbNodeAugmentation createOvsdbAugmentation(ConnectionInfo key) {
86 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
87 ovsdbNodeBuilder.setConnectionInfo(key);
88 return ovsdbNodeBuilder.build();
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 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
103 return new IpAddress(ipv4);
106 public static IpAddress createIpAddress(Inet6Address address) {
107 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
108 return new IpAddress(ipv6);
111 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
112 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
113 new PortNumber(client.getConnectionInfo().getRemotePort()));
116 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
117 InstanceIdentifier<Node> nodePath = InstanceIdentifier
118 .create(NetworkTopology.class)
119 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
120 .child(Node.class,new NodeKey(nodeId));
124 public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key,OvsdbBridgeName bridgeName) {
125 return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
128 public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key,Bridge bridge) {
129 String managedNodePathString = bridge
130 .getExternalIdsColumn()
132 .get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
133 InstanceIdentifier<Node> managedNodePath = null;
134 if (managedNodePathString != null) {
135 managedNodePath = (InstanceIdentifier<Node>) SouthboundUtil
136 .deserializeInstanceIdentifier(managedNodePathString);
138 if (managedNodePath == null) {
139 managedNodePath = SouthboundMapper.createInstanceIdentifier(key,new OvsdbBridgeName(bridge.getName()));
141 return managedNodePath;
144 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
145 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
146 return nodeKey.getNodeId();
149 public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key) {
150 return createInstanceIdentifier(key.getRemoteIp(), key.getRemotePort());
153 public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
154 InstanceIdentifier<Node> path = InstanceIdentifier
155 .create(NetworkTopology.class)
156 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
157 .child(Node.class,createNodeKey(ip,port));
158 LOG.debug("Created ovsdb path: {}",path);
162 public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
163 return new NodeKey(createNodeId(ip,port));
166 public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
167 return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
168 new PortNumber(connectionInfo.getRemotePort()));
171 public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName) {
172 return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
173 new PortNumber(connectionInfo.getRemotePort()),
177 public static NodeId createManagedNodeId(ConnectionInfo key, OvsdbBridgeName bridgeName) {
178 return createManagedNodeId(key.getRemoteIp(),key.getRemotePort(),bridgeName);
181 public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
182 return new NodeId(createNodeId(ip,port).getValue()
183 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue());
186 public static NodeId createNodeId(IpAddress ip, PortNumber port) {
187 String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://"
188 + new String(ip.getValue()) + ":" + port.getValue();
189 Uri uri = new Uri(uriString);
190 NodeId nodeId = new NodeId(uri);
194 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
195 if (ip.getIpv4Address() != null) {
196 return InetAddress.getByName(ip.getIpv4Address().getValue());
197 } else if (ip.getIpv6Address() != null) {
198 return InetAddress.getByName(ip.getIpv6Address().getValue());
200 throw new UnknownHostException("IP Address has no value");
204 public static DatapathId createDatapathId(Bridge bridge) {
205 Preconditions.checkNotNull(bridge);
206 if (bridge.getDatapathIdColumn() == null) {
209 return createDatapathId(bridge.getDatapathIdColumn().getData());
213 public static DatapathId createDatapathId(Set<String> dpids) {
214 Preconditions.checkNotNull(dpids);
215 if (dpids.isEmpty()) {
218 String[] dpidArray = new String[dpids.size()];
219 dpids.toArray(dpidArray);
220 return createDatapathId(dpidArray[0]);
224 public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
225 String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
227 if (mdsalbridge.getDatapathType() != null) {
228 if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
229 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
231 throw new IllegalArgumentException("Unknown datapath type "
232 + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
238 public static Class<? extends DatapathTypeBase> createDatapathType(String type) {
239 Preconditions.checkNotNull(type);
240 if (type.isEmpty()) {
241 return DatapathTypeSystem.class;
243 ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
244 SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
245 return mapper.get(type);
249 public static DatapathId createDatapathId(String dpid) {
250 Preconditions.checkNotNull(dpid);
252 if (dpid.matches("^[0-9a-fA-F]{16}")) {
253 Splitter splitter = Splitter.fixedLength(2);
254 Joiner joiner = Joiner.on(":");
255 datapath = new DatapathId(joiner.join(splitter.split(dpid)));
257 datapath = new DatapathId(dpid);
262 public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
263 Set<String> protocols = new HashSet<String>();
264 if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
265 for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
266 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
267 protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
269 throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
276 public static Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
277 Preconditions.checkNotNull(type);
278 return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
281 public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
282 Preconditions.checkNotNull(mdsaltype);
283 ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
284 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
285 return mapper.get(mdsaltype);
288 public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
289 Set<String> protocols = null;
291 protocols = bridge.getProtocolsColumn().getData();
292 } catch (SchemaVersionMismatchException e) {
293 LOG.warn("protocols not supported by this version of ovsdb", e);
295 List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
296 if (protocols != null && protocols.size() > 0) {
297 ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
298 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
299 for (String protocol : protocols) {
300 if (protocol != null && mapper.get(protocol) != null) {
301 protocolList.add(new ProtocolEntryBuilder().
302 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
309 public static List<ControllerEntry> createControllerEntries(Bridge bridge,Map<UUID,
310 Controller> updatedControllerRows) {
311 LOG.debug("Bridge: {}, updatedControllerRows: {}",bridge,updatedControllerRows);
312 Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
313 List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
314 for (UUID controllerUUID : controllerUUIDs ) {
315 Controller controller = updatedControllerRows.get(controllerUUID);
316 if (controller != null && controller.getTargetColumn() != null
317 && controller.getTargetColumn() != null) {
318 String targetString = controller.getTargetColumn().getData();
319 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid uuid =
320 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
321 .ietf.yang.types.rev130715.Uuid(controller.getUuid().toString());
322 controllerEntries.add(new ControllerEntryBuilder()
323 .setTarget(new Uri(targetString))
324 .setIsConnected(controller.getIsConnectedColumn().getData())
325 .setControllerUuid(uuid).build());
328 return controllerEntries;
331 public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
332 List<ControllerEntry> controllerEntries = omn.getControllerEntry();
333 Map<UUID,Controller> controllerMap = new HashMap<UUID,Controller>();
334 if (controllerEntries != null && !controllerEntries.isEmpty()) {
335 for (ControllerEntry controllerEntry : controllerEntries) {
336 String controllerNamedUUID = "Controller_" + getRandomUUID();
337 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
338 controller.setTarget(controllerEntry.getTarget().getValue());
339 controllerMap.put(new UUID(controllerNamedUUID), controller);
342 return controllerMap;
345 public static String getRandomUUID() {
346 return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
348 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
349 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
350 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
351 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
352 return connectionInfoBuilder.build();
355 public static ConnectionInfo createConnectionInfo(OvsdbNodeAugmentation ovsdbNodeAugmentation) {
356 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
357 connectionInfoBuilder.setRemoteIp(ovsdbNodeAugmentation.getConnectionInfo().getRemoteIp());
358 connectionInfoBuilder.setRemotePort(ovsdbNodeAugmentation.getConnectionInfo().getRemotePort());
359 return connectionInfoBuilder.build();