libpldm: Add pldm_entity_extract method

Add the pldm_entity_extract method in libpldm/pdr.h and extract
pldm entity structure explicitly.

Tested: built pldm daemon successfully and the unit test passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I330e0c76e75d01093148c3a432fcc18cea1d36e3
diff --git a/libpldm/pdr.c b/libpldm/pdr.c
index 3aa9541..92ac843 100644
--- a/libpldm/pdr.c
+++ b/libpldm/pdr.c
@@ -333,6 +333,13 @@
 	return ++tree->last_used_container_id;
 }
 
+pldm_entity pldm_entity_extract(pldm_entity_node *node)
+{
+	assert(node != NULL);
+
+	return node->entity;
+}
+
 pldm_entity_association_tree *pldm_entity_association_tree_init()
 {
 	pldm_entity_association_tree *tree =
diff --git a/libpldm/pdr.h b/libpldm/pdr.h
index 0489814..24137ab 100644
--- a/libpldm/pdr.h
+++ b/libpldm/pdr.h
@@ -244,6 +244,14 @@
 void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
 					pldm_entity **entities, size_t *size);
 
+/** @brief Extract pldm entity by the pldm_entity_node
+ *
+ *  @param[in] node     - opaque pointer to added entity
+ *
+ *  @return pldm_entity - pldm entity
+ */
+pldm_entity pldm_entity_extract(pldm_entity_node *node);
+
 /** @brief Destroy entity association tree
  *
  *  @param[in] tree - opaque pointer acting as a handle to the tree
diff --git a/libpldm/tests/libpldm_pdr_test.cpp b/libpldm/tests/libpldm_pdr_test.cpp
index bbf950d..454b072 100644
--- a/libpldm/tests/libpldm_pdr_test.cpp
+++ b/libpldm/tests/libpldm_pdr_test.cpp
@@ -607,6 +607,47 @@
     EXPECT_EQ(out[8].entity_container_id, 3u);
 
     free(out);
+
+    pldm_entity p1 = pldm_entity_extract(l1);
+    EXPECT_EQ(p1.entity_type, 1u);
+    EXPECT_EQ(p1.entity_instance_num, 1u);
+    EXPECT_EQ(p1.entity_container_id, 0u);
+
+    pldm_entity p2a = pldm_entity_extract(l2a);
+    EXPECT_EQ(p2a.entity_type, 2u);
+    EXPECT_EQ(p2a.entity_instance_num, 1u);
+    EXPECT_EQ(p2a.entity_container_id, 1u);
+    pldm_entity p2b = pldm_entity_extract(l2b);
+    EXPECT_EQ(p2b.entity_type, 2u);
+    EXPECT_EQ(p2b.entity_instance_num, 2u);
+    EXPECT_EQ(p2b.entity_container_id, 1u);
+    pldm_entity p2c = pldm_entity_extract(l2c);
+    EXPECT_EQ(p2c.entity_type, 3u);
+    EXPECT_EQ(p2c.entity_instance_num, 1u);
+    EXPECT_EQ(p2c.entity_container_id, 1u);
+
+    pldm_entity p3a = pldm_entity_extract(l3a);
+    EXPECT_EQ(p3a.entity_type, 4u);
+    EXPECT_EQ(p3a.entity_instance_num, 1u);
+    EXPECT_EQ(p3a.entity_container_id, 2u);
+    pldm_entity p3b = pldm_entity_extract(l3b);
+    EXPECT_EQ(p3b.entity_type, 5u);
+    EXPECT_EQ(p3b.entity_instance_num, 1u);
+    EXPECT_EQ(p3b.entity_container_id, 2u);
+    pldm_entity p3c = pldm_entity_extract(l3c);
+    EXPECT_EQ(p3c.entity_type, 5u);
+    EXPECT_EQ(p3c.entity_instance_num, 2u);
+    EXPECT_EQ(p3c.entity_container_id, 2u);
+
+    pldm_entity p4a = pldm_entity_extract(l4a);
+    EXPECT_EQ(p4a.entity_type, 6u);
+    EXPECT_EQ(p4a.entity_instance_num, 1u);
+    EXPECT_EQ(p4a.entity_container_id, 3u);
+    pldm_entity p4b = pldm_entity_extract(l4b);
+    EXPECT_EQ(p4b.entity_type, 7u);
+    EXPECT_EQ(p4b.entity_instance_num, 1u);
+    EXPECT_EQ(p4b.entity_container_id, 4u);
+
     pldm_entity_association_tree_destroy(tree);
 }