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.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.InterfaceTypeBase;
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.OvsdbBridgeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.google.common.base.Joiner;
59 import com.google.common.base.Preconditions;
60 import com.google.common.base.Splitter;
61 import com.google.common.collect.ImmutableBiMap;
63 public class SouthboundMapper {
64 private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
66 public static Node createNode(OvsdbClient client) {
67 NodeBuilder nodeBuilder = new NodeBuilder();
68 nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
69 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
70 return nodeBuilder.build();
72 public static Node createNode(OvsdbClientKey key) {
73 NodeBuilder nodeBuilder = new NodeBuilder();
74 nodeBuilder.setNodeId(createNodeId(key.getIp(),key.getPort()));
75 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
76 return nodeBuilder.build();
79 public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
80 return createOvsdbAugmentation(new OvsdbClientKey(client));
83 public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClientKey key) {
84 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
85 ovsdbNodeBuilder.setIp(key.getIp());
86 ovsdbNodeBuilder.setPort(key.getPort());
87 return ovsdbNodeBuilder.build();
90 public static IpAddress createIpAddress(InetAddress address) {
92 if(address instanceof Inet4Address) {
93 ip = createIpAddress((Inet4Address)address);
94 } else if (address instanceof Inet6Address) {
95 ip = createIpAddress((Inet6Address)address);
100 public static IpAddress createIpAddress(Inet4Address address) {
101 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
102 return new IpAddress(ipv4);
105 public static IpAddress createIpAddress(Inet6Address address) {
106 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
107 return new IpAddress(ipv6);
110 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
111 return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
112 new PortNumber(client.getConnectionInfo().getRemotePort()));
115 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
116 InstanceIdentifier<Node> nodePath = InstanceIdentifier
117 .create(NetworkTopology.class)
118 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
119 .child(Node.class,new NodeKey(nodeId));
123 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,OvsdbBridgeName bridgeName) {
124 return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
127 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key) {
128 return createInstanceIdentifier(key.getIp(), key.getPort());
131 public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
132 InstanceIdentifier<Node> path = InstanceIdentifier
133 .create(NetworkTopology.class)
134 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
135 .child(Node.class,createNodeKey(ip,port));
136 LOG.info("Created ovsdb path: {}",path);
140 public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
141 return new NodeKey(createNodeId(ip,port));
144 public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
145 return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
146 new PortNumber(connectionInfo.getRemotePort()));
149 public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName) {
150 return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
151 new PortNumber(connectionInfo.getRemotePort()),
155 public static NodeId createManagedNodeId(OvsdbClientKey key, OvsdbBridgeName bridgeName) {
156 return createManagedNodeId(key.getIp(),key.getPort(),bridgeName);
159 public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
160 return new NodeId(createNodeId(ip,port).getValue()
161 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+"/"+ bridgeName.getValue());
164 public static NodeId createNodeId(IpAddress ip, PortNumber port) {
165 String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + new String(ip.getValue()) +
166 ":" + port.getValue();
167 Uri uri = new Uri(uriString);
168 NodeId nodeId = new NodeId(uri);
172 public static TpId createTerminationPointId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName, String tpName) {
173 return createTerminationPointId(createIpAddress(connectionInfo.getRemoteAddress()),
174 new PortNumber(connectionInfo.getRemotePort()),
178 public static TpId createTerminationPointId(OvsdbClientKey key, OvsdbBridgeName bridgeName, String tpName) {
179 return createTerminationPointId(key.getIp(),key.getPort(),bridgeName, tpName);
182 public static TpId createTerminationPointId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName, String tpName) {
183 return new TpId(createNodeId(ip,port).getValue()
184 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+"/"+ bridgeName.getValue()
185 + "/"+SouthboundConstants.TP_URI_PREFIX+"/"+ tpName);
188 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
189 if(ip.getIpv4Address() != null) {
190 return InetAddress.getByName(ip.getIpv4Address().getValue());
191 } else if(ip.getIpv6Address() != null) {
192 return InetAddress.getByName(ip.getIpv6Address().getValue());
194 throw new UnknownHostException("IP Address has no value");
198 public static DatapathId createDatapathId(Bridge bridge) {
199 Preconditions.checkNotNull(bridge);
200 if(bridge.getDatapathIdColumn() == null) {
203 return createDatapathId(bridge.getDatapathIdColumn().getData());
207 public static DatapathId createDatapathId(Set<String> dpids) {
208 Preconditions.checkNotNull(dpids);
209 if(dpids.isEmpty()) {
212 String[] dpidArray = new String[dpids.size()];
213 dpids.toArray(dpidArray);
214 return createDatapathId(dpidArray[0]);
218 public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
219 String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
221 if (mdsalbridge.getDatapathType() != null) {
222 if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
223 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
225 throw new IllegalArgumentException("Unknown datapath type " + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
231 public static Class<? extends DatapathTypeBase> createDatapathType(String type) {
232 Preconditions.checkNotNull(type);
233 if (type.isEmpty()) {
234 return DatapathTypeSystem.class;
236 ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper = SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
237 return mapper.get(type);
241 public static DatapathId createDatapathId(String dpid) {
242 Preconditions.checkNotNull(dpid);
244 if(dpid.matches("^[0-9a-fA-F]{16}")) {
245 Splitter splitter = Splitter.fixedLength(2);
246 Joiner joiner = Joiner.on(":");
247 datapath = new DatapathId(joiner.join(splitter.split(dpid)));
249 datapath = new DatapathId(dpid);
254 public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
255 Set<String> protocols = new HashSet<String>();
256 if(ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
257 for(ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
258 if(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
259 protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
261 throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
268 public static Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
269 Preconditions.checkNotNull(type);
270 return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
273 public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
274 Preconditions.checkNotNull(mdsaltype);
275 ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper = SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
276 return mapper.get(mdsaltype);
279 public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
280 Set<String> protocols = bridge.getProtocolsColumn().getData();
281 List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
282 if(protocols != null && protocols.size() >0) {
283 ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper = SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
284 for(String protocol : protocols) {
285 if(protocol != null && mapper.get(protocol) != null) {
286 protocolList.add(new ProtocolEntryBuilder().setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
293 public static List<ControllerEntry> createControllerEntries(Bridge bridge,Map<UUID,Controller> updatedControllerRows) {
294 LOG.debug("Bridge: {}, updatedControllerRows: {}",bridge,updatedControllerRows);
295 Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
296 List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
297 for(UUID controllerUUID : controllerUUIDs ) {
298 Controller controller = updatedControllerRows.get(controllerUUID);
299 if(controller != null && controller.getTargetColumn() != null
300 && controller.getTargetColumn() != null) {
301 String targetString = controller.getTargetColumn().getData();
302 controllerEntries.add(new ControllerEntryBuilder().setTarget(new Uri(targetString)).build());
305 return controllerEntries;
308 public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
309 List<ControllerEntry> controllerEntries = omn.getControllerEntry();
310 Map<UUID,Controller> controllerMap = new HashMap<UUID,Controller>();
311 if(controllerEntries != null && !controllerEntries.isEmpty()) {
313 for(ControllerEntry controllerEntry : controllerEntries) {
314 String controllerNamedUUID = "Controller_" + omn.getBridgeName().getValue() + index++;
315 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
316 controller.setTarget(controllerEntry.getTarget().getValue());
317 controllerMap.put(new UUID(controllerNamedUUID), controller);
320 return controllerMap;