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