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