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