Fix NPE on unicreate 86/29186/1
authorgrmontpetit <grmontpetit@inocybe.com>
Tue, 3 Nov 2015 13:05:10 +0000 (08:05 -0500)
committergrmontpetit <grmontpetit@inocybe.com>
Tue, 3 Nov 2015 13:05:27 +0000 (08:05 -0500)
Change-Id: Ia0ba8722181dbe99a5752d51f94493732d022e12
Signed-off-by: grmontpetit <grmontpetit@inocybe.com>
impl/src/main/java/org/opendaylight/unimgr/command/UniCreateCommand.java
impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrUtils.java

index 90cdc51f962b5faf22e9a910c909992f3e9b627c..8aa4be29c10296a093146e6df39fa1c00cd5864b 100644 (file)
@@ -58,8 +58,8 @@ public class UniCreateCommand extends AbstractCreateCommand {
                  * Passive mode (PTCP): the UUID is in format ovsdb://IP:6640
                  *
                  */
-                OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
-                if (ovsdbNodeRef != null || ovsdbNodeRef.getValue() != null) {
+                if (uni.getOvsdbNodeRef() != null) {
+                    OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
                     Optional<Node> optionalNode = UnimgrUtils.readNode(dataBroker, ovsdbNodeRef.getValue());
                     if (optionalNode.isPresent()) {
                         Node ovsdbNode = optionalNode.get();
@@ -98,8 +98,9 @@ public class UniCreateCommand extends AbstractCreateCommand {
                 } else {
                     // We assume the ovs is in passive mode
                     // Check if the ovsdb node exist
-                    Node ovsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
-                    if (ovsdbNode != null) {
+                    Node ovsdbNode;
+                    if (UnimgrUtils.findOvsdbNode(dataBroker, uni) != null) {
+                        ovsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
                         LOG.info("Retrieved the OVSDB node");
                         UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
                                                   uniKey,
@@ -112,17 +113,19 @@ public class UniCreateCommand extends AbstractCreateCommand {
                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
                     } else {
                         ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker, uni);
-                        LOG.info("Could not retrieve the OVSDB node,"
-                               + "created a new one: {}", ovsdbNode.getNodeId());
-                        UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
-                                                  uniKey,
-                                                  uni,
-                                                  ovsdbNode,
-                                                  dataBroker);
-                        UnimgrUtils.createBridgeNode(dataBroker,
-                                                     ovsdbNode,
-                                                     uni,
-                                                     UnimgrConstants.DEFAULT_BRIDGE_NAME);
+                        if (ovsdbNode != null) {
+                            LOG.info("Could not retrieve the OVSDB node,"
+                                    + "created a new one: {}", ovsdbNode.getNodeId());
+                             UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
+                                                       uniKey,
+                                                       uni,
+                                                       ovsdbNode,
+                                                       dataBroker);
+                             UnimgrUtils.createBridgeNode(dataBroker,
+                                                          ovsdbNode,
+                                                          uni,
+                                                          UnimgrConstants.DEFAULT_BRIDGE_NAME);
+                        }
                     }
                 }
             }
index cbe35ef5667badc1cdd4099e8800aba3f204f8e4..72d4afd89baebd16a6742b14fbd2956a4404a025 100644 (file)
@@ -135,11 +135,11 @@ public class UnimgrUtils {
 
     public static OvsdbNodeAugmentation createOvsdbNodeAugmentation(Uni uni) {
         ConnectionInfo connectionInfos = new ConnectionInfoBuilder()
-                .setRemoteIp(uni.getIpAddress())
-                .setRemotePort(new PortNumber(UnimgrConstants.OVSDB_PORT))
-                .build();
+                                                .setRemoteIp(uni.getIpAddress())
+                                                .setRemotePort(new PortNumber(UnimgrConstants.OVSDB_PORT))
+                                                .build();
         OvsdbNodeAugmentation ovsdbNode = new OvsdbNodeAugmentationBuilder()
-                .setConnectionInfo(connectionInfos).build();
+                                                .setConnectionInfo(connectionInfos).build();
         return ovsdbNode;
     }
 
@@ -220,18 +220,27 @@ public class UnimgrUtils {
     }
 
     public static Node createOvsdbNode(DataBroker dataBroker, UniAugmentation uni) {
-        List<Node> ovsdbNodes = getOvsdbNodes(dataBroker);
-        if (!ovsdbNodes.isEmpty()) {
-            for (Node ovsdbNode: ovsdbNodes) {
-                OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
-                        .getAugmentation(OvsdbNodeAugmentation.class);
-                if (ovsdbNodeAugmentation.getConnectionInfo()
-                                         .getRemoteIp().getIpv4Address()
-                                         .equals(uni.getIpAddress().getIpv4Address())) {
-                    LOG.info("Found ovsdb node");
-                    return ovsdbNode;
-                }
-            }
+        NodeId ovsdbNodeId = new NodeId(createOvsdbNodeId(uni.getIpAddress()));
+        try {
+            InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper.getOvsdbNodeIID(ovsdbNodeId);
+            NodeKey ovsdbNodeKey = new NodeKey(ovsdbNodeId);
+            Node nodeData = new NodeBuilder()
+                                    .setNodeId(ovsdbNodeId)
+                                    .setKey(ovsdbNodeKey)
+                                    .addAugmentation(OvsdbNodeAugmentation.class, UnimgrUtils.createOvsdbNodeAugmentation(uni))
+                                    .build();
+            // Submit the node to the datastore
+            WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
+            transaction.put(LogicalDatastoreType.CONFIGURATION,
+                            ovsdbNodeIid,
+                            nodeData);
+            transaction.submit();
+            LOG.info("Created and submitted a new OVSDB node {}",
+                    nodeData.getNodeId());
+            return nodeData;
+        } catch (Exception e) {
+            LOG.error("Exception while creating OvsdbNodeAugmentation, "
+                    + "Uni is null. Node Id: {}", ovsdbNodeId);
         }
         return null;
     }
@@ -358,7 +367,7 @@ public class UnimgrUtils {
     public static void createBridgeNode(DataBroker dataBroker, Node ovsdbNode, UniAugmentation uni, String bridgeName) {
         LOG.info("Creating a bridge on node {}", ovsdbNode.getNodeId());
         InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper
-                .getOvsdbNodeIID(uni.getIpAddress());
+                                                    .getOvsdbNodeIID(uni.getIpAddress());
         ConnectionInfo connectionInfo = UnimgrUtils.getConnectionInfo(dataBroker, ovsdbNode.getNodeId());
         if (connectionInfo != null) {
             NodeBuilder bridgeNodeBuilder = new NodeBuilder();