Upgrade ietf-{inet,yang}-types to 2013-07-15
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / SouthboundMapper.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound;
9
10 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
11
12 import com.google.common.base.Joiner;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.net.InetAddresses;
17 import java.net.Inet4Address;
18 import java.net.Inet6Address;
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import org.opendaylight.ovsdb.lib.OvsdbClient;
28 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
29 import org.opendaylight.ovsdb.lib.notation.UUID;
30 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
31 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
32 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
33 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
34 import org.opendaylight.ovsdb.schema.openvswitch.Manager;
35 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
36 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.QosTypeBase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntryBuilder;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 public class SouthboundMapper {
69
70     private SouthboundMapper() { }
71
72     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
73     private static final String N_CONNECTIONS_STR = "n_connections";
74
75     public static IpAddress createIpAddress(InetAddress address) {
76         IpAddress ip = null;
77         if (address instanceof Inet4Address) {
78             ip = createIpAddress((Inet4Address)address);
79         } else if (address instanceof Inet6Address) {
80             ip = createIpAddress((Inet6Address)address);
81         }
82         return ip;
83     }
84
85     public static IpAddress createIpAddress(Inet4Address address) {
86         return IetfInetUtil.INSTANCE.ipAddressFor(address);
87     }
88
89     public static IpAddress createIpAddress(Inet6Address address) {
90         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
91         return new IpAddress(ipv6);
92     }
93
94     public static InstanceIdentifier<Topology> createTopologyInstanceIdentifier() {
95         return InstanceIdentifier
96                 .create(NetworkTopology.class)
97                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
98     }
99
100     public static InstanceIdentifier<OvsdbBridgeAugmentation>
101         createBridgeInstanceIdentifier(OvsdbConnectionInstance client, String bridgeName) {
102         return createInstanceIdentifier(client, bridgeName).augmentation(OvsdbBridgeAugmentation.class);
103     }
104
105     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
106         return createTopologyInstanceIdentifier()
107                 .child(Node.class,new NodeKey(nodeId));
108     }
109
110     @SuppressWarnings("unchecked")
111     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbConnectionInstance client,Bridge bridge) {
112         InstanceIdentifier<Node> iid;
113         if (bridge.getExternalIdsColumn() != null
114                 && bridge.getExternalIdsColumn().getData() != null
115                 && bridge.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
116             String iidString = bridge.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
117             iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
118         } else {
119             iid = createInstanceIdentifier(client, bridge.getName());
120         }
121         return iid;
122     }
123
124     @SuppressWarnings("unchecked")
125     public static InstanceIdentifier<Node> createInstanceIdentifier(
126             OvsdbConnectionInstance client, Controller controller, String bridgeName) {
127         InstanceIdentifier<Node> iid;
128         if (controller.getExternalIdsColumn() != null
129                 && controller.getExternalIdsColumn().getData() != null
130                 && controller.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
131             String iidString = controller.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
132             iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
133         } else {
134             iid = createInstanceIdentifier(client, bridgeName);
135         }
136         return iid;
137     }
138
139     public static InstanceIdentifier<Node> createInstanceIdentifier(
140             OvsdbConnectionInstance client, String bridgeName) {
141         String nodeString = client.getNodeKey().getNodeId().getValue()
142                 + "/bridge/" + bridgeName;
143         NodeId nodeId = new NodeId(new Uri(nodeString));
144         return createInstanceIdentifier(nodeId);
145
146     }
147
148     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
149         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
150         return nodeKey.getNodeId();
151     }
152
153     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
154         if (ip.getIpv4Address() != null) {
155             return InetAddresses.forString(ip.getIpv4Address().getValue());
156         } else if (ip.getIpv6Address() != null) {
157             return InetAddress.getByName(ip.getIpv6Address().getValue());
158         } else {
159             throw new UnknownHostException("IP Address has no value");
160         }
161     }
162
163     public static DatapathId createDatapathId(Bridge bridge) {
164         Preconditions.checkNotNull(bridge);
165         if (bridge.getDatapathIdColumn() == null) {
166             return null;
167         } else {
168             return createDatapathId(bridge.getDatapathIdColumn().getData());
169         }
170     }
171
172     public static DatapathId createDatapathId(Set<String> dpids) {
173         Preconditions.checkNotNull(dpids);
174         if (dpids.isEmpty()) {
175             return null;
176         } else {
177             String[] dpidArray = new String[dpids.size()];
178             dpids.toArray(dpidArray);
179             return createDatapathId(dpidArray[0]);
180         }
181     }
182
183     public static DatapathId createDatapathId(String dpid) {
184         Preconditions.checkNotNull(dpid);
185         DatapathId datapath;
186         if (dpid.matches("^[0-9a-fA-F]{16}")) {
187             Splitter splitter = Splitter.fixedLength(2);
188             Joiner joiner = Joiner.on(":");
189             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
190         } else {
191             datapath = new DatapathId(dpid);
192         }
193         return datapath;
194     }
195
196     public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
197         String datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class);
198
199         if (mdsalbridge.getDatapathType() != null && !mdsalbridge.getDatapathType().equals(DatapathTypeBase.class)) {
200             datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
201             if (datapathtype == null) {
202                 throw new IllegalArgumentException("Unknown datapath type " + mdsalbridge.getDatapathType().getName());
203             }
204         }
205         return datapathtype;
206     }
207
208     public static  Class<? extends DatapathTypeBase> createDatapathType(String type) {
209         Preconditions.checkNotNull(type);
210         if (type.isEmpty()) {
211             return DatapathTypeSystem.class;
212         } else {
213             ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
214                     SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
215             return mapper.get(type);
216         }
217     }
218
219     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
220         Set<String> protocols = new HashSet<>();
221         if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
222             for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
223                 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
224                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
225                 } else {
226                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
227                 }
228             }
229         }
230         return protocols;
231     }
232
233     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
234         Preconditions.checkNotNull(type);
235         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
236     }
237
238     public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
239         Preconditions.checkNotNull(mdsaltype);
240         ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
241                 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
242         return mapper.get(mdsaltype);
243     }
244
245     public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
246         Set<String> protocols = null;
247         try {
248             protocols = bridge.getProtocolsColumn().getData();
249         } catch (SchemaVersionMismatchException e) {
250             schemaMismatchLog("protocols", "Bridge", e);
251         }
252         List<ProtocolEntry> protocolList = new ArrayList<>();
253         if (protocols != null && protocols.size() > 0) {
254             ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
255                     SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
256             for (String protocol : protocols) {
257                 if (protocol != null && mapper.get(protocol) != null) {
258                     protocolList.add(new ProtocolEntryBuilder().setProtocol(mapper.get(protocol)).build());
259                 }
260             }
261         }
262         return protocolList;
263     }
264
265     /**
266      * Create the {@link ControllerEntry} list given an OVSDB {@link Bridge}
267      * and {@link Controller} rows.
268      *
269      * @param bridge the {@link Bridge} to update
270      * @param updatedControllerRows the list of {@link Controller} controllers with updates
271      * @return list of {@link ControllerEntry} entries
272      */
273     public static List<ControllerEntry> createControllerEntries(Bridge bridge,
274                                                                 Map<UUID, Controller> updatedControllerRows) {
275
276         LOG.debug("createControllerEntries Bridge: {}\n, updatedControllerRows: {}",
277                 bridge, updatedControllerRows);
278         final Set<UUID> controllerUuids = bridge.getControllerColumn().getData();
279         final List<ControllerEntry> controllerEntries = new ArrayList<>();
280         for (UUID controllerUuid : controllerUuids ) {
281             final Controller controller = updatedControllerRows.get(controllerUuid);
282             addControllerEntries(controllerEntries, controller);
283         }
284         LOG.debug("controllerEntries: {}", controllerEntries);
285         return controllerEntries;
286     }
287
288     /**
289      * Create the {@link ControllerEntry} list given an MDSAL {@link Node} bridge
290      * and {@link Controller} rows.
291      *
292      * @param bridgeNode the {@link Node} to update
293      * @param updatedControllerRows the list of {@link Controller} controllers with updates
294      * @return list of {@link ControllerEntry} entries
295      */
296     public static List<ControllerEntry> createControllerEntries(Node bridgeNode,
297                                                                 Map<UUID, Controller> updatedControllerRows) {
298
299         LOG.debug("createControllerEntries Bridge 2: {}\n, updatedControllerRows: {}",
300                 bridgeNode, updatedControllerRows);
301         final List<ControllerEntry> controllerEntriesCreated = new ArrayList<>();
302         final OvsdbBridgeAugmentation ovsdbBridgeAugmentation =
303                 bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
304         if (ovsdbBridgeAugmentation == null) {
305             return controllerEntriesCreated;
306         }
307
308         final List<ControllerEntry> controllerEntries = ovsdbBridgeAugmentation.getControllerEntry();
309         if (controllerEntries != null) {
310             for (ControllerEntry controllerEntry : controllerEntries) {
311                 final Controller controller = updatedControllerRows.get(
312                         new UUID(controllerEntry.getControllerUuid().getValue()));
313                 addControllerEntries(controllerEntriesCreated, controller);
314             }
315         }
316         LOG.debug("controllerEntries: {}", controllerEntriesCreated);
317         return controllerEntriesCreated;
318     }
319
320     /**
321      * Add the OVSDB {@link Controller} updates to the MDSAL {@link ControllerEntry} list.
322      *
323      * @param controllerEntries the list of {@link ControllerEntry} to update
324      * @param controller the updated OVSDB {@link Controller}
325      */
326     public static void addControllerEntries(List<ControllerEntry> controllerEntries,
327                                             final Controller controller) {
328
329         if (controller != null && controller.getTargetColumn() != null) {
330             final String targetString = controller.getTargetColumn().getData();
331             final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid uuid =
332                     new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
333                             .ietf.yang.types.rev130715.Uuid(controller.getUuid().toString());
334
335             controllerEntries.add(new ControllerEntryBuilder()
336                     .setTarget(new Uri(targetString))
337                     .setIsConnected(controller.getIsConnectedColumn().getData())
338                     .setControllerUuid(uuid).build());
339         }
340     }
341
342     public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
343         List<ControllerEntry> controllerEntries = omn.getControllerEntry();
344         Map<UUID,Controller> controllerMap = new HashMap<>();
345         if (controllerEntries != null && !controllerEntries.isEmpty()) {
346             for (ControllerEntry controllerEntry : controllerEntries) {
347                 String controllerNamedUuid = "Controller_" + getRandomUuid();
348                 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
349                 controller.setTarget(controllerEntry.getTarget().getValue());
350                 controllerMap.put(new UUID(controllerNamedUuid), controller);
351             }
352         }
353         return controllerMap;
354     }
355
356     public static String getRandomUuid() {
357         return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
358     }
359
360     public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
361         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
362         connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
363         connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
364         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
365         connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
366         return connectionInfoBuilder.build();
367     }
368
369     public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
370         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
371         connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
372         connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
373         return connectionInfoBuilder.build();
374     }
375
376     /**
377      * Create the {@link ManagerEntry} list given an OVSDB {@link OpenVSwitch}
378      * and {@link Manager} rows.
379      *
380      * @param ovsdbNode the {@link OpenVSwitch} to update
381      * @param updatedManagerRows the list of {@link Manager} managers with updates
382      * @return list of {@link ManagerEntry} entries
383      */
384     public static List<ManagerEntry> createManagerEntries(OpenVSwitch ovsdbNode,
385                                                                 Map<UUID, Manager> updatedManagerRows) {
386
387         LOG.debug("createManagerEntries OpenVSwitch: {}\n, updatedManagerRows: {}",
388                 ovsdbNode, updatedManagerRows);
389         final Set<UUID> managerUuids = ovsdbNode.getManagerOptionsColumn().getData();
390         final List<ManagerEntry> managerEntries = new ArrayList<>();
391         for (UUID managerUuid : managerUuids) {
392             final Manager manager = updatedManagerRows.get(managerUuid);
393             addManagerEntries(managerEntries, manager);
394         }
395         LOG.debug("managerEntries: {}", managerEntries);
396         return managerEntries;
397     }
398
399     /**
400      * Create the {@link ManagerEntry} list given an MDSAL {@link Node} ovsdbNode
401      * and {@link Manager} rows.
402      *
403      * @param ovsdbNode the {@link Node} to update
404      * @param updatedManagerRows the list of {@link Manager} managers with updates
405      * @return list of {@link ManagerEntry} entries
406      */
407     public static List<ManagerEntry> createManagerEntries(Node ovsdbNode,
408                                                                 Map<Uri, Manager> updatedManagerRows) {
409
410         LOG.debug("createManagerEntries based on OVSDB Node: {}\n, updatedManagerRows: {}",
411                 ovsdbNode, updatedManagerRows);
412         final List<ManagerEntry> managerEntriesCreated = new ArrayList<>();
413         final OvsdbNodeAugmentation ovsdbNodeAugmentation =
414                 ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
415         if (ovsdbNodeAugmentation == null) {
416             return managerEntriesCreated;
417         }
418
419         final List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
420         if (managerEntries != null) {
421             for (ManagerEntry managerEntry : managerEntries) {
422                 final Manager manager = updatedManagerRows.get(managerEntry.getTarget());
423                 addManagerEntries(managerEntriesCreated, manager);
424             }
425         }
426         LOG.debug("managerEntries: {}", managerEntriesCreated);
427         return managerEntriesCreated;
428     }
429
430     /**
431      * Add the OVSDB {@link Manager} updates to the MDSAL {@link ManagerEntry} list.
432      *
433      * @param managerEntries the list of {@link ManagerEntry} to update
434      * @param manager the updated OVSDB {@link Manager}
435      */
436     public static void addManagerEntries(List<ManagerEntry> managerEntries,
437                                             final Manager manager) {
438
439         if (manager != null && manager.getTargetColumn() != null) {
440             long numberOfConnections = 0;
441             final String targetString = manager.getTargetColumn().getData();
442
443             final Map<String, String> statusAttributeMap = manager.getStatusColumn().getData();
444             if (statusAttributeMap.containsKey(N_CONNECTIONS_STR)) {
445                 String numberOfConnectionValueStr = statusAttributeMap.get(N_CONNECTIONS_STR);
446                 numberOfConnections = Integer.parseInt(numberOfConnectionValueStr);
447             } else {
448                 final boolean isConnected = manager.getIsConnectedColumn().getData();
449                 if (isConnected) {
450                     numberOfConnections = 1;
451                 }
452             }
453             managerEntries.add(new ManagerEntryBuilder()
454                     .setTarget(new Uri(targetString))
455                     .setNumberOfConnections(numberOfConnections)
456                     .setConnected(manager.getIsConnectedColumn().getData()).build());
457         }
458     }
459
460     /**
461      * Return the MD-SAL QoS type class corresponding to the QoS type {@link Qos}.
462      *
463      * @param type the QoS type to match {@link String}
464      * @return class matching the input QoS type {@link QosTypeBase}
465      */
466     public static  Class<? extends QosTypeBase> createQosType(String type) {
467         Preconditions.checkNotNull(type);
468         if (type.isEmpty()) {
469             LOG.info("QoS type not supplied");
470             return QosTypeBase.class;
471         } else {
472             ImmutableBiMap<String, Class<? extends QosTypeBase>> mapper =
473                     SouthboundConstants.QOS_TYPE_MAP.inverse();
474             if (mapper.get(type) == null) {
475                 LOG.info("QoS type not found in model: {}", type);
476                 return QosTypeBase.class;
477             } else {
478                 return mapper.get(type);
479             }
480         }
481     }
482
483     public static String createQosType(Class<? extends QosTypeBase> qosTypeClass) {
484         String qosType = SouthboundConstants.QOS_TYPE_MAP.get(QosTypeBase.class);
485
486         if (qosTypeClass != null && !qosTypeClass.equals(QosTypeBase.class)) {
487             qosType = SouthboundConstants.QOS_TYPE_MAP.get(qosTypeClass);
488             if (qosType == null) {
489                 throw new IllegalArgumentException("Unknown QoS type" + qosTypeClass.getName());
490             }
491         }
492         return qosType;
493     }
494
495
496     public static InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
497         if (ovs.getExternalIdsColumn() != null
498                 && ovs.getExternalIdsColumn().getData() != null
499                 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
500             String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
501             return (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
502         } else {
503             String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
504                     + ovs.getUuid().toString();
505             NodeId nodeId = new NodeId(new Uri(nodeString));
506             NodeKey nodeKey = new NodeKey(nodeId);
507             return InstanceIdentifier.builder(NetworkTopology.class)
508                     .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
509                     .child(Node.class,nodeKey)
510                     .build();
511         }
512     }
513 }