Migrate to use Objects.requireNonNull
[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 java.util.Objects.requireNonNull;
11 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
12
13 import com.google.common.base.Joiner;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Splitter;
16 import com.google.common.collect.ImmutableBiMap;
17 import com.google.common.net.InetAddresses;
18 import java.net.Inet4Address;
19 import java.net.Inet6Address;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import org.opendaylight.ovsdb.lib.OvsdbClient;
29 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
30 import org.opendaylight.ovsdb.lib.notation.UUID;
31 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
32 import org.opendaylight.ovsdb.lib.schema.typed.TypedDatabaseSchema;
33 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
34 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
35 import org.opendaylight.ovsdb.schema.openvswitch.Manager;
36 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
37 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.QosTypeBase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntryBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntryKey;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
71 import org.opendaylight.yangtools.yang.binding.DataObject;
72 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
73 import org.opendaylight.yangtools.yang.common.Uint16;
74 import org.opendaylight.yangtools.yang.common.Uint32;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78 public final class SouthboundMapper {
79     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
80     private static final String N_CONNECTIONS_STR = "n_connections";
81
82     private SouthboundMapper() {
83
84     }
85
86     public static IpAddress createIpAddress(final InetAddress address) {
87         IpAddress ip = null;
88         if (address instanceof Inet4Address) {
89             ip = createIpAddress((Inet4Address)address);
90         } else if (address instanceof Inet6Address) {
91             ip = createIpAddress((Inet6Address)address);
92         }
93         return ip;
94     }
95
96     public static IpAddress createIpAddress(final Inet4Address address) {
97         return IetfInetUtil.INSTANCE.ipAddressFor(address);
98     }
99
100     public static IpAddress createIpAddress(final Inet6Address address) {
101         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
102         return new IpAddress(ipv6);
103     }
104
105     public static InstanceIdentifier<Topology> createTopologyInstanceIdentifier() {
106         return InstanceIdentifier
107                 .create(NetworkTopology.class)
108                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
109     }
110
111     public static InstanceIdentifier<Node> createInstanceIdentifier(final NodeId nodeId) {
112         return createTopologyInstanceIdentifier()
113                 .child(Node.class,new NodeKey(nodeId));
114     }
115
116     @SuppressWarnings("unchecked")
117     public static InstanceIdentifier<Node> createInstanceIdentifier(
118             final InstanceIdentifierCodec instanceIdentifierCodec, final OvsdbConnectionInstance client,
119             final Bridge bridge) {
120         InstanceIdentifier<Node> iid;
121         if (bridge.getExternalIdsColumn() != null
122                 && bridge.getExternalIdsColumn().getData() != null
123                 && bridge.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
124             String iidString = bridge.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
125             iid = (InstanceIdentifier<Node>) instanceIdentifierCodec.bindingDeserializerOrNull(iidString);
126         } else {
127             iid = createInstanceIdentifier(client, bridge.getName());
128         }
129         return iid;
130     }
131
132     @SuppressWarnings("unchecked")
133     public static InstanceIdentifier<Node> createInstanceIdentifier(
134             final InstanceIdentifierCodec instanceIdentifierCodec, final OvsdbConnectionInstance client,
135             final Controller controller, final String bridgeName) {
136         InstanceIdentifier<Node> iid;
137         if (controller.getExternalIdsColumn() != null
138                 && controller.getExternalIdsColumn().getData() != null
139                 && controller.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
140             String iidString = controller.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
141             iid = (InstanceIdentifier<Node>) instanceIdentifierCodec.bindingDeserializerOrNull(iidString);
142         } else {
143             iid = createInstanceIdentifier(client, bridgeName);
144         }
145         return iid;
146     }
147
148     public static InstanceIdentifier<Node> createInstanceIdentifier(
149             final OvsdbConnectionInstance client, final String bridgeName) {
150         String nodeString = client.getNodeKey().getNodeId().getValue()
151                 + "/bridge/" + bridgeName;
152         NodeId nodeId = new NodeId(new Uri(nodeString));
153         return createInstanceIdentifier(nodeId);
154
155     }
156
157     public static NodeId createManagedNodeId(final InstanceIdentifier<Node> iid) {
158         NodeKey nodeKey = iid.firstKeyOf(Node.class);
159         return nodeKey.getNodeId();
160     }
161
162     public static InetAddress createInetAddress(final IpAddress ip) throws UnknownHostException {
163         if (ip.getIpv4Address() != null) {
164             return InetAddresses.forString(ip.getIpv4Address().getValue());
165         } else if (ip.getIpv6Address() != null) {
166             return InetAddress.getByName(ip.getIpv6Address().getValue());
167         } else {
168             throw new UnknownHostException("IP Address has no value");
169         }
170     }
171
172     public static DatapathId createDatapathId(final Bridge bridge) {
173         requireNonNull(bridge);
174         if (bridge.getDatapathIdColumn() == null) {
175             return null;
176         } else {
177             return createDatapathId(bridge.getDatapathIdColumn().getData());
178         }
179     }
180
181     public static DatapathId createDatapathId(final Set<String> dpids) {
182         requireNonNull(dpids);
183         if (dpids.isEmpty()) {
184             return null;
185         } else {
186             String[] dpidArray = new String[dpids.size()];
187             dpids.toArray(dpidArray);
188             return createDatapathId(dpidArray[0]);
189         }
190     }
191
192     public static DatapathId createDatapathId(final String dpid) {
193         requireNonNull(dpid);
194         DatapathId datapath;
195         if (dpid.matches("^[0-9a-fA-F]{16}")) {
196             Splitter splitter = Splitter.fixedLength(2);
197             Joiner joiner = Joiner.on(":");
198             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
199         } else {
200             datapath = new DatapathId(dpid);
201         }
202         return datapath;
203     }
204
205     public static String createDatapathType(final OvsdbBridgeAugmentation mdsalbridge) {
206         String datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class);
207
208         if (mdsalbridge.getDatapathType() != null && !mdsalbridge.getDatapathType().equals(DatapathTypeBase.class)) {
209             datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
210             if (datapathtype == null) {
211                 throw new IllegalArgumentException("Unknown datapath type " + mdsalbridge.getDatapathType().getName());
212             }
213         }
214         return datapathtype;
215     }
216
217     public static Class<? extends DatapathTypeBase> createDatapathType(final String type) {
218         if (type.isEmpty()) {
219             return DatapathTypeSystem.class;
220         }
221         return SouthboundConstants.DATAPATH_TYPE_MAP.inverse().get(type);
222     }
223
224     public static Set<String> createOvsdbBridgeProtocols(final OvsdbBridgeAugmentation ovsdbBridgeNode) {
225         Set<String> protocols = new HashSet<>();
226         Map<ProtocolEntryKey, ProtocolEntry> entries = ovsdbBridgeNode.getProtocolEntry();
227         if (entries != null) {
228             for (ProtocolEntry protocol : entries.values()) {
229                 Class<? extends OvsdbBridgeProtocolBase> lookup = protocol.getProtocol();
230                 final String toAdd = SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol());
231                 Preconditions.checkArgument(toAdd != null, "Unknown protocol %s", lookup);
232                 protocols.add(toAdd);
233             }
234         }
235         return protocols;
236     }
237
238     public static  Class<? extends InterfaceTypeBase> createInterfaceType(final String type) {
239         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(requireNonNull(type));
240     }
241
242     public static String createOvsdbInterfaceType(final Class<? extends InterfaceTypeBase> mdsaltype) {
243         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse().get(requireNonNull(mdsaltype));
244     }
245
246     public static List<ProtocolEntry> createMdsalProtocols(final Bridge bridge) {
247         Set<String> protocols = null;
248         try {
249             protocols = bridge.getProtocolsColumn().getData();
250         } catch (SchemaVersionMismatchException e) {
251             schemaMismatchLog("protocols", "Bridge", e);
252         }
253         List<ProtocolEntry> protocolList = new ArrayList<>();
254         if (protocols != null && !protocols.isEmpty()) {
255             ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
256                     SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
257             for (String protocol : protocols) {
258                 if (protocol != null) {
259                     final Class<? extends OvsdbBridgeProtocolBase> mapped = mapper.get(protocol);
260                     if (mapped != null) {
261                         protocolList.add(new ProtocolEntryBuilder().setProtocol(mapped).build());
262                     }
263                 }
264             }
265         }
266         return protocolList;
267     }
268
269     /**
270      * Create the {@link ControllerEntry} list given an OVSDB {@link Bridge}
271      * and {@link Controller} rows.
272      *
273      * @param bridge the {@link Bridge} to update
274      * @param updatedControllerRows the list of {@link Controller} controllers with updates
275      * @return list of {@link ControllerEntry} entries
276      */
277     public static List<ControllerEntry> createControllerEntries(final Bridge bridge,
278                                                                 final Map<UUID, Controller> updatedControllerRows) {
279
280         LOG.debug("createControllerEntries Bridge: {}\n, updatedControllerRows: {}",
281                 bridge, updatedControllerRows);
282         final Set<UUID> controllerUuids = bridge.getControllerColumn().getData();
283         final List<ControllerEntry> controllerEntries = new ArrayList<>();
284         for (UUID controllerUuid : controllerUuids) {
285             final Controller controller = updatedControllerRows.get(controllerUuid);
286             addControllerEntries(controllerEntries, controller);
287         }
288         LOG.debug("controllerEntries: {}", controllerEntries);
289         return controllerEntries;
290     }
291
292     /**
293      * Create the {@link ControllerEntry} list given an MDSAL {@link Node} bridge
294      * and {@link Controller} rows.
295      *
296      * @param bridgeNode the {@link Node} to update
297      * @param updatedControllerRows the list of {@link Controller} controllers with updates
298      * @return list of {@link ControllerEntry} entries
299      */
300     public static List<ControllerEntry> createControllerEntries(final Node bridgeNode,
301                                                                 final Map<UUID, Controller> updatedControllerRows) {
302
303         LOG.debug("createControllerEntries Bridge 2: {}\n, updatedControllerRows: {}",
304                 bridgeNode, updatedControllerRows);
305         final List<ControllerEntry> controllerEntriesCreated = new ArrayList<>();
306         final OvsdbBridgeAugmentation ovsdbBridgeAugmentation =
307                 bridgeNode.augmentation(OvsdbBridgeAugmentation.class);
308         if (ovsdbBridgeAugmentation == null) {
309             return controllerEntriesCreated;
310         }
311
312         final Map<ControllerEntryKey, ControllerEntry> controllerEntries = ovsdbBridgeAugmentation.getControllerEntry();
313         if (controllerEntries != null) {
314             for (ControllerEntry controllerEntry : controllerEntries.values()) {
315                 final Controller controller = updatedControllerRows.get(
316                         new UUID(controllerEntry.getControllerUuid().getValue()));
317                 addControllerEntries(controllerEntriesCreated, controller);
318             }
319         }
320         LOG.debug("controllerEntries: {}", controllerEntriesCreated);
321         return controllerEntriesCreated;
322     }
323
324     /**
325      * Add the OVSDB {@link Controller} updates to the MDSAL {@link ControllerEntry} list.
326      *
327      * @param controllerEntries the list of {@link ControllerEntry} to update
328      * @param controller the updated OVSDB {@link Controller}
329      */
330     public static void addControllerEntries(final List<ControllerEntry> controllerEntries,
331                                             final Controller controller) {
332
333         if (controller != null && controller.getTargetColumn() != null) {
334             final String targetString = controller.getTargetColumn().getData();
335             final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid uuid =
336                     new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
337                             .ietf.yang.types.rev130715.Uuid(controller.getUuid().toString());
338             ControllerEntryBuilder builder = new ControllerEntryBuilder();
339
340             if (controller.getMaxBackoffColumn() != null && controller.getMaxBackoffColumn().getData() != null
341                     && !controller.getMaxBackoffColumn().getData().isEmpty()) {
342                 builder.setMaxBackoff(Uint32.valueOf(controller.getMaxBackoffColumn().getData().iterator().next()));
343             }
344             if (controller.getInactivityProbeColumn() != null && controller.getInactivityProbeColumn().getData() != null
345                     && !controller.getInactivityProbeColumn().getData().isEmpty()) {
346                 builder.setInactivityProbe(
347                     Uint32.valueOf(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(Uint16.valueOf(client.getConnectionInfo().getRemotePort())));
380         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
381         connectionInfoBuilder.setLocalPort(new PortNumber(Uint16.valueOf(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             Uint32 numberOfConnections = Uint32.ZERO;
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 = Uint32.valueOf(numberOfConnectionValueStr);
464             } else {
465                 final boolean isConnected = manager.getIsConnectedColumn().getData();
466                 if (isConnected) {
467                     numberOfConnections = Uint32.ONE;
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         requireNonNull(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 }