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