Ejemplo n.º 1
0
TEST(ConfdUtTranslation, ConfdNewBuilder)
{
  TEST_DESCRIPTION("Translate a RWXML DOM to Confd and back without list filter and new builder");
  XMLManager::uptr_t mgr(xml_manager_create_xerces());
  ASSERT_TRUE(mgr.get());

  YangModel* model = mgr->get_yang_model();
  ASSERT_TRUE(model);

  YangModule* ydom_top = model->load_module("base-conversion");
  EXPECT_TRUE(ydom_top);
  ydom_top = 0;
  
  ydom_top = model->load_module("flat-conversion");
  EXPECT_TRUE(ydom_top);

  model->register_ypbc_schema(
      (const rw_yang_pb_schema_t*)RWPB_G_SCHEMA_YPBCSD(FlatConversion));

  std::ifstream fp;
  
  ConfdUnittestHarness::uptr_t harness
    = ConfdUnittestHarness::create_harness_autostart("ConfdDOM", model);
  ASSERT_TRUE(harness.get());
  EXPECT_TRUE(harness->is_running());

  ConfdUnittestHarness::context_t* ctx= harness->get_confd_context();
  UNUSED (ctx);

  ConfdUnittestHarness::sockaddr_t sa;
  harness->make_confd_sockaddr(&sa);

  EXPECT_TRUE(harness->wait_till_phase2(&sa));

  {

    bool ret = model->app_data_get_token (YANGMODEL_ANNOTATION_KEY,YANGMODEL_ANNOTATION_CONFD_NS ,
                                          &model->adt_confd_ns_);
    RW_ASSERT(ret);
    
    ret =  model->app_data_get_token (YANGMODEL_ANNOTATION_KEY, YANGMODEL_ANNOTATION_CONFD_NAME,
                                      &model->adt_confd_name_);
    RW_ASSERT(ret);

    ASSERT_EQ (CONFD_OK, confd_load_schemas ((struct sockaddr *)&sa, sizeof(sa)) );

    namespace_map_t ns_map;
    struct confd_nsinfo *listp;
    
    uint32_t ns_count = confd_get_nslist(&listp);
    
    RW_ASSERT (ns_count); // for now
    
    for (uint32_t i = 0; i < ns_count; i++) {
      ns_map[listp[i].uri] = listp[i].hash;
    }
    
    rw_confd_annotate_ynodes (model, ns_map, confd_str2hash,
                              YANGMODEL_ANNOTATION_CONFD_NS,
                              YANGMODEL_ANNOTATION_CONFD_NAME );


  // Create a DOM
  std::string file_name = get_rift_root() +
      std::string("/modules/core/util/yangtools/test/confd.xml");

  XMLDocument::uptr_t dom(mgr->create_document_from_file
                              (file_name.c_str(), false/*validate*/));

  ASSERT_TRUE(dom.get());

  XMLNode* root_node = dom->get_root_node();
  ASSERT_TRUE (root_node);

  root_node = root_node->get_first_child();
  // associated confd structures
  struct confd_cs_node *cs_node = confd_find_cs_root(flat_conversion__ns);

  // Translate the DOM to confd
  
  ConfdTLVBuilder builder(cs_node,true);
  XMLTraverser traverser(&builder,root_node);

   traverser.traverse();


  ASSERT_TRUE (builder.length());

  size_t length = builder.length();

  // Add 3 more  - to add a delete keyspec
  confd_tag_value_t
      *array =  (confd_tag_value_t *) RW_MALLOC (sizeof (confd_tag_value_t) * (length + 3));

  builder.copy_and_destroy_tag_array(array);
  

  // Array is of the following form for the test
  // The nodes with C++ comments added to test deletion
  /*  1      C_XMLBEGIN  "container_1-1"         */
  /*  2          C_BUF "leaf-1_1.1"              */
  /*  3          C_XMLBEGIN "list-1.1_2"         */
  /*  4      	C_INT32 "int_1.1.2_1"              */
  /*  5          C_XMLEND "list-1.1_2"           */
  /*  6          C_XMLBEGIN "list-1.1_2"         */
  /*  7              C_XMLTAG "grey-goose"       */
  /*  8              C_INT32 "int_1.1.2_1"       */
  /*  9          C_XMLEND  "list-1.1_2"          */
  // 10          C_XMLBEGINDEL "list-1.1_2"         */
  // 11              C_INT32 "int_1.1.2_1"       */
  // 12          C_XMLEND  "list-1.1_2"          */
  /* 13      C_XMLEND "container_1-1"            */
  /* 14      C_LIST "leaf.list.1.2"              */
  /* 15      C_XMLBEGIN "two-keys"               */
  /* 16          C_ENUM_VALUE "prim-enum"        */
  /* 17          C_BUF "sec-string"              */
  /* 18      C_XMLEND "two-keys"                 */
  /* 19      C_XMLBEGIN "two-keys"               */
  /* 20          C_ENUM_VALUE "prim-enum"        */
  /* 21          C_BUF "sec-string"              */
  /* 22      C_XMLEND "two-keys"                 */
  /* 23      C_XMLTAG "empty-1_3"                */
  /* 24      C_LIST "enum_1-4"                   */
  /* 25      C_ENUM_VALUE "enum_1.5"             */

  // add 3 lines in the middle
  memmove (&array[12], &array[9], sizeof (confd_tag_value_t) * 13);
  memcpy (&array[9], &array[2],  sizeof (confd_tag_value_t) * 3);
  array[9].v.type = C_XMLBEGINDEL;
  array[10].v.val.i32 = 0xdeadbeef;
  
  length += 3;
    // Translate the confd to DOM
  rw_yang::XMLDocument::uptr_t converted = 
      std::move(mgr->create_document(model->get_root_node()));

  XMLNode *root = converted->get_root_node();

  YangNode *container_1 = ydom_top->get_first_node();
  XMLNode *build_node = root->add_child (container_1);
  XMLBuilder xml_builder(build_node);
  
  ConfdTLVTraverser tlv_traverser(&xml_builder, array, length );

  
  tlv_traverser.traverse();
  std::string orig = dom->get_root_node()->get_first_element()->to_string();
  std::string copy = xml_builder.current_node_->to_string();

  ASSERT_EQ (1, xml_builder.delete_ks_.size());

  for (size_t i = 0; i < length; i++) {
    confd_free_value(CONFD_GET_TAG_VALUE(&array[i]));
  }

  RW_FREE (array);

  }
}
Ejemplo n.º 2
0
TEST(ConfdUtTranslation, DomToConfd)
{
  TEST_DESCRIPTION("Translate a RWXML DOM to Confd and back");
  XMLManager::uptr_t mgr(xml_manager_create_xerces());
  ASSERT_TRUE(mgr.get());

  YangModel* model = mgr->get_yang_model();
  ASSERT_TRUE(model);

  YangModule* ydom_top = model->load_module("base-conversion");
  EXPECT_TRUE(ydom_top);
  ydom_top = 0;
  
  ydom_top = model->load_module("flat-conversion");
  EXPECT_TRUE(ydom_top);

  YangNode *container_1 = ydom_top->get_first_node();
  
  std::ifstream fp;
  
  ConfdUnittestHarness::uptr_t harness
    = ConfdUnittestHarness::create_harness_autostart("ConfdDOM", model);
  ASSERT_TRUE(harness.get());
  EXPECT_TRUE(harness->is_running());

  ConfdUnittestHarness::context_t* ctx= harness->get_confd_context();
  UNUSED (ctx);

  ConfdUnittestHarness::sockaddr_t sa;
  harness->make_confd_sockaddr(&sa);

  EXPECT_TRUE(harness->wait_till_phase2(&sa));

  {

    bool ret = model->app_data_get_token (YANGMODEL_ANNOTATION_KEY,YANGMODEL_ANNOTATION_CONFD_NS ,
                                          &model->adt_confd_ns_);
    RW_ASSERT(ret);
    
    ret =  model->app_data_get_token (YANGMODEL_ANNOTATION_KEY, YANGMODEL_ANNOTATION_CONFD_NAME,
                                      &model->adt_confd_name_);
    RW_ASSERT(ret);

    ASSERT_EQ (CONFD_OK, confd_load_schemas ((struct sockaddr *)&sa, sizeof(sa)) );

    namespace_map_t ns_map;
    struct confd_nsinfo *listp;
    
    uint32_t ns_count = confd_get_nslist(&listp);
    
    RW_ASSERT (ns_count); // for now
    
    for (uint32_t i = 0; i < ns_count; i++) {
      ns_map[listp[i].uri] = listp[i].hash;
    }
    
    rw_confd_annotate_ynodes (model, ns_map, confd_str2hash,
                              YANGMODEL_ANNOTATION_CONFD_NS,
                              YANGMODEL_ANNOTATION_CONFD_NAME );


  // Create a DOM
  std::string file_name = get_rift_root() +
      std::string("/modules/core/util/yangtools/test/confd.xml");

  XMLDocument::uptr_t dom(mgr->create_document_from_file
                              (file_name.c_str(), false/*validate*/));

  ASSERT_TRUE(dom.get());

  XMLNode* root_node = dom->get_root_node();
  ASSERT_TRUE (root_node);

  root_node = root_node->get_first_child();

  // The DOMs should be similar - EXCEPT that the new one will not have
  // the list children.. And only YANG nodes assocaited nodes will be present

  rw_yang::XMLDocument::uptr_t filtered = 
      std::move(mgr->create_document(model->get_root_node()));

  XMLNode* filter_root = filtered->get_root_node();

  XMLNoListCopier copier (filter_root);
  XMLWalkerInOrder walker (root_node);

  walker.walk(&copier);

  // associated confd structures
  struct confd_cs_node *cs_node = confd_find_cs_root(flat_conversion__ns);

  // Translate the DOM to confd
  
  ConfdTLVBuilder builder(cs_node);
  XMLTraverser traverser(&builder,root_node);

  traverser.traverse();

  ASSERT_TRUE (builder.length());

  size_t length = builder.length();
  
  confd_tag_value_t
      *array =  (confd_tag_value_t *) RW_MALLOC (sizeof (confd_tag_value_t) * length);

  builder.copy_and_destroy_tag_array(array);

  // Translate the confd to DOM
  rw_yang::XMLDocument::uptr_t converted = 
      std::move(mgr->create_document(model->get_root_node()));

  XMLNode *root = converted->get_root_node();



  XMLNode *build_node = root->add_child (container_1);
  XMLBuilder xml_builder(build_node);
  
  ConfdTLVTraverser tlv_traverser(&xml_builder, array, length );
  tlv_traverser.traverse();

  for (size_t i = 0; i < length; i++) {
    confd_free_value(CONFD_GET_TAG_VALUE(&array[i]));
  }
  RW_FREE (array);
    
  std::string orig = filter_root->to_string();
  std::string copied = root->to_string();

  ASSERT_STREQ (orig.c_str(), copied.c_str());
  }

  harness->stop();
}